Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add a setting for the AdminText prefix in window title #537

Closed
nebosite opened this issue Jan 18, 2018 · 14 comments
Closed

Comments

@nebosite
Copy link

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.

@sdwheeler
Copy link

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)"
}

@rkeithhill
Copy link
Collaborator

You can also disable posh-git from messing with your title bar completely. Put $GitPromptSettings.EnableWindowTitle = $false in your profile script.

@nebosite
Copy link
Author

@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: #538

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 19, 2018

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:

  • If EnableWindowTitle is a string`, use the existing logic (with hard-coded 'Administrator: ' prefix)
  • Else if 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?

@rkeithhill
Copy link
Collaborator

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.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jan 24, 2018

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.

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 24, 2018

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: 1ed6d4d.

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jan 24, 2018

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.

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 24, 2018

OK, so my proposal:

v0

  1. Add RepoName to $GitStatus
  2. Accept [ScriptBlock] for EnableWindowTitle in addition to current support for $false or a Git prefix string
  3. If it's a [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.

v1

  1. Replace all expandable strings with [ScriptBlock]s, for consistency.
  2. Rename EnableWindowTitle to simply WindowTitle, to be run on every prompt unless it's $null, as described above.

@rkeithhill
Copy link
Collaborator

Sounds reasonable. I'll tackle the v1 changes tonight.

@nebosite
Copy link
Author

I'm probably too crunched for time to make these changes without screwing something up. :(

@dahlbyk
Copy link
Owner

dahlbyk commented Jan 24, 2018

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. 🥂

@nebosite
Copy link
Author

Haha! Of course!

I really love posh-git, so I will try to keep contributing in the future.

@dahlbyk
Copy link
Owner

dahlbyk commented Apr 20, 2018

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 #537 (comment) a go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants