添加日期时间到 posh-hg 提示符

cod*_*dog 0 powershell mercurial

我已按如下方式更改了我的 Powershell 配置文件,以在 posh-hg 提示符中包含日期时间:

if(!(Test-Path function:\TabExpansion)) { New-Item function:\Global:TabExpansion -value '' | Out-Null }

# Load posh-hg example profile
. 'C:\ProgramData\chocolatey\lib\Posh-HG\JeremySkinner-posh-hg-e273b0d\profile.example.ps1'

Rename-Item Function:\Prompt PoshHGPrompt -Force
function Prompt() {if(Test-Path Function:\PrePoshHGPrompt){++$global:poshScope; New-Item function:\script:Write-host -value "param([object] `$object, `$backgroundColor, `$foregroundColor, [switch] `$nonewline) " -Force | Out-Null;$private:p = PrePoshHGPrompt; if(--$global:poshScope -eq 0) {Remove-Item function:\Write-Host -Force}}PoshHGPrompt}

$global:HgPromptSettings.BeforeText = " [$(Get-Date -format g)] `n["
Run Code Online (Sandbox Code Playgroud)

由于某种原因,日期根本没有更新。我怀疑这可能与 posh-hg 如何构建提示有关,但这有点疯狂的猜测。

Veg*_*gar 6

我试图做同样的事情Posh-Git,并得到相同的结果:时间戳在会话开始时评估一次,然后修复。

所以我在互联网上搜索,找到了这个关于 posh-hg 的帖子,并得出结论,这对于 posh-git 来说也是如此。

当天晚上晚些时候,我在阅读 post-git issues 时,发现了这个线程有关在提示中添加“副作用”的考虑到我刚刚失败,我添加了一条评论,告诉它可能因为“仅评估一次”而行不通。

但你瞧!事实证明我的 powershell 知识和我担心的一样有限:

@rkeithhill:提供字符串时请小心使用单引号。如果使用双引号,任何变量/子表达式即 $() 只会被评估一次。

所以现在我已经$global:GitPromptSettings.DefaultPromptPrefix = '$(Get-Date -format HH:mm:ss) '在我的个人资料中,并在我的提示符前面获取时间戳。当然,我不知道这是否适用于 posh-hg,但至少你可以尝试一下。