【问题标题】:Powershell Custom Prompt Adds a Zero at the EndPowershell 自定义提示在末尾添加一个零
【发布时间】:2021-08-05 08:16:46
【问题描述】:

我一直在尝试为 Powershell 创建我自己的受 Powerline 启发的自定义提示。

我在$PROFILE.CurrentUserAllHosts 文件中创建了Prompt 函数,如下所示:

using namespace System.IO
using namespace System.Collections

function Prompt() {

    class PromptBlock {
        [string]       $Contents
        [ConsoleColor] $Fore
        [ConsoleColor] $Back

        PromptBlock([string]$str, [ConsoleColor]$fg, [ConsoleColor] $bg) {
            $this.Contents = $str
            $this.Fore = $fg
            $this.Back = $bg
        }
    }

    $Characters = [PSCustomObject]@{
        ZWNJ = "$([char]0x200C)"
        NBSP = "$([char]0x00A0)"
    }

    $PLGlyphs = [PSCustomObject]@{
        Branch  = "$([char]0xE0A0)" #  Version control branch
        LN      = "$([char]0xE0A1)" #  LN (line) symbol
        Padlock = "$([char]0xE0A2)" #  Closed padlock
        RArrow  = "$([char]0xE0B0)" #  Rightwards black arrowhead
        WRArrow = "$([char]0xE0B1)" #  Rightwards arrowhead
        LArrow  = "$([char]0xE0B2)" #  Leftwards black arrowhead
        WLArrow = "$([char]0xE0B3)" #  Leftwards arrowhead
    }

    function Print([string]$str, [ConsoleColor]$fo, [ConsoleColor]$bg) {
        Write-Host $str -ForegroundColor $fo -BackgroundColor $bg -NoNewline
    }

    function Section([ConsoleColor]$from, [ConsoleColor]$to = [ConsoleColor]::Black) {
        Print "$($PLGlyphs.RArrow)" $from $to
    }

    # Prints the user name, white on blue
    $CurrentUserName = ((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username) -split '\\' )[1]
    
    # Current Path
    $CurrPath = $PWD.Path

    $Blocks = [ArrayList]::new()

    $Blocks.Add([PromptBlock]::new($CurrentUserName, [ConsoleColor]::White, [ConsoleColor]::DarkBlue))
    $Blocks.Add([PromptBlock]::new($CurrPath, [ConsoleColor]::Black, [ConsoleColor]::Green))

    for ($i = 0; $i -lt $Blocks.Count; $i++) {
        $CurrentBlock = $Blocks[$i]
        $NextBlock = $Blocks[$i + 1]
        $NextBlockColor = $NextBlock.Back
        if (-not $NextBlockColor) {
             $NextBlockColor = [ConsoleColor]::Black
        }

        Print " $($CurrentBlock.Contents) " $CurrentBlock.Fore $CurrentBlock.Back
        Section $CurrentBlock.Back $NextBlockColor
    }

    return "$($Characters.NBSP)"
}

但此提示的结果在 Windows 终端中显示如下,并在“老式”Powershell 控制台中通过扩展显示。

我完全不知道零是从哪里来的,以及为什么代码没有到达return 语句。

【问题讨论】:

  • 无法复制。我的 PowerShell (5.1) 中没有显示零,只有默认字体中缺少特殊字形。

标签: powershell prompt


【解决方案1】:

零来自第一个$Blocks.Add(...) 语句。

抑制来自ArrayList.Add() 的输出,它会起作用:

[void]$Blocks.Add([PromptBlock]::new($CurrentUserName, [ConsoleColor]::White, [ConsoleColor]::DarkBlue))
[void]$Blocks.Add([PromptBlock]::new($CurrPath, [ConsoleColor]::Black, [ConsoleColor]::Green))

或者使用调用Add()时不返回任何内容的集合类型:

$Blocks = [System.Collections.Generic.List[PromptBlock]]::new()

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2014-06-26
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多