A beautiful and useful low-latency prompt for your shell, written in go
Clone or download
justjanne Merge pull request #93 from pdf/fix_jobs
Consistently fix jobs segment
Latest commit 6f16015 Oct 14, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
themes Adding duration Aug 14, 2018
.editorconfig Add .editorconfig and set indent_size to 4 Aug 24, 2017
.gitignore Add powerline-go to .gitignore Sep 9, 2017
.travis.yml Update .travis.yml Sep 26, 2017
LICENSE.md Replaced License with the official GPLv3 Markdown version Aug 22, 2017
README.md Merge pull request #98 from fgaz/patch-1 Oct 14, 2018
aws.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
defaults.go Merge remote-tracking branch 'upstream/master' Sep 11, 2018
generatePreview.sh Add preview lines for terraform workspace segment Aug 21, 2018
main.go Added subversion support. Sep 18, 2018
powerline.go Style fixes Mar 23, 2018
preview.png Switched the preview generation from SVG to using an actual shell Aug 23, 2017
segment-cwd.go Fix path alias feature to always sort paths by reverse length Apr 12, 2018
segment-docker.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-dotenv.go Allow .envrc for dotenv segment May 10, 2018
segment-duration.go Adding duration Aug 14, 2018
segment-exitcode.go Issue #11 - Exit codes either meaningful or numeric Dec 30, 2017
segment-git.go Ignore git submodules when using git segment Oct 10, 2018
segment-gitlite.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-hg.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-hostname.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-jobs.go Consistently fix jobs segment Jul 1, 2018
segment-kube.go Fix a broken default, and add kube segment Oct 18, 2017
segment-load.go Add load module Apr 17, 2018
segment-newline.go Add support for newline segment Nov 25, 2017
segment-nix-shell.go Add support for nix-shell Apr 23, 2018
segment-node.go added node module/segment that displays version from package.json in … Mar 30, 2018
segment-perlbrew.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-readonly.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-readonly_windows.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-root.go Introduced a system for shell segment priorities, which, if space is Aug 24, 2017
segment-shellvar.go Style fixes Mar 23, 2018
segment-ssh.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-subversion.go Added subversion support. Sep 18, 2018
segment-termtitle.go Style fixes Mar 23, 2018
segment-terraform_workspace.go Add segment-terraform_workspace.go Aug 22, 2018
segment-time.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-username.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
segment-vgo.go Add segment for virtualgo Feb 26, 2018
segment-virtualenv.go Improved truncation functionality for terminals of smaller width Oct 18, 2017
themes.go Merge remote-tracking branch 'upstream/master' Sep 11, 2018

README.md

A Powerline style prompt for your shell

A Powerline like prompt for Bash, ZSH and Fish. Based on Powerline-Shell by @banga. Ported to golang by @justjanne.

Solarized+Powerline

  • Shows some important details about the git/hg branch (see below)
  • Changes color if the last command exited with a failure code
  • If you're too deep into a directory tree, shortens the displayed path with an ellipsis
  • Shows the current Python virtualenv environment
  • Shows if you are in a nix shell
  • It's easy to customize and extend. See below for details.

Table of Contents

Version Control

All of the version control systems supported by powerline shell give you a quick look into the state of your repo:

  • The current branch is displayed and changes background color when the branch is dirty.
  • When the local branch differs from the remote, the difference in number of commits is shown along with or indicating whether a git push or pull is pending

In addition, git has a few extra symbols:

  • -- a file has been modified, but not staged for commit
  • -- a file is staged for commit
  • -- a file has conflicts
  • + -- untracked files are present
  • -- stash is present

Each of these will have a number next to it if more than one file matches.

Installation

powerline-go uses ANSI color codes, these should nowadays work everywhere, but you may have to set your $TERM to xterm-256color for it to work.

If you want to use the "patched" mode (which is the default, and provides improved UI), you'll need to install a powerline font, either as fallback, or by patching the font you use for your terminal: see powerline-fonts.
Alternatively you can use "compatible" or "flat" mode.

