【问题标题】:Custom PowerShell prompts [closed]自定义 PowerShell 提示 [关闭]
【发布时间】:2010-11-23 05:35:18
【问题描述】:

我正在寻找自定义 Powershell prompt 函数实现的不同示例。如果您有自己的自定义实现,请发布脚本。现有资源的链接也很好。

发布提示实际外观的屏幕截图(预览)可获得奖励积分。

【问题讨论】:

    标签: powershell prompt


    【解决方案1】:

    这是 jaykul 提示的修改版本。好处是

    -有一个当前的历史 id,所以你可以很容易地从历史中调用以前的项目(你知道 id) - 这是一个小提醒 - 我将我的任务添加到提示中,这样我就不会忘记它们(见截图)

    function prompt {
      $err = !$?
      $origOfs = $ofs;
      $ofs = "|"
      $toPrompt = "$($global:__PromptVars)"
      $ofs = $origOfs;
      if ($toPrompt.Length -gt 0) { 
        Write-Host "$($toPrompt) >" -ForegroundColor Green -NoNewline }
    
      $host.UI.RawUI.WindowTitle = "PS1 > " + $(get-location)
    
      # store the current color, and change the color of the prompt text
      $script:fg = $Host.UI.RawUI.ForegroundColor
      # If there's an error, set the prompt foreground to "Red"
      if($err) { $Host.UI.RawUI.ForegroundColor = 'Red' }
      else { $Host.UI.RawUI.ForegroundColor = 'Yellow' }
    
      # Make sure that Windows and .Net know where we are at all times
      [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
    
      # Determine what nesting level we are at (if any)
      $Nesting = "$([char]0xB7)" * $NestedPromptLevel
    
      # Generate PUSHD(push-location) Stack level string
      $Stack = "+" * (Get-Location -Stack).count
    
      # Put the ID of the command in, so we can get/invoke-history easier
      # eg: "r 4" will re-run the command that has [4]: in the prompt
      $nextCommandId = (Get-History -count 1).Id + 1
      # Output prompt string
      # Notice: no angle brackets, makes it easy to paste my buffer to the web
      Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine
    
      # Set back the color
      $Host.UI.RawUI.ForegroundColor = $script:fg
    
      if ($toPrompt.Length -gt 0) { 
          $host.UI.RawUI.WindowTitle = "$($toPrompt) -- " + $host.UI.RawUI.WindowTitle
      }
      " "
    }
    function AddTo-Prompt($str) {
      if (!$global:__PromptVars) { $global:__PromptVars = @() }
      $global:__PromptVars += $str
    }
    function RemoveFrom-Prompt($str) {
      if ($global:__PromptVars) {
        $global:__PromptVars = @($global:__PromptVars | ? { $_ -notlike $str })
      }
    }
    

    【讨论】:

    • 将任务面包屑放在提示符上的想法非常有趣。我可以将其纳入我的提示中。 ... 看起来像第一个 Write-Host 上的错字。 'Newline' 应该是 '-NoNewline',如下所示:Write-Host "$($toPrompt) >" -ForegroundColor Green -NoNewline }
    • 可爱!我添加了当前目录。
    【解决方案2】:

    这里是mine

    function prompt {
       # our theme
       $cdelim = [ConsoleColor]::DarkCyan
       $chost = [ConsoleColor]::Green
       $cloc = [ConsoleColor]::Cyan
    
       write-host "$([char]0x0A7) " -n -f $cloc
       write-host ([net.dns]::GetHostName()) -n -f $chost
       write-host ' {' -n -f $cdelim
       write-host (shorten-path (pwd).Path) -n -f $cloc
       write-host '}' -n -f $cdelim
       return ' '
    }
    

    它使用这个辅助函数:

    function shorten-path([string] $path) {
       $loc = $path.Replace($HOME, '~')
       # remove prefix for UNC paths
       $loc = $loc -replace '^[^:]+::', ''
       # make path shorter like tabs in Vim,
       # handle paths starting with \\ and . correctly
       return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
    }
    

    【讨论】:

    • 我喜欢这个提示的格式,简短而简单。当您像我一样刚开始学习 PS 时,这也是自定义提示的一个很好的起点:D
    【解决方案3】:

    这是我的提示功能

    function prompt() {
        if ( Test-Wow64 ) {
            write-host -NoNewLine "Wow64 "
        }
        if ( Test-Admin ) { 
            write-host -NoNewLine -f red "Admin "
        }
        write-host -NoNewLine -ForegroundColor Green $(get-location)
        foreach ( $entry in (get-location -stack)) {
            write-host -NoNewLine -ForegroundColor Red '+';
        }
        write-host -NoNewLine -ForegroundColor Green '>'
        ' '
    }
    

    【讨论】:

    • Write-Host -NoNewLine -Fore Red ('+' * (Get-Location -Stack).Count)? :-)
    • @Johannes,在提示中添加了一系列 +,表示我在推送堆栈中的深度
    • 我知道,我只是消除了 foreach,因为您实际上不需要每个单独的项目,而只需要项目的数量。
    • @Johannes,啊,明白了。需要更多咖啡
    【解决方案4】:

    我经常使用 posh 作为计算,所以我设置了 $ans 变量。 https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=386493

    PS > 100
    100
    PS > $ans * 9
    900
    PS > $ans*$ans
    810000

    【讨论】:

    • Get-LastLine 是什么样的?我收到一个错误:Get-LastLine:“Get-LastLine”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。
    【解决方案5】:

    这是我的。每个命令都有历史 ID,因此我可以轻松识别命令的 ID。我还使用 windowtitle 给我当前的工作目录,而不是让它显示在提示本身中。

    106 >  cat function:\prompt
    
        $history = @(get-history)
        if($history.Count -gt 0)
    {
            $lastItem = $history[$history.Count - 1]
            $lastId = $lastItem.Id
        }
    
        $nextCommand = $lastId + 1
        $Host.ui.rawui.windowtitle = "PS " + $(get-location)
        $myPrompt = "$nextCommand > "
        if ($NestedPromptLevel -gt 0) {$arrows = ">"*$NestedPromptLevel; $myPrompt = "PS-nested $arrows"}
        Write-Host ($myPrompt) -nonewline
        return " "
    

    许多人忘记的一件事是在自定义提示中处理的是嵌套提示。请注意,我检查了 $nestedPromptLevel 并为每个嵌套级别添加了一个箭头。

    安迪

    【讨论】:

      【解决方案6】:

      我倾向于重新输入

      function prompt { "PS> " }
      

      每次我准备示例时,我都可以复制/粘贴给某人,尤其是当我走在只会分散注意力的繁琐长路径中时。

      我仍然计划编写一个体面的提示函数,通过使用当前目录(没有通向那里的路径)或(如果是数字)下一个更高级别,向我显示驱动器和有用的位置近似值也。但这可能非常特定于我自己的文件系统。而且我从来没有被默认提示打扰到真正做到这一点:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-14
        • 1970-01-01
        相关资源
        最近更新 更多