You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
7.5 KiB
201 lines
7.5 KiB
# from github.com/dahlbyk/posh-git |
|
# ------------------------------------------------------------------------------------ |
|
#Copyright (c) 2010-2018 Keith Dahlby, Keith Hill, and contributors |
|
|
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
# ------------------------------------------------------------------------------------ |
|
|
|
# Always skip setting the console mode on non-Windows platforms. |
|
if (($PSVersionTable.PSVersion.Major -ge 6) -and !$IsWindows) { |
|
function Set-ConsoleMode { |
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
|
param() |
|
} |
|
|
|
return |
|
} |
|
|
|
$consoleModeSource = @" |
|
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 Set-ConsoleMode |
|
{ |
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
|
param( |
|
[Parameter(ParameterSetName = "ANSI")] |
|
[switch] |
|
$ANSI, |
|
|
|
[Parameter(ParameterSetName = "Mode")] |
|
[uint32] |
|
$Mode, |
|
|
|
[switch] |
|
$StandardInput |
|
) |
|
|
|
begin { |
|
# Module import is speeded up by deferring the Add-Type until the first time this function is called. |
|
# Add the NativeConsoleMethods type but only once per session. |
|
if (!('NativeConsoleMethods' -as [System.Type])) { |
|
Add-Type $consoleModeSource |
|
} |
|
} |
|
|
|
end { |
|
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 Get-ConsoleMode |
|
{ |
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
|
param( |
|
[switch] |
|
$StandardInput |
|
) |
|
|
|
begin { |
|
# Module import is speeded up by deferring the Add-Type until the first time this function is called. |
|
# Add the NativeConsoleMethods type but only once per session. |
|
if (!('NativeConsoleMethods' -as [System.Type])) { |
|
Add-Type $consoleModeSource |
|
} |
|
} |
|
|
|
end { |
|
$mode = [NativeConsoleMethods]::GetConsoleMode($StandardInput) |
|
write-Output $mode |
|
return |
|
} |
|
} |
|
function psmain { |
|
param ( |
|
[validateSet('enableRaw', 'disableRaw')] |
|
[string]$Action |
|
) |
|
$inputflags = Get-ConsoleMode -StandardInput |
|
$resultflags = $inputflags #default |
|
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
|
#cooked mode |
|
$initialstate = "cooked" |
|
if ($action -eq "enableraw") { |
|
#disable cooked flags |
|
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT) |
|
$adjustedflags = $inputflags -band ($disable) |
|
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags) |
|
} |
|
} else { |
|
#raw mode |
|
$initialstate = "raw" |
|
if ($action -eq "disableraw") { |
|
#set cooked flags |
|
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT |
|
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags) |
|
} |
|
} |
|
#return in format that can act as a tcl dict |
|
write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags" |
|
} |
|
psmain @args |
|
|
|
#write-host (Get-ConsoleMode) |
|
#Set-ConsoleMode -ANSI -StandardInput |
|
#write-host (Get-ConsoleMode) |
|
|
|
#test toggle |
|
#if ((($inputflags -band [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE)) -eq [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) { |
|
# #quick edit is on |
|
# write-host "quick edit is on" |
|
# $adjustedflags = $inputflags -band (-bnot [uint32][ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) |
|
# $resultflags = [NativeConsoleMethods]::SetConsoleMode($true, $adjustedflags) |
|
## |
|
#} else { |
|
# #quick edit is off |
|
# write-host "quick edit is off" |
|
# $resultflags = [NativeConsoleMethods]::SetConsoleMode($true, $inputflags -bor [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) |
|
#} |
|
|
|
|
|
#todo - parameters so it doesn't act as a toggle |
|
#we want to be able to explicitly set raw vs cooked |
|
|
|
#multi |
|
|
|
|
|
|
|
|