├── powerline-screenshot.png ├── LICENSE.md ├── README.md └── ps1_prompt /powerline-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhijitvalluri/bash-powerline-shell/HEAD/powerline-screenshot.png -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Abhijit Kiran Valluri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A bash-script based Powerline style prompt for your shell 2 | ========================================================= 3 | 4 | A [Powerline](https://github.com/Lokaltog/vim-powerline) like prompt for Bash using bash scripts 5 | 6 | This implementation was inspired by [Powerline-Shell](https://github.com/banga/powerline-shell). 7 | But that version, which is based on python, was exceedingly slow for my taste, taking upto 1.5 8 | seconds for the shell prompt to be set with the python script. Hence, I decided to recreate the 9 | appearance created in that prompt using only shell scripts. 10 | 11 | Consequently this is not nearly as readily configurable as python based one above. However, with 12 | a little bit of effort and knowledge of shell scripts, you can make this look however you desire. 13 | 14 | ![Screenshot of powerline style shell](https://github.com/abhijitvalluri/bash-powerline-shell/blob/master/powerline-screenshot.png?raw=true) 15 | 16 | ### Main Features 17 | 18 | * Shows time, hostname, current directory, and prompt 19 | * Optional segments for number of background jobs, read-only status of current directory, return 20 | code for last command if exited in failure along with color change of the prompt to red 21 | * Segment that shows current git branch if in a git repository, and number of modified, staged 22 | and conflicted files in the git repository. 23 | 24 | ### Unicode symbols used 25 | 26 | * Number of background jobs: `⏎` followed by number 27 | * Read-only status of current directory: `` 28 | * Return code from previous bash command: `⚑` 29 | * Git branch: `` 30 | * Number of modified files in git repo: `✚` 31 | * Number of staged files in git repo: `` 32 | * Number of conflicted files in git repo: `✘` 33 | 34 | In addition, the following symbols are used to separate different segments: ,  35 | 36 | # Setup 37 | 38 | * This shell script heavily relies on ANSI color codes to display colors in the terminal window. 39 | They may not be portable, hence it may not work for you out of the box. You may need to set 40 | your $TERM to `xterm-256color`. 41 | 42 | * In addition, Unicode symbols require a special font to be used in your terminal. Please use 43 | one of the powerline fonts available at: https://github.com/Lokaltog/powerline-fonts 44 | 45 | * The "DejaVu Sans Mono for Powerline" font seems to be one of the better fonts for Unicode support. 46 | 47 | * You may need to use `uxterm`, the XTerm with unicode support, in place of `xterm` if the 48 | Unicode characters are not correctly rendered. 49 | 50 | * This script uses the `__git_ps1` command to identify if a directory is a git repository, and if 51 | it is, identify the branch name. If you do not have this command, please install it for your system. 52 | 53 | * Clone this repository, or copy the `ps1_prompt` shell script to a suitable location. Let us assume 54 | that you placed this in your home directory. 55 | 56 | * In your `.bashrc` or `.profile`, whichever is used, source the `ps1_prompt` script as follows: 57 | 58 | ``` 59 | source ~/ps1_prompt 60 | ``` 61 | 62 | # Customization 63 | 64 | At this point, this script has no simple option for customization apart from manually editting 65 | the script to your liking. Once you get the flow of the script, and after familiarizing yourself 66 | with the ANSI color codes, it should be fairly straightforward to enhance the script to your taste. 67 | 68 | Some resources that can help you with tweaking the script: 69 | 70 | * [Bash tips: Colors and formatting (ANSI/VT100 Control sequences)](https://misc.flogisoft.com/bash/tip_colors_and_formatting) 71 | * [ANSI color codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors): Color codes for the xterm/uxterm. 72 | * [UTF-8 Unicode test documents](https://github.com/bits/UTF-8-Unicode-Test-Documents.git): Use the test documents in this repo to see what symbols are rendered using your chosen font. 73 | * [PS1 cheat sheet](https://ss64.com/bash/syntax-prompt.html): Prompt variable characters for customizing the output of the PS1 prompt. 74 | 75 | If you can contribute to make this script more customizable, please do so and submit a pull request! 76 | -------------------------------------------------------------------------------- /ps1_prompt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # TODO: Currently, this is hardcoded. Ideally, this should be rewritten to be configurable. 4 | 5 | function ps1_powerline { 6 | RETCODE=$? # save return code 7 | NUM_JOBS=$(jobs -rp | wc -l) 8 | GIT_BRANCH=$(__git_ps1) 9 | 10 | local GREY="\[\e[48;5;240m\]\[\e[38;5;250m\]" 11 | local GREY_END="\[\e[48;5;2m\]\[\e[38;5;240m\]" 12 | 13 | local GREEN="\[\e[48;5;2m\]\[\e[38;5;255m\]" 14 | local GREEN_END="\[\e[48;5;27m\]\[\e[38;5;2m\]" 15 | 16 | local ORANGE="\[\e[48;5;208m\]\[\e[38;5;255m\]" 17 | local ORANGE_END="\[\e[48;5;236m\]\[\e[38;5;208m\]" 18 | local ORANGE_RET_END="\[\e[48;5;160m\]\[\e[38;5;208m\]" # when next segment is prompt with return code 19 | 20 | local BLUE="\[\e[48;5;27m\]\[\e[38;5;255m\]" 21 | local BLUE_END="\[\e[48;5;208m\]\[\e[38;5;27m\]" # when next segment is git 22 | local BLUE_END_JOBS="\[\e[48;5;93m\]\[\e[38;5;27m\]" # when next segment is jobs 23 | local BLUE_END_ALT="\[\e[48;5;236m\]\[\e[38;5;27m\]" # when next segment is prompt 24 | local BLUE_END_RET="\[\e[48;5;160m\]\[\e[38;5;27m\]" # when next segment is prompt with return code 25 | 26 | local JOBS="\[\e[48;5;93m\]\[\e[38;5;255m\] ⏎" 27 | local JOBS_END="\[\e[48;5;236m\]\[\e[38;5;93m\]" # when next segment is prompt 28 | local JOBS_NO_RET_END="\[\e[48;5;208m\]\[\e[38;5;93m\]" # when next segment is git 29 | local JOBS_NO_GIT_END="\[\e[48;5;160m\]\[\e[38;5;93m\]" # when next segment is prompt with return code 30 | 31 | local RET="\[\e[48;5;160m\]\[\e[38;5;255m\]" 32 | local RET_END="\[\e[0m\]\[\e[38;5;160m\]\[\e[0m\] " 33 | 34 | local PROMPT="\[\e[48;5;236m\]\[\e[38;5;255m\]" 35 | local PROMPT_END="\[\e[0m\]\[\e[38;5;236m\]\[\e[0m\] " 36 | 37 | if [ ! -w "$PWD" ]; then 38 | # Current directory is not writable 39 | BLUE_END="\[\e[48;5;160m\]\[\e[38;5;27m\]\[\e[38;5;255m\]  \[\e[48;5;208m\]\[\e[38;5;160m\]" 40 | BLUE_END_JOBS="\[\e[48;5;160m\]\[\e[38;5;27m\]\[\e[38;5;255m\]  \[\e[48;5;93m\]\[\e[38;5;160m\]" 41 | BLUE_END_ALT="\[\e[48;5;160m\]\[\e[38;5;27m\]\[\e[38;5;255m\]  \[\e[48;5;236m\]\[\e[38;5;160m\]" 42 | BLUE_END_RET="\[\e[48;5;160m\]\[\e[38;5;27m\]\[\e[38;5;255m\]  " 43 | fi 44 | 45 | if [ -z "$GIT_BRANCH" ]; then 46 | # Is not a git repo 47 | if [ "$RETCODE" -eq 0 ]; then 48 | if [ "$NUM_JOBS" -eq 0 ]; then 49 | # No jobs or ret code 50 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_ALT$PROMPT \$ $PROMPT_END" 51 | else 52 | # no ret code but jobs 53 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_JOBS$JOBS$NUM_JOBS $JOBS_END$PROMPT \$ $PROMPT_END" 54 | fi 55 | else 56 | if [ "$NUM_JOBS" -eq 0 ]; then 57 | # No jobs but ret code is there 58 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_RET$RET \$ ⚑ $RETCODE $RET_END" 59 | else 60 | # Both jobs and ret code 61 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_JOBS$JOBS$NUM_JOBS $JOBS_NO_GIT_END$RET \$ ⚑ $RETCODE $RET_END" 62 | fi 63 | fi 64 | else 65 | # Is a git repo 66 | local NUM_MODIFIED=$(git diff --name-only --diff-filter=M | wc -l) 67 | local NUM_STAGED=$(git diff --staged --name-only --diff-filter=AM | wc -l) 68 | local NUM_CONFLICT=$(git diff --name-only --diff-filter=U | wc -l) 69 | local GIT_STATUS="\[\e[48;5;255m\]\[\e[38;5;208m\]\[\e[38;5;27m\] ✚$NUM_MODIFIED \[\e[38;5;208m\]\[\e[38;5;2m\] ✔$NUM_STAGED \[\e[38;5;208m\]\[\e[38;5;9m\] ✘$NUM_CONFLICT " 70 | if [ "$RETCODE" -eq 0 ]; then 71 | GIT_STATUS+="\[\e[38;5;255m\]\[\e[48;5;236m\]" 72 | else 73 | GIT_STATUS+="\[\e[38;5;255m\]\[\e[48;5;160m\]" 74 | fi 75 | 76 | if [ "$RETCODE" -eq 0 ]; then 77 | if [ "$NUM_JOBS" -eq 0 ]; then 78 | # No jobs or ret code 79 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END$ORANGE $GIT_BRANCH $GIT_STATUS$PROMPT \$ $PROMPT_END" 80 | else 81 | # no ret code but jobs 82 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_JOBS$JOBS$NUM_JOBS $JOBS_NO_RET_END$ORANGE $GIT_BRANCH $GIT_STATUS$PROMPT \$ $PROMPT_END" 83 | fi 84 | else 85 | if [ "$NUM_JOBS" -eq 0 ]; then 86 | # No jobs but ret code is there 87 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END$ORANGE $GIT_BRANCH $GIT_STATUS$RET \$ ⚑ $RETCODE $RET_END" 88 | else 89 | # Both jobs and ret code 90 | PS1="$GREY \t $GREY_END$GREEN @\h $GREEN_END$BLUE \W $BLUE_END_JOBS$JOBS$NUM_JOBS $JOBS_NO_RET_END$ORANGE $GIT_BRANCH $GIT_STATUS$RET \$ ⚑ $RETCODE $RET_END" 91 | fi 92 | fi 93 | 94 | fi 95 | } 96 | 97 | if [ "$TERM" != "linux" ]; then 98 | PROMPT_COMMAND="ps1_powerline; $PROMPT_COMMAND" 99 | fi 100 | --------------------------------------------------------------------------------