-
-
Notifications
You must be signed in to change notification settings - Fork 811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ANSI color codes if SupportsVirtualTerminal #304
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Hack! https://gist.github.com/lzybkr/f2059cb2ee8d0c13c65ab933b75e998c | ||
|
||
Add-Type @" | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
public class NativeConsoleMethods | ||
{ | ||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
public static extern IntPtr GetStdHandle(int handleId); | ||
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); | ||
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); | ||
|
||
public static uint GetConsoleMode(bool input = false) | ||
{ | ||
var handle = GetStdHandle(input ? -10 : -11); | ||
uint mode; | ||
if (GetConsoleMode(handle, out mode)) | ||
{ | ||
return mode; | ||
} | ||
return 0xffffffff; | ||
} | ||
|
||
public static uint SetConsoleMode(bool input, uint mode) | ||
{ | ||
var handle = GetStdHandle(input ? -10 : -11); | ||
if (SetConsoleMode(handle, mode)) | ||
{ | ||
return GetConsoleMode(input); | ||
} | ||
return 0xffffffff; | ||
} | ||
} | ||
"@ | ||
|
||
[Flags()] | ||
enum ConsoleModeInputFlags | ||
{ | ||
ENABLE_PROCESSED_INPUT = 0x0001 | ||
ENABLE_LINE_INPUT = 0x0002 | ||
ENABLE_ECHO_INPUT = 0x0004 | ||
ENABLE_WINDOW_INPUT = 0x0008 | ||
ENABLE_MOUSE_INPUT = 0x0010 | ||
ENABLE_INSERT_MODE = 0x0020 | ||
ENABLE_QUICK_EDIT_MODE = 0x0040 | ||
ENABLE_EXTENDED_FLAGS = 0x0080 | ||
ENABLE_AUTO_POSITION = 0x0100 | ||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0200 | ||
} | ||
|
||
[Flags()] | ||
enum ConsoleModeOutputFlags | ||
{ | ||
ENABLE_PROCESSED_OUTPUT = 0x0001 | ||
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002 | ||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 | ||
} | ||
|
||
function Get-ConsoleMode | ||
{ | ||
[CmdletBinding()] | ||
param( | ||
[switch] | ||
$StandardInput | ||
) | ||
|
||
$mode = [NativeConsoleMethods]::GetConsoleMode($StandardInput) | ||
if ($StandardInput) | ||
{ | ||
[ConsoleModeInputFlags]$mode | ||
} | ||
else | ||
{ | ||
[ConsoleModeOutputFlags]$mode | ||
} | ||
} | ||
|
||
function Set-ConsoleMode | ||
{ | ||
param( | ||
[Parameter(ParameterSetName = "ANSI")] | ||
[switch] | ||
$ANSI, | ||
|
||
[Parameter(ParameterSetName = "Mode")] | ||
[uint32] | ||
$Mode, | ||
|
||
[switch] | ||
$StandardInput | ||
) | ||
|
||
if ($ANSI) | ||
{ | ||
$outputMode = [NativeConsoleMethods]::GetConsoleMode($false) | ||
$null = [NativeConsoleMethods]::SetConsoleMode($false, $outputMode -bor [ConsoleModeOutputFlags]::ENABLE_VIRTUAL_TERMINAL_PROCESSING) | ||
|
||
if ($StandardInput) | ||
{ | ||
$inputMode = [NativeConsoleMethods]::GetConsoleMode($true) | ||
$null = [NativeConsoleMethods]::SetConsoleMode($true, $inputMode -bor [ConsoleModeInputFlags]::ENABLE_VIRTUAL_TERMINAL_PROCESSING) | ||
} | ||
} | ||
else | ||
{ | ||
[NativeConsoleMethods]::SetConsoleMode($StandardInput, $Mode) | ||
} | ||
} | ||
|
||
function Reset-Colors | ||
{ | ||
$mode = [NativeConsoleMethods]::GetConsoleMode() | ||
Set-ConsoleMode -ANSI | ||
"$([char]0x1b)[0m" | ||
[NativeConsoleMethods]::SetConsoleMode($false, $mode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works! 🎉