├── LICENSE ├── README.md ├── bash-powerline.sh └── screenshots └── solarized-light.png /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bash-powerline 2 | 3 | Powerline for Bash in pure Bash script. 4 | 5 | ![bash-powerline](https://raw.github.com/riobard/bash-powerline/master/screenshots/solarized-light.png) 6 | 7 | ## Features 8 | 9 | * Git: show branch name, tag name, or unique short hash. 10 | * Git: show "*" symbol with uncommited modifications. 11 | * Git: show "↑" symbol and number of commits ahead of remote. 12 | * Git: show "↓" symbol and number of commits behind remote. 13 | * Platform-dependent prompt symbols. 14 | * Color-coded prompt symbol according to previous command execution status. 15 | * Use Bash builtin when possible to reduce delay. Delay sucks! 16 | * No need for patched fonts. 17 | 18 | 19 | ## Installation 20 | 21 | Download the Bash script 22 | 23 | curl https://raw.githubusercontent.com/riobard/bash-powerline/master/bash-powerline.sh > ~/.bash-powerline.sh 24 | 25 | And source it in your `.bashrc` 26 | 27 | source ~/.bash-powerline.sh 28 | 29 | For best result, use [Solarized 30 | colorscheme](https://github.com/altercation/solarized) for your terminal 31 | emulator. Or hack your own colorscheme by modifying the script. It's really 32 | easy. 33 | 34 | 35 | ## Why? 36 | 37 | This script is inspired by 38 | [powerline-shell](https://github.com/milkbikis/powerline-shell), which is 39 | implemented in Python. Python scripts are much easier to write and maintain than 40 | Bash scripts, but invoking Python interpreter introduces noticable delay to 41 | draw. I hate delays, so I ported just the part I need to pure Bash script. 42 | 43 | The other reason is that I don't like the idea of patching fonts. The 44 | font patching mechanism from the original Powerline does not work with the 45 | bitmap font (Apple Monaco without anti-aliasing) I use on non-retina screens. 46 | I'd rather stick with existing unicode symbols. 47 | 48 | 49 | ## See also 50 | 51 | * [zsh-powerline](https://github.com/riobard/zsh-powerline): Same thing but for 52 | Zsh. 53 | * [powerline](https://github.com/Lokaltog/powerline): Unified Powerline 54 | written in Python. This is the future of all Powerline derivatives. 55 | * [vim-powerline](https://github.com/Lokaltog/vim-powerline): Powerline in Vim 56 | writtien in pure Vimscript. Deprecated. 57 | * [tmux-powerline](https://github.com/erikw/tmux-powerline): Powerline for Tmux 58 | written in Bash script. Deprecated. 59 | * [powerline-shell](https://github.com/milkbikis/powerline-shell): Powerline for 60 | Bash/Zsh/Fish implemented in Python. Might be merged into the unified 61 | Powerline. 62 | * [emacs powerline](https://github.com/milkypostman/powerline): Powerline for 63 | Emacs 64 | -------------------------------------------------------------------------------- /bash-powerline.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ## Uncomment to disable git info 4 | #POWERLINE_GIT=0 5 | 6 | __powerline() { 7 | # Colors 8 | COLOR_RESET='\[\033[m\]' 9 | COLOR_CWD=${COLOR_CWD:-'\[\033[0;34m\]'} # blue 10 | COLOR_GIT=${COLOR_GIT:-'\[\033[0;36m\]'} # cyan 11 | COLOR_SUCCESS=${COLOR_SUCCESS:-'\[\033[0;32m\]'} # green 12 | COLOR_FAILURE=${COLOR_FAILURE:-'\[\033[0;31m\]'} # red 13 | 14 | # Symbols 15 | SYMBOL_GIT_BRANCH=${SYMBOL_GIT_BRANCH:-⑂} 16 | SYMBOL_GIT_MODIFIED=${SYMBOL_GIT_MODIFIED:-*} 17 | SYMBOL_GIT_PUSH=${SYMBOL_GIT_PUSH:-↑} 18 | SYMBOL_GIT_PULL=${SYMBOL_GIT_PULL:-↓} 19 | 20 | if [[ -z "$PS_SYMBOL" ]]; then 21 | case "$(uname)" in 22 | Darwin) PS_SYMBOL='';; 23 | Linux) PS_SYMBOL='$';; 24 | *) PS_SYMBOL='%';; 25 | esac 26 | fi 27 | 28 | __git_info() { 29 | [[ $POWERLINE_GIT = 0 ]] && return # disabled 30 | hash git 2>/dev/null || return # git not found 31 | local git_eng="env LANG=C git" # force git output in English to make our work easier 32 | 33 | # get current branch name 34 | local ref=$($git_eng symbolic-ref --short HEAD 2>/dev/null) 35 | 36 | if [[ -n "$ref" ]]; then 37 | # prepend branch symbol 38 | ref=$SYMBOL_GIT_BRANCH$ref 39 | else 40 | # get tag name or short unique hash 41 | ref=$($git_eng describe --tags --always 2>/dev/null) 42 | fi 43 | 44 | [[ -n "$ref" ]] || return # not a git repo 45 | 46 | local marks 47 | 48 | # scan first two lines of output from `git status` 49 | while IFS= read -r line; do 50 | if [[ $line =~ ^## ]]; then # header line 51 | [[ $line =~ ahead\ ([0-9]+) ]] && marks+=" $SYMBOL_GIT_PUSH${BASH_REMATCH[1]}" 52 | [[ $line =~ behind\ ([0-9]+) ]] && marks+=" $SYMBOL_GIT_PULL${BASH_REMATCH[1]}" 53 | else # branch is modified if output contains more lines after the header line 54 | marks="$SYMBOL_GIT_MODIFIED$marks" 55 | break 56 | fi 57 | done < <($git_eng status --porcelain --branch 2>/dev/null) # note the space between the two < 58 | 59 | # print the git branch segment without a trailing newline 60 | printf " $ref$marks" 61 | } 62 | 63 | ps1() { 64 | # Check the exit code of the previous command and display different 65 | # colors in the prompt accordingly. 66 | if [ $? -eq 0 ]; then 67 | local symbol="$COLOR_SUCCESS $PS_SYMBOL $COLOR_RESET" 68 | else 69 | local symbol="$COLOR_FAILURE $PS_SYMBOL $COLOR_RESET" 70 | fi 71 | 72 | local cwd="$COLOR_CWD\w$COLOR_RESET" 73 | # Bash by default expands the content of PS1 unless promptvars is disabled. 74 | # We must use another layer of reference to prevent expanding any user 75 | # provided strings, which would cause security issues. 76 | # POC: https://github.com/njhartwell/pw3nage 77 | # Related fix in git-bash: https://github.com/git/git/blob/9d77b0405ce6b471cb5ce3a904368fc25e55643d/contrib/completion/git-prompt.sh#L324 78 | if shopt -q promptvars; then 79 | __powerline_git_info="$(__git_info)" 80 | local git="$COLOR_GIT\${__powerline_git_info}$COLOR_RESET" 81 | else 82 | # promptvars is disabled. Avoid creating unnecessary env var. 83 | local git="$COLOR_GIT$(__git_info)$COLOR_RESET" 84 | fi 85 | 86 | PS1="$cwd$git$symbol" 87 | } 88 | 89 | PROMPT_COMMAND="ps1${PROMPT_COMMAND:+; $PROMPT_COMMAND}" 90 | } 91 | 92 | __powerline 93 | unset __powerline 94 | -------------------------------------------------------------------------------- /screenshots/solarized-light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riobard/bash-powerline/9335174f1da016d2aaa7dd86e81912fdaccfe5c4/screenshots/solarized-light.png --------------------------------------------------------------------------------