当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用另一个 PowerShell 提示跳出框框思考

作者:精品下载站 日期:2024-12-14 07:54:22 浏览:15 分类:玩电脑

使用另一个 PowerShell 提示跳出框框思考


前几天,我分享了我的简单 PowerShell 提示功能,该功能在颜色编码框中显示了一些可能有用的信息。今天我有一个轻微的变化,其中也包含一些改进。在之前的版本中,让我感到困惑的事情之一就是提示本身。我喜欢在提示符下有尽可能多的空间来编写我的 PowerShell 表达式。显示日期和版本号占用了太多空间。所以我将日期和时间移到框中作为第二行。

调整

为了正确计算框的长度并填充文本行以使所有内容对齐,我最终对行长度进行排序以获得最长的行。

#get the length of the longest line in the box and uas that to calculate lengths and padding
    $longest = $text1.length, $text2.length | Sort-Object | Select-Object -last 1
    $len = $longest + 2

我还使用这个值用空格填充每行。

Write-Host $text2.PadRight($longest, ' ') -NoNewline -ForegroundColor yellow

代码

完整的功能在Github上。

BoxPrompt2.ps1:

#requires -version 5.1



This prompt should run cross platform, although Linux testing has been limited.

┌─────────────────────┐
│ [jeff] /home/jeff   │
│ 1/21/19 10:49:18 AM │
└─────────────────────┘
 v6.1.2 PS>

To use the prompt dot source the script file in your PowerShell profile script.

This may not display properly in the Visual Studio Code PowerShell integrated console.
#>

Function prompt {

    if ($env:userdomain -AND $env:username) {
        $me = "$($env:userdomain)$($env:username)"
    }
    elseif ($env:LOGNAME) {
        $me = $env:LOGNAME
    }
    else {
        #last resort
        $me = "PSUser"
    }

    #define lines of text to include in the box
    $text1 = "[$me] $($executionContext.SessionState.Path.CurrentLocation)"
    $text2 = ((Get-Date).ToString()).trim()

    if ($IsLinux) {
        if ($(id -g) -eq 0 ) {
            #running as SU
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    elseif ($isWindows -or $psEdition -eq 'desktop') {
        $IsAdmin = [System.Security.Principal.WindowsPrincipal]::new([System.Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole("Administrators")
        if ($IsAdmin) {
            $lineColor = "Red"
        }
        else {
            $lineColor = "Green"
        }
    }
    else {
        #for everything else not tested
        $lineColor = "Yellow"
    }

    #get the length of the longest line in the box and uas that to calculate lengths and padding
    $longest = $text1.length, $text2.length | Sort-Object | Select-Object -last 1
    $len = $longest + 2
    $top = "┌$(("─" * $len))┐"
    Write-Host $top -ForegroundColor $lineColor
    Write-Host "│ " -ForegroundColor $lineColor -NoNewline
    Write-Host $text1.PadRight($longest, ' ') -NoNewline
    Write-Host " │" -ForegroundColor $lineColor
    Write-Host "│ " -ForegroundColor $lineColor -NoNewline
    Write-Host $text2.PadRight($longest, ' ') -NoNewline -ForegroundColor yellow
    Write-Host " │" -ForegroundColor $lineColor
    $bottom = "└$(("─" * $len))┘"
    Write-Host $bottom -ForegroundColor $lineColor

    #parse out the PSVersionTable to most meaningful values
    $ver = $(([regex]"\d+\.\d+.\d+").match($psversiontable.psversion).value)

    #the prompt function needs to write something to the pipeline
    " v$($ver) PS$('>' * ($nestedPromptLevel + 1)) "

}

PowerShell 提示

以下是一些实际提示的示例。

[玩转系统] 使用另一个 PowerShell 提示跳出框框思考

[玩转系统] 使用另一个 PowerShell 提示跳出框框思考

[玩转系统] 使用另一个 PowerShell 提示跳出框框思考

如果您尝试并修改它,我希望您能让我知道您的想法。祝你度过愉快的一周。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