Skip to content

Commit

Permalink
fix(logger): log not showing colors on windows #20
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Jan 2, 2022
1 parent 29c5e17 commit 498bc9c
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 23 deletions.
28 changes: 28 additions & 0 deletions src/Husky/Cli.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.InteropServices;

namespace Husky;

public static class Cli
Expand All @@ -13,6 +15,8 @@ private static void Exit(int code)

public static async ValueTask Start(string[] args)
{
HandleEvnArguments();

if (!args.Any())
{
Help();
Expand All @@ -37,6 +41,30 @@ public static async ValueTask Start(string[] args)
}
}

private static void HandleEvnArguments()
{
if (Environment.GetEnvironmentVariable("vt100") == "1")
{
try
{
// ENABLE_VIRTUAL_TERMINAL_PROCESSING
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Win32Console.Initialize();
Logger.Vt100Colors = true;
}
}
catch (Exception e)
{
e.Message.LogVerbose(ConsoleColor.Red);
}
}
if (Environment.GetEnvironmentVariable("HUSKY_DEBUG") == "1")
{
Logger.Verbose = true;
}
}


private static string[] HandleHighPriorityArgs(string[] args)
{
Expand Down
55 changes: 32 additions & 23 deletions src/Husky/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,53 @@ public static class Logger
{
public static bool Verbose = false;
public static bool Colors = true;
public static bool Vt100Colors = false;

public static void Husky(this string message, ConsoleColor? color = null)
private static void Write(string message, ConsoleColor? color = null)
{
if (Colors)
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("[Husky] ");
Console.ResetColor();

if (Colors && color != null)
Console.ForegroundColor = color.Value;
Console.Write($"{message}\n");
Console.ResetColor();
{
if (Vt100Colors)
{
Vt100.Write(message, color.Value);
}
else
{
Console.ForegroundColor = color.Value;
Console.Write(message);
Console.ResetColor();
}
}
else
Console.Write(message);
}

public static void Log(this string message, ConsoleColor? color = null)
private static void WriteLine(string message, ConsoleColor? color = null)
{
if (Colors && color != null)
Console.ForegroundColor = color.Value;
Write(message, color);
Write(Environment.NewLine);
}

public static void Husky(this string message, ConsoleColor? color = null)
{
Write("[Husky] ", ConsoleColor.Cyan);
WriteLine($"{message}", color);
}

Console.WriteLine(message);
Console.ResetColor();
public static void Log(this string message, ConsoleColor? color = null)
{
WriteLine(message, color);
}

public static void Hr(int count = 50, ConsoleColor? color = ConsoleColor.DarkGray)
{
if (Colors && color != null)
Console.ForegroundColor = color.Value;

Console.WriteLine(new string('-', count));
Console.ResetColor();
WriteLine(new string('-', count), color);
}

public static void LogVerbose(this string message, ConsoleColor color = ConsoleColor.DarkGray)
{
if (!Verbose) return;
if (Colors)
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
WriteLine(message, color);
}

public static void LogErr(this string message)
Expand Down
152 changes: 152 additions & 0 deletions src/Husky/Vt100.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;

internal static class Vt100
{
public static void SwitchToAlternateBuffer()
{
Console.Write("\x1b[?1049h");
}

public static void SwitchToMainBuffer()
{
Console.Write("\x1b[?1049l");
}

public static void ShowCursor()
{
Console.Write("\x1b[?25h");
}

public static void HideCursor()
{
Console.Write("\x1b[?25l");
}

public static void SetCursorPosition(int x, int y)
{
Console.Write($"\x1b[{y + 1};{x + 1}H");
}

public static void NegativeColors()
{
Console.Write("\x1b[7m");
}

public static void PositiveColors()
{
Console.Write("\x1b[27m");
}

public static void Write(string message, ConsoleColor color)
{
SetForegroundColor(color);
Console.Write(message);
ResetColors();
}

public static void WriteLine(string message, ConsoleColor color)
{
SetForegroundColor(color);
Console.WriteLine(message);
ResetColors();
}

/// <summary>
/// https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting
/// Git-bash only supports 8 colors provided by the Windows Console.
/// </summary>
/// <param name="color"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static void SetForegroundColor(ConsoleColor color)
{
var colorCode = color switch
{
ConsoleColor.Black => 30,
ConsoleColor.Red or ConsoleColor.DarkRed => 31,
ConsoleColor.Green or ConsoleColor.DarkGreen => 32,
ConsoleColor.Yellow or ConsoleColor.DarkYellow => 33,
ConsoleColor.Blue or ConsoleColor.DarkBlue => 34,
ConsoleColor.Magenta or ConsoleColor.DarkMagenta => 35,
ConsoleColor.Cyan or ConsoleColor.DarkCyan => 36,
ConsoleColor.White or ConsoleColor.DarkGray or ConsoleColor.Gray => 37,
_ => throw new ArgumentOutOfRangeException(nameof(color), color, null)
};
Console.Write($"\x1b[{colorCode}m");
}

public static void SetExtendedForegroundColor(int r, int g, int b)
{
Console.Write($"\x1b[38;2;{r};{g};{b}m");
}

public static void SetExtendedBackgroundColor(int r, int g, int b)
{
Console.Write($"\x1b[48;2;{r};{g};{b}m");
}

public static void SetExtendedForegroundColor(ConsoleColor color)
{
var (r, g, b) = GetExtendedColor(color);
SetExtendedForegroundColor(r, g, b);
}

public static void SetExtendedBackgroundColor(ConsoleColor color)
{
var (r, g, b) = GetExtendedColor(color);
SetExtendedBackgroundColor(r, g, b);
}

private static (int R, int G, int B) GetExtendedColor(ConsoleColor color)
{
return color switch
{
ConsoleColor.Black => (12, 12, 12),
ConsoleColor.DarkBlue => (0, 55, 218),
ConsoleColor.DarkGreen => (19, 161, 14),
ConsoleColor.DarkCyan => (58, 150, 221),
ConsoleColor.DarkRed => (197, 15, 31),
ConsoleColor.DarkMagenta => (136, 23, 152),
ConsoleColor.DarkYellow => (193, 156, 0),
ConsoleColor.Gray => (204, 204, 204),
ConsoleColor.DarkGray => (118, 118, 118),
ConsoleColor.Blue => (59, 120, 255),
ConsoleColor.Green => (22, 198, 12),
ConsoleColor.Cyan => (97, 214, 214),
ConsoleColor.Red => (231, 72, 86),
ConsoleColor.Magenta => (180, 0, 158),
ConsoleColor.Yellow => (249, 241, 165),
ConsoleColor.White => (242, 242, 242),
_ => throw new Exception($"Unexpected color: {color}"),
};
}

public static void ResetScrollMargins()
{
Console.Write($"\x1b[r");
}

public static void SetScrollMargins(int top, int bottom)
{
Console.Write($"\x1b[{top};{bottom}r");
}

public static void ScrollUp(int lines)
{
Console.Write($"\x1b[{lines}S");
}

public static void ScrollDown(int lines)
{
Console.Write($"\x1b[{lines}T");
}

public static void EraseRestOfCurrentLine()
{
Console.Write($"\x1b[K");
}

public static void ResetColors()
{
Console.Write("\x1b[0m");
}
}
41 changes: 41 additions & 0 deletions src/Husky/Win32Console.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Runtime.InteropServices;

namespace Husky;

internal static class Win32Console
{
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]
public static extern uint GetLastError();

#pragma warning disable IDE1006 // Naming Styles
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

// private const int STD_INPUT_HANDLE = -10;
// private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
// private const uint ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
// private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
// private const uint ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002;

#pragma warning restore IDE1006 // Naming Styles

public static void Initialize()
{
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (GetConsoleMode(iStdOut, out var outConsoleMode))
{
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(iStdOut, outConsoleMode);
}
}
}
1 change: 1 addition & 0 deletions src/Husky/templates/husky.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if [ -z "$husky_skip_init" ]; then
fi

export readonly husky_skip_init=1
export vt100=1
sh -e "$0" "$@"
exitCode="$?"

Expand Down

0 comments on commit 498bc9c

Please sign in to comment.