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

[玩转系统] PowerShell 风格预测

作者:精品下载站 日期:2024-12-14 08:06:49 浏览:16 分类:玩电脑

PowerShell 风格预测


我多年来一直在 PowerShell 中使用 PSReadline 模块。当模块根据您的历史记录添加内联命令预测时,我特别喜欢它。您将开始输入命令,模块将搜索您保存的历史记录并建议内联完成。

[玩转系统] PowerShell 风格预测

在我的 PowerShell 配置文件中,我启用并配置了此功能。

Set-PSReadLineOption -PredictionSource History -colors @{
    InlinePrediction = "$([char]27)[4;92m"
}

最近,微软将PSReadline模块更新至2.2.2版本。您可以从 PowerShell 库安装它。如果您从未更新过该模块,则需要实际安装它。

Install-Module PSReadline -force

这是因为 PSReadline 附带了 PowerShell。安装更新版本后,您可以使用 Update-Module 使其保持最新。

新版本带来了替代的预测视图样式。您现在可以指定列表视图。

Set-PSReadLineOption -PredictionViewStyle ListView

[玩转系统] PowerShell 风格预测

向下箭头找到所需的选项,然后按 Enter。它可能看起来不多,但正是这样的小事情可以节省您一整天的时间并消除一些命令行摩擦。

切换样式

我发现有时我想要内联预测,有时我想要一个列表。但我不想不断运行 Set-PSReadlineOption。虽然有命令预测,但我想也不会太糟糕。相反,我编写了一个 PowerShell 函数,默认情况下会切换预测视图样式。

Function Set-PredictionView {
    [cmdletbinding(SupportsShouldProcess)]
    [alias("spv", 'sview')]
    [OutputType("none")]
    Param(
        [Parameter(Position = 0, HelpMessage = "Specify the prediction style. Toggle will switch to the unused style.")]
        [ValidateSet("List", "InLine", "Toggle")]
        [string]$View = "Toggle",
        [switch]$Passthru
    )

    Try {
        Switch ($View) {
            "List" { $style = "ListView" }
            "Inline" { $style = "InLineView" }
            "Toggle" {
                Switch ((Get-PSReadLineOption).PredictionViewStyle) {
                    "InLineview" {
                        $style = "ListView"
                    }
                    "ListView" {
                        $style = "InLineView"
                    }
                    Default {
                        #fail safe action. This should never happen.
                        Write-Warning "Could not determine a view style. Are you running PSReadline v2.2.2 or later?"
                    }
                } #nested switch
            } #toggle
        } #switch view

        if ($style -AND ($PSCmdlet.ShouldProcess($style, "Set prediction view style"))) {
            Set-PSReadLineOption -PredictionViewStyle $style
            if ($Passthru) {
                Get-PredictionView
            }
        }
    } #try
    Catch {
        Write-Warning "There was a problem. Could not determine a view style. Are you running PSReadline v2.2.2 or later? $($_.exception.message)."
    }
} #end function

此代码需要 PSReadline 版本 2.2.2 或更高版本。您可以使用该功能轻松设置视图样式。

[玩转系统] PowerShell 风格预测

我设置默认“样式”以在列表和内联之间切换。如果我使用列表,该函数会将样式切换为内联,反之亦然。或者我可以明确设置一种样式。

Set-predictionview List

获取视图设置

当然,如果我要设置一些东西,我应该有一个函数来获取一些东西。

Function Get-PredictionView {
    [cmdletbinding()]
    [OutputType('PSPredictionView')]
    [alias('gview')]
    Param()

    #define escape character based on PowerShell version
    if ($IsCoreCLR) {
        $e = '`e'
    }
    else {
        $e = '[char]0x1b)'
    }

    #get prediction related PSReadline options
    $options = Get-PSReadLineOption | Select-Object PredictionSource, PredictionViewStyle, ListPredictionColor,
    ListPredictionSelectedColor, InlinePredictionColor

    #create a new PSPredictionView object
    [PSCustomObject]@{
        PSTypeName        = "PSPredictionView"
        Source            = $options.PredictionSource
        Style             = $options.PredictionViewStyle
        ListColor         = "{0}$e{1}$([char]27)[0m" -f $options.ListPredictionColor, ($options.ListPredictionColor -replace [char]27, "")
        ListSelectedColor = "{0}$e{1}$([char]27)[0m" -f $options.ListPredictionSelectedColor, ($options.ListPredictionSelectedColor -replace [char]27, "")
        InlineColor       = "{0}$e{1}$([char]27)[0m" -f $options.InlinePredictionColor, ($options.InlinePredictionColor -replace [char]27, "")
    }

} #end function

运行 Set-PredictionView -passthru 时调用此函数。

[玩转系统] PowerShell 风格预测

这些函数有别名,这使得它们很容易从控制台使用。只需按几次键,我就可以在列表视图和内联视图之间切换。谈论减少摩擦!

概括

如果您不使用 PSReadline 功能,那么您就错过了。我知道存在潜在的安全风险,因为“历史记录”选项将命令存储在纯文本文件中,因此有些人选择不使用它。但如果您这样做,我希望您尝试一下这些命令并让我知道您的想法。

那天晚些时候…

我想微软一定也认为你可能想要在视图之间切换。我从来没有看过,但感谢 Twitter 上的朋友,您还可以按 F2 在内联和列表预测视图之间切换。我猜你并不真正需要我的 Set 函数,除非你将 F2 重新映射到其他东西。您应该运行 Get-PSReadlineKeyHandler 看看您还可能错过什么!

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

取消回复欢迎 发表评论:

关灯