Skip to content

Commit

Permalink
lib stuff (use ansi colors instead of console.foreground)
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 25, 2024
1 parent 5cab3c2 commit e538d71
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
8 changes: 3 additions & 5 deletions Lib/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,10 @@ public static void RunAll(bool executeSolvers, bool showSplashScreen, bool showL
}

private static void WriteLine(ConsoleColor color = ConsoleColor.Gray, string text = "") {
Write(color, text + "\n");
Terminal.WriteLine(color, text);
}
private static void Write(ConsoleColor color = ConsoleColor.Gray, string text = "") {
var c = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(text);
Console.ForegroundColor = c;
Terminal.Write(color, text);
}

}
5 changes: 1 addition & 4 deletions Lib/SlocChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ public static void Show(int year, List<(int day, int sloc)> slocs) {
rows.Insert(0, row);
}

var c = Console.ForegroundColor;
foreach (var row in rows) {
foreach (var item in row) {
Console.ForegroundColor = item.c;
Console.Write(item.st);
Terminal.Write(item.c, item.st);
}
Console.WriteLine();
}
Console.ForegroundColor = c;
Console.WriteLine("");
}
}
36 changes: 36 additions & 0 deletions Lib/Terminal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

using System;

class Terminal {
public static void WriteLine(ConsoleColor color = ConsoleColor.Gray, string text = "") {
Write(color, text + "\n");
}
public static void Write(ConsoleColor color = ConsoleColor.Gray, string text = "") {

Console.Write($"\u001b[{ToAnsiColorCode(color)}");
Console.Write(text);
Console.Write("\u001b[0m");
}

private static string ToAnsiColorCode(ConsoleColor color) {
switch(color) {
case ConsoleColor.Black: return "30m";
case ConsoleColor.DarkRed: return "31m";
case ConsoleColor.DarkGreen: return "32m";
case ConsoleColor.DarkYellow: return "33m";
case ConsoleColor.DarkBlue: return "34m";
case ConsoleColor.DarkMagenta: return "35m";
case ConsoleColor.DarkCyan: return "36m";
case ConsoleColor.DarkGray: return "37m";
case ConsoleColor.Gray: return "90m";
case ConsoleColor.Red: return "91m";
case ConsoleColor.Green: return "92m";
case ConsoleColor.Yellow: return "93m";
case ConsoleColor.Blue: return "94m";
case ConsoleColor.Magenta: return "95m";
case ConsoleColor.Cyan: return "96m";
case ConsoleColor.White: return "97m";
}
throw new Exception($"unhandled color code {color}");
}
}

0 comments on commit e538d71

Please sign in to comment.