Precompiled Binaries

I provide precompiled binaries for x64 Linux and macOS in the releases tab

Other Platforms

  • Install (and update) the package with
go get -u github.com/justjanne/powerline-go
  • By default it will be in ~/go/bin, if you want to change that, you can set your $GOPATH and/or $GOBIN, but will need to change the path in the following scripts, too.

Bash

Add the following to your .bashrc (or .profile on Mac):

function _update_ps1() {
    PS1="$($GOPATH/bin/powerline-go -error $?)"
}

if [ "$TERM" != "linux" ] && [ -f "$GOPATH/bin/powerline-go" ]; then
    PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi

ZSH

Add the following to your .zshrc:

function powerline_precmd() {
    PS1="$(~/go/bin/powerline-go -error $? -shell zsh)"
}

function install_powerline_precmd() {
  for s in "${precmd_functions[@]}"; do
    if [ "$s" = "powerline_precmd" ]; then
      return
    fi
  done
  precmd_functions+=(powerline_precmd)
}

if [ "$TERM" != "linux" ]; then
    install_powerline_precmd
fi

Fish

Redefine fish_prompt in ~/.config/fish/config.fish:

function fish_prompt
    ~/go/bin/powerline-go -error $status -shell bare
end

Nix

When using nix-shell --pure, powerline-go will not be accessible, and your prompt will disappear.

To work around this you can add this snippet to your .bashrc, which should re-enable the prompt in most cases:

# Workaround for nix-shell --pure
if [ "$IN_NIX_SHELL" == "pure" ]; then
    if [ -x "$HOME/.nix-profile/bin/powerline-go" ]; then
        alias powerline-go="$HOME/.nix-profile/bin/powerline-go"
    elif [ -x "/run/current-system/sw/bin/powerline-go" ]; then
        alias powerline-go="/run/current-system/sw/bin/powerline-go"
    fi
fi

Customization

There are a few optional arguments which can be seen by running powerline-go -help. These can be used by changing the command you have set in your shell’s init file.

Usage of powerline-go:
  -colorize-hostname
    	 Colorize the hostname based on a hash of itself
  -cwd-max-depth int
    	 Maximum number of directories to show in path
    	 (default 5)
  -cwd-max-dir-size int
    	 Maximum number of letters displayed for each directory in the path
    	 (default -1)
  -cwd-mode string
    	 How to display the current directory
    	 (valid choices: fancy, plain, dironly)
    	 (default "fancy")
  -duration string
    	 The number of wall-clock seconds elapsed for the previous command.  May be an integer (whole seconds) or floating point (seconds and fractions of a second).
  -east-asian-width
    	 Use East Asian Ambiguous Widths
  -error int
    	 Exit code of previously executed command
  -ignore-repos string
    	 A list of git repos to ignore. Separate with ','.
    	 Repos are identified by their root directory.
  -max-width int
    	 Maximum width of the shell that the prompt may use, in percent. Setting this to 0 disables the shrinking subsystem.
  -mode string
    	 The characters used to make separators between segments.
    	 (valid choices: patched, compatible, flat)
    	 (default "patched")
  -modules string
    	 The list of modules to load, separated by ','
    	 (valid choices: aws, cwd, docker, dotenv, duration, exit, git, gitlite, hg, host, jobs, load, nix-shell, perlbrew, perms, root, shell-var, ssh, termtitle, time, user, venv, node)
    	 (default "venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root")
  -newline
    	 Show the prompt on a new line
  -numeric-exit-codes
    	 Shows numeric exit codes for errors.
  -path-aliases string
    	 One or more aliases from a path to a short name. Separate with ','.
    	 An alias maps a path like foo/bar/baz to a short name like FBB.
    	 Specify these as key/value pairs like foo/bar/baz=FBB.
    	 Use '~' for your home dir. You may need to escape this character to avoid shell substitution.
  -priority string
    	 Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','
    	 (valid choices: aws, cwd, cwd-path, docker, duration, exit, git-branch, git-status, hg, host, jobs, load, nix-shell, perlbrew, perms, root, ssh, time, user, venv, node)
    	 (default "root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path")
  -shell string
    	 Set this to your shell type
    	 (valid choices: bare, bash, zsh)
    	 (default "bash")
  -shell-var string
    	 A shell variable to add to the segments.
  -shorten-gke-names
    	 Shortens names for GKE Kube clusters.
  -theme string
    	 Set this to the theme you want to use
    	 (valid choices: default, low-contrast)
    	 (default "default")
  -truncate-segment-width int
    	 Minimum width of a segment, segments longer than this will be shortened if space is limited. Setting this to 0 disables it.
    	 (default 16)

