From 56d77c8b8f66c2d45e9a460e24ef49315ccf0caa Mon Sep 17 00:00:00 2001 From: Julian Noble Date: Fri, 29 Aug 2025 02:21:11 +1000 Subject: [PATCH] getpunk windows SetConsoleMode take2 --- getpunk.cmd | 56 +++++++++++++++++++++----------------- src/scriptapps/getpunk.ps1 | 55 ++++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 50 deletions(-) diff --git a/getpunk.cmd b/getpunk.cmd index 9a5b560a..8a00f706 100644 --- a/getpunk.cmd +++ b/getpunk.cmd @@ -1029,48 +1029,54 @@ git branch --set-upstream-to=origin/master master Set-Location $launchdir #restore original CWD +# Define the necessary Win32 API functions and constants Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; -public class NativeMethods +public class WinAPI { - [DllImport("kernel32.dll", SetLastError=true)] - public static extern IntPtr GetStdHandle(int nStdHandle); - - [DllImport("kernel32.dll", SetLastError=true)] - public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode); + // Console Input/Output Handles + public const int STD_OUTPUT_HANDLE = -11; - [DllImport("kernel32.dll", SetLastError=true)] - public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode); + // Console Modes + public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + public const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008; - // Constants for standard handles - public const int STD_INPUT_HANDLE = -10; - public const int STD_OUTPUT_HANDLE = -11; - public const int STD_ERROR_HANDLE = -12; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr GetStdHandle(int nStdHandle); - // Console Input Modes - public const int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); - // Console Output Modes - public const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); } "@ -# Get the output handle -$hConsoleOutput = [NativeMethods]::GetStdHandle([NativeMethods]::STD_OUTPUT_HANDLE) +# Get the handle to the console output buffer +$stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE) -# Get the current output mode -$currentMode = 0 -[NativeMethods]::GetConsoleMode($hConsoleOutput, [ref]$currentMode) | Out-Null +# Get the current console mode +[uint]$currentMode = 0 +if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { + Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)" + return +} -# Enable Virtual Terminal Processing -$newMode = $currentMode -bor [NativeMethods]::ENABLE_VIRTUAL_TERMINAL_PROCESSING +# Enable virtual terminal processing +$newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING # Set the new console mode -[NativeMethods]::SetConsoleMode($hConsoleOutput, $newMode) | Out-Null +if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { + Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)" + return +} -write-host "`e[92m getpunk done `e[m" +Write-Host "Virtual terminal processing enabled successfully." + +write-host "`e[92m getpunk done `e[m" + # # diff --git a/src/scriptapps/getpunk.ps1 b/src/scriptapps/getpunk.ps1 index 3a19cbf0..53ec857d 100644 --- a/src/scriptapps/getpunk.ps1 +++ b/src/scriptapps/getpunk.ps1 @@ -120,45 +120,50 @@ git branch --set-upstream-to=origin/master master Set-Location $launchdir #restore original CWD +# Define the necessary Win32 API functions and constants Add-Type -TypeDefinition @" using System; using System.Runtime.InteropServices; -public class NativeMethods +public class WinAPI { - [DllImport("kernel32.dll", SetLastError=true)] - public static extern IntPtr GetStdHandle(int nStdHandle); - - [DllImport("kernel32.dll", SetLastError=true)] - public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode); + // Console Input/Output Handles + public const int STD_OUTPUT_HANDLE = -11; - [DllImport("kernel32.dll", SetLastError=true)] - public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode); + // Console Modes + public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + public const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008; - // Constants for standard handles - public const int STD_INPUT_HANDLE = -10; - public const int STD_OUTPUT_HANDLE = -11; - public const int STD_ERROR_HANDLE = -12; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr GetStdHandle(int nStdHandle); - // Console Input Modes - public const int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); - // Console Output Modes - public const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); } "@ -# Get the output handle -$hConsoleOutput = [NativeMethods]::GetStdHandle([NativeMethods]::STD_OUTPUT_HANDLE) +# Get the handle to the console output buffer +$stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE) -# Get the current output mode -$currentMode = 0 -[NativeMethods]::GetConsoleMode($hConsoleOutput, [ref]$currentMode) | Out-Null +# Get the current console mode +[uint]$currentMode = 0 +if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { + Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)" + return +} -# Enable Virtual Terminal Processing -$newMode = $currentMode -bor [NativeMethods]::ENABLE_VIRTUAL_TERMINAL_PROCESSING +# Enable virtual terminal processing +$newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING # Set the new console mode -[NativeMethods]::SetConsoleMode($hConsoleOutput, $newMode) | Out-Null +if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { + Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)" + return +} + +Write-Host "Virtual terminal processing enabled successfully." -write-host "`e[92m getpunk done `e[m" \ No newline at end of file +write-host "`e[92m getpunk done `e[m"