Last active
January 20, 2024 08:14
-
-
Save krymtkts/52de8e9d4b864db7919d892795486978 to your computer and use it in GitHub Desktop.
Test-ConsoleWindowWithoutBufferCleaning
This file contains hidden or 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
# test script for interactive console window. | |
# currently cannot prevent mouse scrolling. | |
function Test-ConsoleWindowWithoutBufferCleaning { | |
# backup cursor x position. | |
$x = [Console]::CursorLeft | |
# add lines to the end of the screen for scrolling using the PSReadLine method. | |
# https://github.com/PowerShell/PSReadLine/blob/e57f7d691d8df8c1121fddf47084f96aea74a688/PSReadLine/DisplayBlockBase.cs#L17-L24 | |
$h = [Console]::WindowHeight | |
0..($h - 1) | ForEach-Object { [Console]::Write("`n") } | |
# write contents. | |
$yy = [COnsole]::CursorTop | |
$yy .. 0 | ForEach-Object { | |
[Console]::SetCursorPosition($x, $_) | |
[Console]::Write("Line: $_") | |
} | |
# some interaction. | |
$arr = @() | |
while ($true) { | |
$k = [Console]::ReadKey() | |
$arr = @($k) + $arr | |
if ($k.Key -eq 'Enter') { | |
break | |
} | |
else { | |
$i = 0 | |
(($arr | Out-String ) -split "`n" ) | Select-Object -First $h | ForEach-Object { | |
[Console]::SetCursorPosition(0, $i) | |
[Console]::Write($_) | |
$i += 1 | |
} | |
} | |
} | |
# clear contests. | |
$w = [Console]::WindowWidth | |
0..($h - 1) | ForEach-Object { | |
[Console]::SetCursorPosition(0, $_) | |
[Console]::Write(' ' * $w) | |
} | |
# restore cursor position. | |
[Console]::SetCursorPosition($x, 0) | |
} | |
Test-ConsoleWindowWithoutBufferCleaning |
This file contains hidden or 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
open System | |
[<TailCall>] | |
let rec readAndDisplay h w arr = | |
let k = Console.ReadKey(true) | |
let arr = k :: arr | |
if k.Key = ConsoleKey.Enter then | |
() | |
else | |
let take = | |
match List.length arr < h with | |
| true -> List.length arr | |
| _ -> h | |
arr | |
|> List.take take | |
|> List.map (fun k -> $"[{k.Key} - {k.Modifiers}]") | |
|> List.iteri (fun i s -> | |
Console.SetCursorPosition(0, i) | |
let s = $"{i} {s}" | |
Console.Write(s + String.replicate (w - String.length s) " ")) | |
readAndDisplay h w arr | |
// test script for interactive console window. | |
// currently cannot prevent mouse scrolling. | |
let testConsoleWindowWithoutBufferCleaning () = | |
// backup cursor x position. | |
let x = Console.CursorLeft | |
// add lines to the end of the screen for scrolling using the PSReadLine method. | |
let h = Console.WindowHeight | |
let w = Console.WindowWidth | |
String.replicate (h - 1) "\n" |> Console.Write | |
// write contents. | |
let yy = Console.CursorTop | |
Console.SetCursorPosition(0, 0) | |
[ 0..yy ] | |
|> List.map (fun i -> | |
let s = $"Line: %d{i}" | |
s + String.replicate (w - s.Length) " ") | |
|> String.concat "\n" | |
|> Console.Write | |
// read and display. | |
readAndDisplay h w [] | |
// clear contests. | |
[ 0 .. (h - 1) ] | |
|> List.iter (fun i -> | |
Console.SetCursorPosition(0, i) | |
Console.Write(String.replicate w " ")) | |
// restore cursor position. | |
Console.SetCursorPosition(x, 0) | |
testConsoleWindowWithoutBufferCleaning () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment