diff --git a/src/git-istage/Patches/PatchDocumentLineRenderer.cs b/src/git-istage/Patches/PatchDocumentLineRenderer.cs index 5c230b3..9b5f823 100644 --- a/src/git-istage/Patches/PatchDocumentLineRenderer.cs +++ b/src/git-istage/Patches/PatchDocumentLineRenderer.cs @@ -12,11 +12,11 @@ internal sealed class PatchDocumentLineRenderer : ViewLineRenderer return document?.Lines[lineIndex]; } - private static ConsoleColor GetForegroundColor(View view, int lineIndex) + private static ConsoleColor? GetForegroundColor(View view, int lineIndex) { var line = GetLine(view, lineIndex); if (line is null) - return ConsoleColor.Gray; + return null; switch (line.Kind) { @@ -33,15 +33,15 @@ private static ConsoleColor GetForegroundColor(View view, int lineIndex) case PatchLineKind.Removal: return ConsoleColor.DarkRed; default: - return ConsoleColor.Gray; + return null; } } - private static ConsoleColor GetBackgroundColor(View view, int lineIndex) + private static ConsoleColor? GetBackgroundColor(View view, int lineIndex) { var patchLine = GetLine(view, lineIndex); if (patchLine is null) - return ConsoleColor.Black; + return null; var kind = patchLine.Kind; @@ -53,7 +53,7 @@ private static ConsoleColor GetBackgroundColor(View view, int lineIndex) : ConsoleColor.DarkGray; } - return kind == PatchLineKind.DiffLine ? ConsoleColor.DarkBlue : ConsoleColor.Black; + return kind == PatchLineKind.DiffLine ? ConsoleColor.DarkBlue : null; } public override void Render(View view, int lineIndex) diff --git a/src/git-istage/UI/FileDocumentLineRenderer.cs b/src/git-istage/UI/FileDocumentLineRenderer.cs index 504a4ab..2018f20 100644 --- a/src/git-istage/UI/FileDocumentLineRenderer.cs +++ b/src/git-istage/UI/FileDocumentLineRenderer.cs @@ -12,11 +12,11 @@ internal sealed class FileDocumentLineRenderer : ViewLineRenderer return document?.GetChange(lineIndex); } - private static ConsoleColor GetForegroundColor(View view, int lineIndex) + private static ConsoleColor? GetForegroundColor(View view, int lineIndex) { var changes = GetChanges(view, lineIndex); if (changes is null) - return ConsoleColor.Gray; + return null; switch (changes.Status) { @@ -34,11 +34,11 @@ private static ConsoleColor GetForegroundColor(View view, int lineIndex) case ChangeKind.TypeChanged: case ChangeKind.Unreadable: default: - return ConsoleColor.Gray; + return null; } } - private static ConsoleColor GetBackgroundColor(View view, int lineIndex) + private static ConsoleColor? GetBackgroundColor(View view, int lineIndex) { var foregroundColor = GetForegroundColor(view, lineIndex); @@ -50,7 +50,7 @@ private static ConsoleColor GetBackgroundColor(View view, int lineIndex) : ConsoleColor.DarkGray; } - return ConsoleColor.Black; + return null; } public override void Render(View view, int lineIndex) diff --git a/src/git-istage/UI/Label.cs b/src/git-istage/UI/Label.cs index fc18f3d..a506d58 100644 --- a/src/git-istage/UI/Label.cs +++ b/src/git-istage/UI/Label.cs @@ -6,8 +6,8 @@ internal sealed class Label private readonly int _left; private readonly int _width; - private ConsoleColor _foreground = ConsoleColor.Gray; - private ConsoleColor _background = ConsoleColor.Black; + private ConsoleColor? _foreground = null; + private ConsoleColor? _background = null; private string _text = string.Empty; public Label(int top, int left, int right) @@ -17,7 +17,7 @@ public Label(int top, int left, int right) _width = right - left; } - public ConsoleColor Foreground + public ConsoleColor? Foreground { get => _foreground; set @@ -27,7 +27,7 @@ public ConsoleColor Foreground } } - public ConsoleColor Background + public ConsoleColor? Background { get => _background; set diff --git a/src/git-istage/UI/View.cs b/src/git-istage/UI/View.cs index de78c92..db03d17 100644 --- a/src/git-istage/UI/View.cs +++ b/src/git-istage/UI/View.cs @@ -159,7 +159,7 @@ private static void RenderNonExistingLine(int visualLine) { Vt100.SetCursorPosition(0, visualLine); Vt100.SetForegroundColor(ConsoleColor.DarkGray); - Vt100.SetBackgroundColor(ConsoleColor.Black); + Vt100.SetBackgroundColor(); Console.Write("~"); Vt100.EraseRestOfCurrentLine(); } @@ -209,6 +209,7 @@ private void UpdateTopLine(int value) { // We need to scroll up by -delta lines. + Vt100.SetBackgroundColor(); Vt100.ScrollDown(Math.Abs(delta)); for (var i = 0; i < -delta; i++) @@ -223,6 +224,7 @@ private void UpdateTopLine(int value) var visualLineCount = Height - delta; + Vt100.SetBackgroundColor(); Vt100.ScrollUp(delta); for (var i = 0; i < delta; i++) diff --git a/src/git-istage/UI/ViewLineRenderer.cs b/src/git-istage/UI/ViewLineRenderer.cs index 8f955e1..6a5318c 100644 --- a/src/git-istage/UI/ViewLineRenderer.cs +++ b/src/git-istage/UI/ViewLineRenderer.cs @@ -8,12 +8,12 @@ public virtual void Render(View view, int lineIndex) { var line = view.Document.GetLine(lineIndex); var isSelected = view.SelectedLine == lineIndex; - var foregroundColor = ConsoleColor.Gray; - var backgroundColor = isSelected ? ConsoleColor.DarkGray : ConsoleColor.Black; + ConsoleColor? foregroundColor = null; + ConsoleColor? backgroundColor = isSelected ? ConsoleColor.DarkGray : null; RenderLine(view, lineIndex, line, foregroundColor, backgroundColor); } - protected static void RenderLine(View view, int lineIndex, string line, ConsoleColor foregroundColor, ConsoleColor backgroundColor) + protected static void RenderLine(View view, int lineIndex, string line, ConsoleColor? foregroundColor, ConsoleColor? backgroundColor) { var textStart = Math.Min(view.LeftChar, line.Length); var textLength = Math.Max(Math.Min(view.Width, line.Length - view.LeftChar), 0); diff --git a/src/git-istage/UI/Vt100.cs b/src/git-istage/UI/Vt100.cs index ea8f7fb..cd3104e 100644 --- a/src/git-istage/UI/Vt100.cs +++ b/src/git-istage/UI/Vt100.cs @@ -37,48 +37,58 @@ public static void PositiveColors() Console.Write("\x1b[27m"); } - public static void SetForegroundColor(int r, int g, int b) + public static void SetForegroundColor(ConsoleColor? color = null) { - Console.Write($"\x1b[38;2;{r};{g};{b}m"); + // If color is null, set to default + var code = color != null ? GetColor(color.Value, foreground: true) : 39; + Console.Write($"\x1b[{(int)code}m"); } - public static void SetBackgroundColor(int r, int g, int b) + public static void SetBackgroundColor(ConsoleColor? color = null) { - Console.Write($"\x1b[48;2;{r};{g};{b}m"); + // If color is null, set to default + var code = color != null ? GetColor(color.Value, foreground: false) : 49; + Console.Write($"\x1b[{(int)code}m"); } - public static void SetForegroundColor(ConsoleColor color) - { - var (r, g, b) = GetColor(color); - SetForegroundColor(r, g, b); - } - - public static void SetBackgroundColor(ConsoleColor color) - { - var (r, g, b) = GetColor(color); - SetBackgroundColor(r, g, b); - } - - private static (int R, int G, int B) GetColor(ConsoleColor color) + private static int GetColor(ConsoleColor color, bool foreground) { switch (color) { - case ConsoleColor.Black: return (12, 12, 12); - case ConsoleColor.DarkBlue: return (0, 55, 218); - case ConsoleColor.DarkGreen: return (19, 161, 14); - case ConsoleColor.DarkCyan: return (58, 150, 221); - case ConsoleColor.DarkRed: return (197, 15, 31); - case ConsoleColor.DarkMagenta: return (136, 23, 152); - case ConsoleColor.DarkYellow: return (193, 156, 0); - case ConsoleColor.Gray: return (204, 204, 204); - case ConsoleColor.DarkGray: return (118, 118, 118); - case ConsoleColor.Blue: return (59, 120, 255); - case ConsoleColor.Green: return (22, 198, 12); - case ConsoleColor.Cyan: return (97, 214, 214); - case ConsoleColor.Red: return (231, 72, 86); - case ConsoleColor.Magenta: return (180, 0, 158); - case ConsoleColor.Yellow: return (249, 241, 165); - case ConsoleColor.White: return (242, 242, 242); + case ConsoleColor.Black when foreground: return 30; + case ConsoleColor.DarkBlue when foreground: return 34; + case ConsoleColor.DarkGreen when foreground: return 32; + case ConsoleColor.DarkCyan when foreground: return 36; + case ConsoleColor.DarkRed when foreground: return 31; + case ConsoleColor.DarkMagenta when foreground: return 35; + case ConsoleColor.DarkYellow when foreground: return 33; + case ConsoleColor.Gray when foreground: return 37; + case ConsoleColor.DarkGray when foreground: return 90; + case ConsoleColor.Blue when foreground: return 94; + case ConsoleColor.Green when foreground: return 92; + case ConsoleColor.Cyan when foreground: return 96; + case ConsoleColor.Red when foreground: return 91; + case ConsoleColor.Magenta when foreground: return 95; + case ConsoleColor.Yellow when foreground: return 93; + case ConsoleColor.White when foreground: return 97; + + case ConsoleColor.Black when !foreground: return 40; + case ConsoleColor.DarkBlue when !foreground: return 44; + case ConsoleColor.DarkGreen when !foreground: return 42; + case ConsoleColor.DarkCyan when !foreground: return 46; + case ConsoleColor.DarkRed when !foreground: return 41; + case ConsoleColor.DarkMagenta when !foreground: return 45; + case ConsoleColor.DarkYellow when !foreground: return 43; + case ConsoleColor.Gray when !foreground: return 47; + case ConsoleColor.DarkGray when !foreground: return 100; + case ConsoleColor.Blue when !foreground: return 104; + case ConsoleColor.Green when !foreground: return 102; + case ConsoleColor.Cyan when !foreground: return 106; + case ConsoleColor.Red when !foreground: return 101; + case ConsoleColor.Magenta when !foreground: return 105; + case ConsoleColor.Yellow when !foreground: return 103; + case ConsoleColor.White when !foreground: return 107; + default: throw new Exception($"Unexpected color: {color}"); }