Skip to content

Instantly share code, notes, and snippets.

@powercode
Created June 27, 2017 12:30
Show Gist options
  • Save powercode/c73575cab1cc11c5d9ed1d6574c9461f to your computer and use it in GitHub Desktop.
Save powercode/c73575cab1cc11c5d9ed1d6574c9461f to your computer and use it in GitHub Desktop.
Reset-Ansi terminal
Add-Type -MemberDefinition @"
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr handle, out int mode);
"@ -Namespace FB.Win32 -Name NativeConsoleMethods
function Reset-Ansi {
$Handle = [FB.Win32.NativeConsoleMethods]::GetStdHandle(-11) # stdout
$Mode = 0
$Result = [FB.Win32.NativeConsoleMethods]::GetConsoleMode($Handle, [ref]$Mode)
$Mode = $Mode -bor 4 # undocumented flag to enable ansi/vt100
$Result = [FB.Win32.NativeConsoleMethods]::SetConsoleMode($Handle, $Mode)
}
[Flags()]
enum ConsoleMode {
None = 0
EnableProcessedOutput = 1
EnableWrapAtEolOutput = 2
EnableVirtualTerminalProcessing = 4
DisableNewlineAutoReturn = 8
}
function Set-ConsoleMode{
param(
[Parameter(Mandatory)]
[ConsoleMode] $Mode
)
$Handle = [FB.Win32.NativeConsoleMethods]::GetStdHandle(-11) # stdout
[FB.Win32.NativeConsoleMethods]::SetConsoleMode($Handle, $Mode)
[void] [FB.Win32.NativeConsoleMethods]::GetConsoleMode($Handle, [ref]$Mode)
$mode
}
function Get-ConsoleMode{
[OutputType([ConsoleMode])]
param()
$Handle = [FB.Win32.NativeConsoleMethods]::GetStdHandle(-11) # stdout
$Mode = 0
[void] [FB.Win32.NativeConsoleMethods]::GetConsoleMode($Handle, [ref]$Mode)
[ConsoleMode]$Mode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment