pure-pwsh

July 19, 2020 · View on GitHub

PowerShell implementation of the pure prompt.

Loads git status information asynchronously so that the prompt doesn't block and is able to update the prompt in response to file system changes without any user interation.

summary

Dependencies

  • Terminal with ANSI colour support
  • PowerShell 7.0+
  • Git 2.0+ on your path

Installation

Install from the gallery or clone this repository

Install-Module pure-pwsh

and import it in your profile. If you use this with posh-git (e.g. for its excellent Git completion) then you'll probably want to import pure-pwsh first so that posh-git doesn't spend time configuring its own prompt.

# ~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1

Import-Module pure-pwsh
Import-Module posh-git

Options

Set options on the $pure global.

OptionDescriptionDefault value
PwdColorCurrent directory name colour
BranchColorCurrent branch name colour
DirtyColorGit dirty marker colour
RemoteColorRemote status colour (up/down arrows)
ErrorColorError prompt color
PromptColorColour of the main prompt
TimeColorColour used for command timings
UserColorColour of user & hostname in SSH
SlowCommandTimeDuration at which command is 'slow'00:05
FetchIntervalInterval at which to fetch from remotes05:00
PromptCharPrompt character
UpCharUp arrow
DownCharDown arrow
PendingCharShown during git status update
WindowTitleCustomise the window title{ $PWD.Path.Replace($HOME, '~')}
BranchFormatterCustomize format of git branch name{ $args }
PwdFormatterCustomize format of working dir name{ $PWD.Path.Replace($HOME, '~')}
PrePromptCustomize the line above the prompt{ param ($user, $cwd, $git, $slow) … }

Compatibility

pure-pwsh should work anywhere PowerShell 7 does, including Windows, Mac, and Linux. Due to a longstanding bug in PSReadLine, async updates may not be scheduled on Mac and Linux until you interact with the console in some way.

Not currently included

  • No vi mode indicator
  • No git stash indicator
  • No git action indicator (rebase, cherry pick, etc.)
  • No python virtual env indicator

Consider raising an issue if you want any of the above, or use one of the recipes below.

Recipes

Shorten path segments

Abbreviate each segment of the current path except for the leaf node.

abbreviated-path

$pure.PwdFormatter = {
  (
    ((Split-Path $pwd).Replace($HOME, '~').Split($pwd.Provider.ItemSeparator) |% {$_[0]}) +
    (Split-Path -Leaf $pwd)
  ) -join '/'
}

Truncate branch name

Truncate the branch name to a maximum of 12 characters.

truncate branch

$pure.BranchFormatter = {
  param ($n)
  $n.Length -lt 12 ? $n : ($n[0..11] -join '') + '…'
}

Show git stash indicator

stash indicator

$pure.PrePrompt = {
  param ($user, $cwd, $git, $slow)
  "`n$user{0}$cwd $git{1}$slow `n" -f($user ? ' ' : ''), ((git stash list) ? ' ≡ ' : '')
}

Show Python virtual env

stash indicator

$pure.PrePrompt = {
    param ($user, $cwd, $git, $slow)
    "`n$user{1}{0}$cwd $git $slow `n" -f
    ($user ? ' ' : ''),
    (($ve = $env:virtual_env) ? "$($pure._branchcolor)($(Split-Path -Leaf $ve)) " : "" )
}

One line prompt

stash indicator

$pure.UserColor = '38;5;242;4'

$pure.PrePrompt = {
  param ($user, $cwd, $git, $slow)
  $seperator = $pure._branchcolor +  " ❯ "
  "`n$user{0}$cwd{1}$git$slow " -f
    ($user ? $seperator : ''),
    ($git ? $seperator : '')
}

Use a different colour for user and hostname parts

stash indicator

$pure.UserFormatter = { param ($ssh, $user, $hostname) $ssh ? "$user`e[32m@$hostname" : '' }