将日期时间添加到 posh-hg 提示
Adding date time to posh-hg prompt
我已如下更改我的 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["
由于某种原因,日期根本没有更新。我怀疑这可能与 posh-hg 构建提示的方式有关,但这是一个有点疯狂的猜测。
目前,无法使用 BeforeText
.
执行您想执行的操作
如果你look at the code, BeforeText
is passed into Write-Prompt
as a string (L29):
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
为了使其工作,必须更改 Write-Prompt
函数代码:
function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) {
$parameters = @{
'NoNewLine' = $True;
'ForegroundColor' = $ForegroundColor;
}
if ($Object -is [ScriptBlock]) {
$parameters.Add('Object', (&$Object))
} else {
$parameters.Add('Object', $Object)
}
if ($BackgroundColor -ge 0) {
$parameters.Add('BackgroundColor', $BackgroundColor)
}
Write-Host @parameters
}
我确定可能还需要进行一些更改才能使其完美,但无论如何...
然后,您可以这样设置 $global:HgPromptSettings.BeforeText
:
$global:HgPromptSettings.BeforeText = {" [$(Get-Date -format g)] `n["}
我尝试对 Posh-Git
执行相同的操作,但得到了相同的结果:时间戳在会话开始时评估一次,之后被修复。
所以我在互联网上搜索了一下,发现了这个关于 posh-hg 的帖子,并得出结论,这也适用于 posh-git。
当晚晚些时候,我通读了 post-git 个问题,发现 this thread 关于在提示中添加 'side effects'。考虑到我的新失败,我添加了一条评论,告诉我它可能因为 'evaluated only once'-thing 而不起作用。
但是瞧瞧!原来我的 powershell 知识和我担心的一样有限:
@rkeithhill: Be careful to use single quotes when supplying the string. If you use double quotes, any variables / subexpressions i.e. $() will only get eval'd once.
所以现在我的个人资料中有 $global:GitPromptSettings.DefaultPromptPrefix = '$(Get-Date -format HH:mm:ss) '
,并在我的提示前获取时间戳。当然,我不知道这是否适用于 posh-hg,但至少你可以试试。
我已如下更改我的 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["
由于某种原因,日期根本没有更新。我怀疑这可能与 posh-hg 构建提示的方式有关,但这是一个有点疯狂的猜测。
目前,无法使用 BeforeText
.
如果你look at the code, BeforeText
is passed into Write-Prompt
as a string (L29):
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
为了使其工作,必须更改 Write-Prompt
函数代码:
function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) {
$parameters = @{
'NoNewLine' = $True;
'ForegroundColor' = $ForegroundColor;
}
if ($Object -is [ScriptBlock]) {
$parameters.Add('Object', (&$Object))
} else {
$parameters.Add('Object', $Object)
}
if ($BackgroundColor -ge 0) {
$parameters.Add('BackgroundColor', $BackgroundColor)
}
Write-Host @parameters
}
我确定可能还需要进行一些更改才能使其完美,但无论如何...
然后,您可以这样设置 $global:HgPromptSettings.BeforeText
:
$global:HgPromptSettings.BeforeText = {" [$(Get-Date -format g)] `n["}
我尝试对 Posh-Git
执行相同的操作,但得到了相同的结果:时间戳在会话开始时评估一次,之后被修复。
所以我在互联网上搜索了一下,发现了这个关于 posh-hg 的帖子,并得出结论,这也适用于 posh-git。
当晚晚些时候,我通读了 post-git 个问题,发现 this thread 关于在提示中添加 'side effects'。考虑到我的新失败,我添加了一条评论,告诉我它可能因为 'evaluated only once'-thing 而不起作用。
但是瞧瞧!原来我的 powershell 知识和我担心的一样有限:
@rkeithhill: Be careful to use single quotes when supplying the string. If you use double quotes, any variables / subexpressions i.e. $() will only get eval'd once.
所以现在我的个人资料中有 $global:GitPromptSettings.DefaultPromptPrefix = '$(Get-Date -format HH:mm:ss) '
,并在我的提示前获取时间戳。当然,我不知道这是否适用于 posh-hg,但至少你可以试试。