【问题标题】:Getting powershell current line before ENTER is pressed在按下 ENTER 之前获取 powershell 当前行
【发布时间】:2021-07-12 02:59:02
【问题描述】:

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

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

预测来源

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

设置-PSReadLineKeyHandler

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

【问题讨论】:

  • 在投票结束之前,请阅读元问题:meta.stackoverflow.com/q/407114/14768
  • @khelwood 你用 3 个音节发音“idea”?
  • @xdhmoore 我想每个人都这样做。
  • @khelwood:哎呀!专注于错误的词;让我再试一次并总结一下:无论是“辉煌”是 2 个音节还是“想法”是 3 个音节都不是直观的,但是那些提供音节(词汇和/或作为音标的一部分)的字典似乎同意就是这种情况;来自元字典搜索引擎的链接集合:onelook.com/?w=brilliant&ls=aonelook.com/?w=idea&ls=a 简而言之:我们有 5 个音节!

标签: powershell powershell-core windows-terminal


【解决方案1】:

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

tl;博士:

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

  • 阅读下一节了解限制


注意:

  • 它是设置密钥处理程序的前 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); 限制

    • 在 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)
  }
}

一个示例屏幕截图正在运行的关键处理程序:

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多