Path Aliases

The point of the path aliases feature is to allow you to replace long paths with a shorter string that you can understand more quickly. This is useful if you're often in deep path hierarchies that end up consuming most of your terminal width, even when some portions are replaced by an ellipsis.

For example, you might want to replace the string ~/go/src/github.com with @GOPATH-GH. When you're in a directory like ~/go/src/github.com/justjanne/powerline-go, you'll instead see @GOPATH-GH > justjanne > powerline-go in the shell prompt.

Aliases are defined as comma-separated key value pairs, like this:

powerline-go ... -path-aliases \~/go/src/github.com=@GOPATH-GH,\~/work/projects/foo=@FOO,\~/work/projects/bar=@BAR

Note that you should use ~ instead of /home/username when specifying the path. Also make sure to escape the ~ character. Otherwise your shell will perform interpolation on it before powerline-go can see it!

Duration

The duration segment requires some assistance from the shell. The shell must have a hook that gets executed immediately before the command.

Bash

Bash 4.4 includes an easy way to get a start-time, using $PS0. However, not all operating systems come with a sufficiently recent version of Bash installed. This example only has seconds precision. Add or modify your .bashrc file to include the following:

INTERACTIVE_BASHPID_TIMER="/tmp/${USER}.START.$$"

PS0='$(echo $SECONDS > "$INTERACTIVE_BASHPID_TIMER")'

function _update_ps1() {
  local __ERRCODE=$?

  local __DURATION=0
  if [ -e $INTERACTIVE_BASHPID_TIMER ]; then
    local __END=$SECONDS
    local __START=$(cat "$INTERACTIVE_BASHPID_TIMER")
    __DURATION="$(($__END - ${__START:-__END}))"
    rm -f "$INTERACTIVE_BASHPID_TIMER"
  fi

  PS1="$($GOPATH/bin/powerline-go -modules duration -duration $__DURATION -error $__ERRCODE -shell bash)"
}

if [ "$TERM" != "linux" ] && [ -f "$GOPATH/bin/powerline-go" ]; then
  PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi

Zsh

Using $EPOCHREALTIME requires loading the 'datetime' module in your .zshrc file, for example:

zmodload zsh/datetime

function preexec() {
  __TIMER=$EPOCHREALTIME
}

function powerline_precmd() {
  local __ERRCODE=$?
  local __DURATION=0

  if [ -n $__TIMER ]; then
    local __ERT=$EPOCHREALTIME
    __DURATION="$(($__ERT - ${__TIMER:-__ERT}))"
  fi

  PS1="$(powerline-go -modules duration -duration $__DURATION -error $__ERRCODE -shell zsh)"
  unset __TIMER
}

If the 'datetime' module is unavailable or unwanted, you may replace $EPOCHREALTIME with $SECONDS, at the loss of precision.

Fish

The fish prompt, in ~/.config/fish/config.fish, will require a minimum of changes, as Fish automatically provides $CMD_DURATION, although with only milliseconds accuracy.

function fish_prompt
    set duration (math -s6 "$CMD_DURATION / 1000")
    ~/go/bin/powerline-go -modules duration -duration $duration -error $status -shell bare
end

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.