Skip to content

Commit

Permalink
Use cswin32 in ansi detector
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Oct 14, 2024
1 parent 33738ae commit 926c41e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 50 deletions.
85 changes: 35 additions & 50 deletions DepotDownloader/AnsiDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.Win32.SafeHandles;
using Windows.Win32;
using Windows.Win32.System.Console;

namespace Spectre.Console;

Expand Down Expand Up @@ -45,7 +48,7 @@ public static (bool SupportsAnsi, bool LegacyConsole) Detect(bool stdError, bool
return (true, false);
}

var supportsAnsi = Windows.SupportsAnsi(upgrade, stdError, out var legacyConsole);
var supportsAnsi = WindowsSupportsAnsi(upgrade, stdError, out var legacyConsole);
return (supportsAnsi, legacyConsole);
}

Expand All @@ -67,68 +70,50 @@ private static (bool SupportsAnsi, bool LegacyConsole) DetectFromTerm()
return (false, true);
}

private static class Windows
private static bool WindowsSupportsAnsi(bool upgrade, bool stdError, out bool isLegacy)
{
private const int STD_OUTPUT_HANDLE = -11;
private const int STD_ERROR_HANDLE = -12;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
isLegacy = false;

[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);
try
{
var @out = PInvoke.GetStdHandle(stdError ? STD_HANDLE.STD_ERROR_HANDLE :STD_HANDLE.STD_OUTPUT_HANDLE);
var safeHandle = new SafeFileHandle(@out, ownsHandle: false);

[DllImport("kernel32.dll")]
public static extern uint GetLastError();
if (!PInvoke.GetConsoleMode(safeHandle, out var mode))
{
// Could not get console mode, try TERM (set in cygwin, WSL-Shell).
var (ansiFromTerm, legacyFromTerm) = DetectFromTerm();

public static bool SupportsAnsi(bool upgrade, bool stdError, out bool isLegacy)
{
isLegacy = false;
isLegacy = ansiFromTerm ? legacyFromTerm : isLegacy;
return ansiFromTerm;
}

try
if ((mode & CONSOLE_MODE.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0||true)
{
var @out = GetStdHandle(stdError ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
if (!GetConsoleMode(@out, out var mode))
{
// Could not get console mode, try TERM (set in cygwin, WSL-Shell).
var (ansiFromTerm, legacyFromTerm) = DetectFromTerm();
isLegacy = true;

isLegacy = ansiFromTerm ? legacyFromTerm : isLegacy;
return ansiFromTerm;
if (!upgrade)
{
return false;
}

if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0)
// Try enable ANSI support.
mode |= CONSOLE_MODE.ENABLE_VIRTUAL_TERMINAL_PROCESSING | CONSOLE_MODE.DISABLE_NEWLINE_AUTO_RETURN;
if (!PInvoke.SetConsoleMode(@out, mode))
{
isLegacy = true;

if (!upgrade)
{
return false;
}

// Try enable ANSI support.
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(@out, mode))
{
// Enabling failed.
return false;
}

isLegacy = false;
// Enabling failed.
return false;
}

return true;
}
catch
{
// All we know here is that we don't support ANSI.
return false;
isLegacy = false;
}

return true;
}
catch
{
// All we know here is that we don't support ANSI.
return false;
}
}
}
3 changes: 3 additions & 0 deletions DepotDownloader/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
GetConsoleMode
GetConsoleProcessList
GetStdHandle
MessageBox
SetConsoleMode

0 comments on commit 926c41e

Please sign in to comment.