自定义PowerShell提示符

15
我正在寻找不同的自定义Powershell 提示符函数实现示例。如果您有自己的自定义实现,请发布脚本。现有资源的链接也是不错的选择。
如果能够发布您的提示符实际外观的屏幕截图(预览),那就更好了。
6个回答

11

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

-有当前历史 ID,因此您可以非常轻松地调用先前的历史记录项(您知道ID) -这是一个小提示 - 我将我的任务添加到提示中,以便我不会忘记它们(请参阅sshot)

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

sshot


在提示符上放置任务面包屑的想法非常有趣。我可能会将其纳入我的提示符中。第一个Write-Host看起来像是打字错误。'Newline' 应该是 '-NoNewline',如下所示:Write-Host "$($toPrompt) >" -ForegroundColor Green -NoNewline } - totorocat
太好了!我已经添加了当前目录。 - Bart Calixto

11

这是我的

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')
}

我喜欢这个提示的格式,简短而简单。对于像我这样刚开始学习 PowerShell 的人来说,它也是自定义提示的绝佳起点 :D - Alex Vallejo

10

这是我的提示函数

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 '>'
    ' '
}

3
Write-Host -NoNewLine -Fore Red ('+' * (Get-Location -Stack).Count)?:-) - Joey
@Johannes,这会向提示符中添加一系列“+”号,表示我在推送堆栈中的深度。 - JaredPar
我知道,我刚刚删除了foreach,因为你实际上不需要每个单独的项目,而只需要知道项目数量。 - Joey
@Johannes,明白了。需要更多的咖啡。 - JaredPar

4

Get-LastLine是什么样子?我收到了一个错误:Get-LastLine:未将该项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 - Daniel Fisher lennybacon

1
这是我的代码。每个命令都有历史记录ID,这样我就可以轻松地识别命令的ID。我还使用窗口标题来给出当前工作目录,而不是在提示符本身中显示它。
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 并为每个嵌套级别添加一个箭头。
安迪

0

我倾向于重新输入

function prompt { "PS> " }

每次我为了给别人准备示例而复制/粘贴时,特别是当我处于繁琐的长路径中时,这将会分散我的注意力。
而且,我仍然计划编写一个体面的提示函数,该函数通过使用当前目录(不包括导致该目录的路径)或(如果它是数字)下一个更高级别的目录来向我显示驱动器和有用的位置近似值。但这可能非常特定于我自己的文件系统。我从未因默认提示而感到困扰到实际执行它:-)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接