Skip to content

Instantly share code, notes, and snippets.

@rbeer
Last active April 25, 2022 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbeer/be10bb8b349a4be8e90082b17fce8af8 to your computer and use it in GitHub Desktop.
Save rbeer/be10bb8b349a4be8e90082b17fce8af8 to your computer and use it in GitHub Desktop.
PS1 Themes
### NOTE
Remove the first `.` from this file's name. Two `..` are necessary to name this gist correctly.
###
# [44/48]
# PS1Themes - v0.1.0
# Author: Raphael Beer <raphael.beer@protonmail.com>
# Dependencies:
# - bash
# - tput
# - arrowline (https://github.com/tryone144/arrowline/)
#
function _set_arrowline_ps1() {
export PS1="$( arrowline ${?} 2> /dev/null || echo -e '\e[0marrowline failed! [\u@\h \W]' )\n\\$ "
}
function _set_dottedhook_ps1() {
local EXIT_CODE=$?
local RESET='\[$(tput sgr0)\]';
# color dividers by exit code
local EXIT_COLOR=''
# - dark red on errors
if [ $EXIT_CODE -ne 0 ]; then
EXIT_COLOR='\[\033[38;5;1m\]'
fi
# - dark yellow on C-c termination
if [ $EXIT_CODE -eq 130 ]; then
EXIT_COLOR='\[\033[38;5;3m\]'
fi
local DIVIDER="$EXIT_COLOR┄┄┄$RESET"
local DIVIDER_SINGLE="$EXIT_COLOR┄$RESET"
# git branch name (dirty tree status by text color)
local GIT_BRANCH=`git branch 2> /dev/null | grep -e ^* | cut -f2 -d" "`
local GIT_DIRTY=`git status --ignore-submodules --porcelain 2>/dev/null`
local GIT_COLOR=''
local GIT_TRACKS=''
if [[ "$GIT_DIRTY" = "" ]]; then
GIT_COLOR='\[\033[38;5;2m\]' # green
else
GIT_COLOR='\[\033[38;5;214m\]' # orange
local GIT_TRACKS_COLORS_ADDED='\[\033[38;5;154m\]' # light green
local GIT_TRACKS_COLORS_MODIFIED='\[\033[38;5;226m\]' # yellow
local GIT_TRACKS_COLORS_DELETED='\[\033[38;5;196m\]' # red
local GIT_TRACKS_COLORS_UNTRACKED='\[\033[38;5;145m\]' # grey
local GIT_TRACKS_ADDED=`echo "$GIT_DIRTY" | grep -c -e '^ *A'`
local GIT_TRACKS_TAGS_ADDED=`[[ $GIT_TRACKS_ADDED -gt 0 ]] && echo "$DIVIDER_SINGLE$GIT_TRACKS_COLORS_ADDED$GIT_TRACKS_ADDED$RESET"`
local GIT_TRACKS_MODIFIED=`echo "$GIT_DIRTY" | grep -c -e '^ *[MRC]'`
local GIT_TRACKS_TAGS_MODIFIED=`[[ $GIT_TRACKS_MODIFIED -gt 0 ]] && echo "$DIVIDER_SINGLE$GIT_TRACKS_COLORS_MODIFIED$GIT_TRACKS_MODIFIED$RESET"`
local GIT_TRACKS_DELETED=`echo "$GIT_DIRTY" | grep -c -e '^ *D'`
local GIT_TRACKS_TAGS_DELETED=`[[ $GIT_TRACKS_DELETED -gt 0 ]] && echo "$DIVIDER_SINGLE$GIT_TRACKS_COLORS_DELETED$GIT_TRACKS_DELETED$RESET"`
local GIT_TRACKS_UNTRACKED=`echo "$GIT_DIRTY" | grep -c -e '^??'`
local GIT_TRACKS_TAGS_UNTRACKED=`[[ $GIT_TRACKS_UNTRACKED -gt "0" ]] && echo "$DIVIDER_SINGLE$GIT_TRACKS_COLORS_UNTRACKED$GIT_TRACKS_UNTRACKED$RESET"`
GIT_TRACKS="$GIT_TRACKS_TAGS_ADDED$GIT_TRACKS_TAGS_MODIFIED$GIT_TRACKS_TAGS_DELETED$GIT_TRACKS_TAGS_UNTRACKED"
fi
# cyan colored pwd
# add /, except in root dir
local PWD="\[\033[38;5;6m\]\w"
[[ $(pwd) == "/" ]] && PWD="$PWD" || PWD="$PWD/"
# show git status in git repos, only
local GIT_STATUS=`[[ "$GIT_BRANCH" != "" ]] && echo " $DIVIDER $GIT_COLOR$GIT_BRANCH$RESET"`
# prompt prefix decoration
local PROMPT_PREFIX="$EXIT_COLOR╰$RESET \[$(tput bold)\]\\$ $RESET"
# assemble
PS1="$EXIT_COLOR┌─┄$RESET $PWD$RESET$GIT_STATUS$GIT_TRACKS\n$PROMPT_PREFIX"
}
function set_prompt_command() {
case "$1" in
arrowline)
export PROMPT_COMMAND=_set_arrowline_ps1
;;
dotted-hook)
export PROMPT_COMMAND=_set_dottedhook_ps1
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w/$ '
;;
esac
}
# set PS1 theme (defined in ~/.bashrc)
set_prompt_command $PS1_THEME
alias ps1t='set_prompt_command $1'
# enable 256 colors
export TERM=xterm-256color
if [ -f ~/.ps1rc ]; then
# set PS1 theme (dotted-hook, arrowline)
export PS1_THEME="dotted-hook"
. ~/.ps1rc
fi
@dj95
Copy link

dj95 commented Jun 6, 2017

https://gist.github.com/rbeer/be10bb8b349a4be8e90082b17fce8af8#file-ps1rc-L7

  • export PS1="$( arrowline ${?} 2> /dev/null || echo -e '\e[0marrowline failed! [\u@\h \W]' )\n\\$ "
  • export PS1="$( arrowline ${?} 2> /dev/null || echo -e '\e[0marrowline failed! [\u@\h \W] $') " )

@rbeer
Copy link
Author

rbeer commented Jul 10, 2017

dotted-hook font dependency: Source Code Pro (Medium)
Needed to display the dotted lines and hook correctly. :)

@rbeer
Copy link
Author

rbeer commented Apr 25, 2022

dotted_hook_ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment