15

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

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

4

6 回答 6

11

This is modified version of jaykul's prompt. The benefit is that

-there is a current history id, so you can invoke previous items from history very easily (you know the id) -it's a little reminder - I add my tasks to the prompt so I don't forget them (see the 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

于 2009-08-27T06:36:09.667 回答
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')
}
于 2009-08-27T13:31:41.120 回答
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 '>'
    ' '
}
于 2009-08-27T01:46:15.003 回答
4

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

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

于 2009-08-29T16:54:29.720 回答
1

这是我的。每个命令都有历史 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 并为每个嵌套级别添加了一个箭头。

安迪

于 2009-09-15T13:48:39.677 回答
0

我倾向于重新输入

function prompt { "PS> " }

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

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

于 2009-08-27T05:36:40.660 回答