I dislike the "Administor: " prefix on the powershell window title because it makes it harder to determine the subject of a window when it is minimized. I usually have 3-6 powershell windows open, so this is important for my workflow.
I already have a commit with a fix for this. It's six-line change to add the setting as well as move the $adminHeader calculation to the bottom of Write-GitStatus where the other window title logic is located.
You can do that with your own custom prompt function. Here is mine:
function prompt {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
$name = ($identity.Name -split '\\')[1]
$path = Convert-Path $executionContext.SessionState.Path.CurrentLocation
$prefix = "($env:PROCESSOR_ARCHITECTURE)"
if($principal.IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) { $prefix = "Admin: $prefix" }
$realLASTEXITCODE = $LASTEXITCODE
$prefix = "Git $prefix"
Write-Host ("$prefix[$Name]") -nonewline
Write-VcsStatus
("`n$('+' * (get-location -stack).count)") + "PS $($path)$('>' * ($nestedPromptLevel + 1)) "
$global:LASTEXITCODE = $realLASTEXITCODE
$host.ui.RawUI.WindowTitle = "$prefix[$Name] $($path)"
}
You can also disable posh-git from messing with your title bar completely. Put $GitPromptSettings.EnableWindowTitle = $false in your profile script.
@sdwheeler , @rkeithhill , understood. I like most of the default settings of the posh prompt, and would rather not have to maintain my own prompt code. I think a new setting would be low-friction and easy to discover.
It's a small change- here's the PR: https://github.com/dahlbyk/posh-git/pull/538
While pondering the proposed setting name (AdminTitlePrefixText), it occurred to me that we could model this after the DefaultPrompt* settings, i.e.
EnableWindowTitle = $true
WindowTitlePrefix = 'posh~git ~ '
WindowTitleAdminPrefix = 'Administrator: posh~git ~ '
To avoid a breaking change, the logic would roughly be:
EnableWindowTitle is astring`, use the existing logic (with hard-coded 'Administrator: ' prefix)EnableWindowTitle is $true, show WindowTitlePrefix or WindowTitleAdminPrefix as appropriate. Bonus points if those strings are expanded to make customization easier (e.g. 'Admin [$PID]: git ').Come to think of it, instead of restricting title updated to a Git context, we could just turn this into a generic dynamic title mechanism, like the prompt (since #520), e.g.
$global:GitPromptSettings = [pscustomobject]@{
...
EnableWindowTitle = 'posh~git ~' # stop supporting string in v1?
WindowTitlePrefix = ''
WindowTitleAdminPrefix = 'Administrator: '
WindowTitle = '$(Get-WindowTitle)'
}
function Get-WindowTitle {
$s = $Global:GitPromptSettings
$status = $Global:GitStatus
if (!$s -or !$status) { return 'PowerShell' }
$repoName = Split-Path -Leaf (Split-Path $status.GitDir)
$prefix = if ($s.EnableWindowTitle -is [string]) { $s.EnableWindowTitle } else { '' }
return "$prefix$repoName [$($status.Branch)]"
}
To customize the title you'd replace $GitPromptSettings.WindowTitle to call your own function.
Thoughts?
Yup, this is one reason I was hesitating a bit on this. Also, we could make the setting take an expandable string like we do for DefaultPromptPrefix/Suffix as well as a ScriptBlock for more significant modifications.
We could tweak this for 1.0. I've always thought it a bit weird that $GitPromptSettings.EnableWindowTitle is not just a bool but holds part of the string to be displayed. Seems like that should really just be a bool (enable/disable). Thene we use $GitPromptSettings.WindowTitle like you suggest and set it to a scriptblock like so:
$GitPromptSettings.WindowTitle = {
param($GitStatus)
"$(if ($GitPromptSettings.IsAdmin) {"Admin "})posh~git ~ $($GitStatus.Repo) [$($GitStatus.Branch)]"
}
A couple of things here. First, by using a scriptblock that takes the Git status object, we make it easy to inject the "current" status object. Or I guess the user could just use the global variable $GitStatus that is created/updated. Second, there is no Repo property on the Git status object. That could easily be added. Third, there is no IsAdmin property on $GitPromptSettings. We could add it there but perhaps it should be on the Git status object. Then the above is tweaked slightly to:
$GitPromptSettings.WindowTitle = {
param($GitStatus)
"$(if ($GitStatus.IsAdmin) {"Admin "})posh~git ~ $($GitStatus.Repo) [$($GitStatus.Branch)]"
}
BTW why is it posh~git in the title instead of posh-git? Also, ~ is commonly used to represent a user's home dir. Having it in the title bar (the second one) is a bit confusing IMO. Is there a better "separator" char or is one even needed? And finally, on Linux/macOS perhaps it should say root instead of Admin.
Would 1.0 support string _or_ ScriptBlock? Seems like we need to pick one. Apart from the global dependency (which is well established posh-git behavior), there doesn't really seem to be much of a difference? The ScriptBlock can essentially live inside '$(...)', no?
We also need to weigh customizability of the default title behavior vs letting/making folks replace the logic entirely.
The ~ was to avoid a bug in Console2: 1ed6d4d3c26059c5b0abbd33708dde2be70ad8f2.
We could make the property be of type [string], [scriptblock] or [psobject]. In the latter case, the user could supply a string or a scriptblock (that would return a string).
The one nice thing about a scriptblock is that you don't have to worry about early interpolation of variables. I've helped a couple of folks with their $GitPromptSettings.DefaultPromptPrefix/Suffix setting where they use a variable but inside a double-quoted string. In that case, PowerShell interpolates the variable before it assigns the string to the setting. You have to use single quotes to prevent this. There is no such problem when you use a scriptblock. You can use double quotes which is kind of what folks expect when they put variables inside a string in PowerShell.
RE customizability vs full replacement, that depends on how complex the default impl is. If it is simple enough (ideally a one-liner) then customization is still pretty easy. In this case:
$GitPromptSettings.WindowTitle = {"posh~git ~ $($GitStatus.Repo) [$($GitStatus.Branch)] $(if ($GitStatus.IsAdmin) {"admin"})"
Also FWIW I tend to like a lot of other types of info in my Window title: PSVersion, (x86/x64) and $PID - the latter is especially handy when I need to attach the debugger.
Thanks for the pointer to the Console2 bug.
OK, so my proposal:
RepoName to $GitStatus[ScriptBlock] for EnableWindowTitle in addition to current support for $false or a Git prefix string[ScriptBlock], run it on every prompt (instead of resetting to the previous title) with two parameters: $GitStatus and $IsAdmin. (When not in a Git context, we don't want to init a new $GitStatus just to indicate admin-ness.)@nebosite I feel like you stumbled on a bit more than you bargained for here. Let us know if you'd like to give this a go.
[ScriptBlock]s, for consistency. EnableWindowTitle to simply WindowTitle, to be run on every prompt unless it's $null, as described above.Sounds reasonable. I'll tackle the v1 changes tonight.
I'm probably too crunched for time to make these changes without screwing something up. :(
I'm probably too crunched for time to make these changes without screwing something up. :(
That's why we have code review. 馃榾
But seriously, thanks for suggesting the feature and proposing a fix - the project is better off for it. 馃
Haha! Of course!
I really love posh-git, so I will try to keep contributing in the future.
For v0.7.2 I've gone ahead and merged @nebosite's simple fix. If someone's bored, cares deeply about customizing their window title, and can't upgrade to posh-git v1, they're welcome to give the proposal in https://github.com/dahlbyk/posh-git/issues/537#issuecomment-360211364 a go.
Most helpful comment
Haha! Of course!
I really love posh-git, so I will try to keep contributing in the future.