虽然没有官方机制来响应每个击键,但您可以通过Set-PSReadLineKeyHandler cmdlet 为每个可打印字符和选择的几个控制字符设置一个键处理程序来实现自己的机制.
在键处理程序中,您可以在输入行下方显示有关输入缓冲区当前状态的信息。
tl;博士:
注意:
-
它是设置密钥处理程序的前 256 个 Unicode 代码点中的可打印字符,这实际上是构成 ISO-8859-1 encoding 的字符集,它本身是 Windows-1252 的子集[ 1]。因此,所有 ASCII 范围的字母加上一些重音字母都被覆盖,但西里尔字母不会,例如。但是,您可以根据需要定制列表。
-
出于说明目的,下面的代码不会尝试可视化 AST,而是以 System.ConsoleKeyInfo 实例的格式化表示形式打印有关刚刚按下的键的信息。
-
在下面的代码中找到以$metaInfo = 开头的行以自定义要显示的内容。
-
获取表示缓冲区内容的AST(抽象语法树)的命令是:
$ast = $tokens = $errors = $cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $ast, [ref] $tokens, [ref] $errors, [ref] $cursor)
-
该代码在常规 Windows 控制台窗口中效果最佳 (conhost.exe); 限制:
# The printable characters to respond to.
$printableChars = [char[]] (0x20..0x7e + 0xa0..0xff)
# The control characters to respond to.
$controlChars = 'Enter', 'Escape', 'Backspace', 'Delete'
# Set up the key handler for all specified characters.
$printableChars + $controlChars | ForEach-Object {
Set-PSReadLineKeyHandler $_ {
param($key, $arg)
$line = $cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $null, [ref] $cursor)
# Handle the key at hand.
switch ($key.Key) {
'Backspace' { [Microsoft.PowerShell.PSConsoleReadLine]::BackwardDeleteChar(); break }
'Delete' { try { [Microsoft.PowerShell.PSConsoleReadLine]::Delete($cursor, 1) } catch { }; break } # ignore error with empty buffer
'Escape' {
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $line, [ref] $null)
[Microsoft.PowerShell.PSConsoleReadLine]::Delete($0, $line.Length)
break
}
'Enter' {
# Clear any previous meta-information output, so that it doesn't linger and get mixed with command output.
try {
# !! On conhost.exe (regular console) windows on Windows, [Console]::CursorTop and [Console]::WindowTop are *relative to the scrollback buffer*.
# !! In Windows Terminal and on Unix, [Console]::WindowTop is always 0, and [Console]::CursorTop is relative to the screen height - even in the presence of a scrollback buffer.
Write-Host -NoNewLine (, (' ' * [Console]::WindowWidth) * ([Console]::WindowTop + [Console]::WindowHeight - [Console]::CursorTop - 1) -join "`n")
}
catch { Write-Warning "`nClearing the screen below the current line failed: $_" } # This shouldn't happen.
# !! Workaround for a display bug: If the cursor isn't at the very end of the line, everything to the
# !! right is inexplicably *erased* on submission, even though the submission itself still works fine.
# !! We detect that case and simply fill the entire buffer again, which leaves it drawn correctly on submission.
# !! (Note that [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($line.Length) does *not* work.)
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $line, [ref] $cursor)
if ($cursor -ne $line.length) {
[Microsoft.PowerShell.PSConsoleReadLine]::Delete(0, $line.Length)
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($line)
}
# Submit the command.
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
return # We're done.
}
Default { [Microsoft.PowerShell.PSConsoleReadLine]::Insert($key.KeyChar) }
}
# Get the updated buffer content and cursor position.
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref] $cursor)
# Note: To get the *AST* (too), use the following:
# $ast = $tokens = $errors = $cursor = $null
# [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $ast, [ref] $tokens, [ref] $errors, [ref] $cursor)
# Determine the meta-informaton to print:
$metaInfo = $key | Out-String
if ($env:OS -ne 'Windows_NT' -or $env:WT_SESSION) {
# Workaround for all terminals except conhost.exe
# See comments at the top of the answer.
if ([Console]::CursorTop + $metaInfo.Count -gt [Console]::WindowTop + [Console]::WindowHeight) {
[Microsoft.PowerShell.PSConsoleReadLine]::ClearScreen()
}
}
# Print the desired information below the line being edited.
# Note:
# * The .PadRight() calls ensure that all lines are fully filled (padded with spaces),
# in order to erase potential remnants from previously displayed information.
# * This is NOT sufficient to deal with *varying line counts* being displayed, however.
Write-Host # blank line
Write-Host -NoNewLine -ForegroundColor Yellow ($metaInfo -split '\r?\n' | ForEach-Object PadRight ([Console]::WindowWidth-1), ' ')
# Set the new cursor position.
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor)
}
}
一个示例屏幕截图正在运行的关键处理程序:
请注意正在编辑的命令行下方的信息如何反映有关最近按下的键(大写 D)的信息。
[1] ISO-8859-1 是关于 可打印字符 的 Windows-1252 的子集,因为它的 0x80-0x9f 范围被所谓的 C1 控制字符占据,而 Windows-1252 包含此范围内的可打印字符(代码点 0x81、0x8d、0x8f、0x90 和 0x9d 除外,它们未定义)。