【发布时间】:2010-11-23 05:35:18
【问题描述】:
我正在寻找自定义 Powershell prompt 函数实现的不同示例。如果您有自己的自定义实现,请发布脚本。现有资源的链接也很好。
发布提示实际外观的屏幕截图(预览)可获得奖励积分。
【问题讨论】:
标签: powershell prompt
我正在寻找自定义 Powershell prompt 函数实现的不同示例。如果您有自己的自定义实现,请发布脚本。现有资源的链接也很好。
发布提示实际外观的屏幕截图(预览)可获得奖励积分。
【问题讨论】:
标签: powershell prompt
这是 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 })
}
}
【讨论】:
这里是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')
}
【讨论】:
这是我的提示功能
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)? :-)
我经常使用 posh 作为计算,所以我设置了 $ans 变量。 https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=386493
PS > 100
100
PS > $ans * 9
900
PS > $ans*$ans
810000
【讨论】:
这是我的。每个命令都有历史 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 并为每个嵌套级别添加了一个箭头。
安迪
【讨论】:
我倾向于重新输入
function prompt { "PS> " }
每次我准备示例时,我都可以复制/粘贴给某人,尤其是当我走在只会分散注意力的繁琐长路径中时。
我仍然计划编写一个体面的提示函数,通过使用当前目录(没有通向那里的路径)或(如果是数字)下一个更高级别,向我显示驱动器和有用的位置近似值也。但这可能非常特定于我自己的文件系统。而且我从来没有被默认提示打扰到真正做到这一点:-)
【讨论】: