在按下 ENTER 之前获取 powershell 当前行

xdh*_*ore 12 powershell powershell-core windows-terminal

我有一个想法,编写一个可视化工具,在您键入时显示 PowerShell 行的 AST。但是要做到这一点,第一步是在提交之前(在按下 ENTER 之前)获取当前行的文本,但我找不到 API 函数或钩子来执行此操作。一个存在吗?

我在新的 Windows 终端上使用 PowerShell Core 7.1.0。

预测来源

似乎 PSReadLine 的 PredictiveSource 选项可能可以用于此目的,前提是它可以在每个字母条目上调用,而不仅仅是在 TAB 上调用,但我在挖掘后找不到有关 3rd-party 插件的类型合同的任何信息通过文档和 C# 代码...

设置 PSReadLineKeyHandler

正如传说中的@mklement0 所建议的那样,也许Set-PSReadLineKeyHandler可以使用。它似乎是用于键绑定的,但我仍在思考如何将其用于此目的。

mkl*_*nt0 22

虽然没有响应每次击键的官方机制,但您可以通过Set-PSReadLineKeyHandlercmdlet为每个可打印字符和选择的几个控制字符设置一个键处理程序来实现自己的机制。在键处理程序中,您可以在输入行下方显示有关输入缓冲区当前状态的信息。

tl;博士:

  • 通过修改该$metaInfo = ...行来调整下面的代码,以确定要实时显示有关正在编辑的命令行的哪些信息,位于其下方。

  • 阅读下一节中的限制


笔记:

  • 密钥处理程序设置的是前 256 个 Unicode 代码点中的可打印字符,这实际上是构成ISO-8859-1 编码的字符集,它本身是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)
      
      Run Code Online (Sandbox Code Playgroud)
  • 该代码在常规 Windows 控制台窗口 ( conhost.exe) 中效果最佳;限制

    • 在 Windows 终端和 Unix 终端中,如果当前输入行太靠近窗口底部边缘而无法容纳显示自定义信息所需的行,则需要一种解决方法:清除屏幕,并在窗口的第一行。

      • 但是,回滚缓冲区的内容被完全保留,因此如果需要,您可以向上滚动以查看滚动到视图之外的屏幕内容。
    • 在键处理程序有效时通过模拟输入粘贴命令似乎会使PSReadLine模块对当前终端行是什么感到困惑,因此自定义信息的多个打印操作最终可能会堆叠在彼此的顶部,而不是在原地相互覆盖.

      • 这只能在 Windows 上避免 - 在常规控制台窗口和 Windows 终端中 - 使用Ctrl-V粘贴,在这种情况下,文本真正粘贴在命令行上,尽管不会触发键处理程序(然后您必须键入另一个(虚拟)字符根据粘贴的内容触发键处理程序)。
      • 相比之下,触发所描述问题的模拟打字是执行的:
        • 总是在 Unix 终端
        • 在 Windows 上右键单击粘贴
# 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)
  }
}
Run Code Online (Sandbox Code Playgroud)

正在运行的密钥处理程序的示例屏幕截图

关键处理程序在行动

请注意正在编辑的命令行下方的信息如何反映有关最近按下的键(大写 D)的信息。


[1] ISO-8859-1 是 Windows-1252 关于可打印字符的一个子集,因为它的0x80-0x9f范围被所谓的 C1 控制字符占据,而 Windows-1252 包含这个范围内的可打印字符(除了代码点0x810x8d0x8f0x900x9d,它们是不确定的)。