Skip to content

Commit

Permalink
Merge pull request #57 from JohnSchmeichel/colors
Browse files Browse the repository at this point in the history
Support foreground and background colors that differ from the color palette
  • Loading branch information
terrajobst authored Feb 6, 2024
2 parents d39e0af + 1500d22 commit 71d0ad4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
12 changes: 6 additions & 6 deletions src/git-istage/Patches/PatchDocumentLineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;

Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/git-istage/UI/FileDocumentLineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/git-istage/UI/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -17,7 +17,7 @@ public Label(int top, int left, int right)
_width = right - left;
}

public ConsoleColor Foreground
public ConsoleColor? Foreground
{
get => _foreground;
set
Expand All @@ -27,7 +27,7 @@ public ConsoleColor Foreground
}
}

public ConsoleColor Background
public ConsoleColor? Background
{
get => _background;
set
Expand Down
4 changes: 3 additions & 1 deletion src/git-istage/UI/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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++)
Expand All @@ -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++)
Expand Down
6 changes: 3 additions & 3 deletions src/git-istage/UI/ViewLineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
76 changes: 43 additions & 33 deletions src/git-istage/UI/Vt100.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Expand Down

0 comments on commit 71d0ad4

Please sign in to comment.