From 0f7ec7fe26932618663e0c5778d11c3d038816c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20R=C3=B8ssum?= <35430583+rasmusroessum@users.noreply.github.com> Date: Sun, 19 Mar 2023 02:43:28 +0100 Subject: [PATCH 1/2] Redirect standard input to null stream This is to handle key-presses. Normally they would end up in the console. (Tested on macOS) --- Write-Menu.ps1 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Write-Menu.ps1 b/Write-Menu.ps1 index 45b0f01..bde3b44 100644 --- a/Write-Menu.ps1 +++ b/Write-Menu.ps1 @@ -432,9 +432,20 @@ function Write-Menu { # Move cursor to first entry and beginning of line [System.Console]::CursorTop = $lineTop [System.Console]::Write("`r") - - # Get pressed key - $menuInput = [System.Console]::ReadKey($false) + + # Redirect standard input to null stream + $nullStream = [System.IO.Stream]::Null + $nullReader = [System.IO.StreamReader]::new($nullStream) + [System.Console]::SetIn($nullReader) + + # Hide input while detecting keypresses + try { + # Get pressed key + $menuInput = [System.Console]::ReadKey($true) + } finally { + # Restore standard input + [System.Console]::SetIn([System.IO.StreamReader]::new([System.IO.MemoryStream]::new())) + } # Define selected entry $entrySelected = $menuEntries[($pageEntryFirst + $lineSelected)] From 12f5c29777214a5bad1c21c552ae22103a949bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20R=C3=B8ssum?= <35430583+rasmusroessum@users.noreply.github.com> Date: Sun, 19 Mar 2023 02:47:24 +0100 Subject: [PATCH 2/2] Fixing no colors set If no colors is set, then there is no highlight. (Tested on macOS) --- Write-Menu.ps1 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Write-Menu.ps1 b/Write-Menu.ps1 index bde3b44..3250d64 100644 --- a/Write-Menu.ps1 +++ b/Write-Menu.ps1 @@ -165,11 +165,21 @@ function Write-Menu { Set-Color #> - function Set-Color ([switch]$Inverted) { + function Set-Color ([switch]$Inverted) { switch ($Inverted) { $true { - [System.Console]::ForegroundColor = $colorBackground - [System.Console]::BackgroundColor = $colorForeground + # If foregroundColor is not set it to black + [System.Console]::ForegroundColor = if([System.Console]::ForegroundColor -eq -1){ + 'Black' + }else{ + $colorBackground + } + # If backgroundColor is not set it to yellow + [System.Console]::BackgroundColor = if([System.Console]::BackgroundColor -eq -1){ + 'Yellow' + }else{ + $colorForeground + } } Default { [System.Console]::ForegroundColor = $colorForeground