Skip to content

Instantly share code, notes, and snippets.

@DASPRiD
Last active November 18, 2021 22:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DASPRiD/5701728 to your computer and use it in GitHub Desktop.
Save DASPRiD/5701728 to your computer and use it in GitHub Desktop.
function _update_ps1() {
export PS1="$(~/bin/powerline-shell.sh $? your-default-username)"
}
export PROMPT_COMMAND="_update_ps1"
#!/bin/bash
fg_black="\[\033[0;30m\]"
fg_red="\[\033[0;31m\]"
fg_green="\[\033[0;32m\]"
fg_yellow="\[\033[0;33m\]"
fg_blue="\[\033[0;34m\]"
fg_magenta="\[\033[0;35m\]"
fg_cyan="\[\033[0;36m\]"
fg_white="\[\033[0;37m\]"
bg_black="\[\033[40m\]"
bg_red="\[\033[41m\]"
bg_green="\[\033[42m\]"
bg_yellow="\[\033[43m\]"
bg_blue="\[\033[44m\]"
bg_magenta="\[\033[45m\]"
bg_cyan="\[\033[46m\]"
bg_white="\[\033[47m\]"
reset="\[\033[00m\]"
CURRENT_BG='none'
SEGMENT_SEPARATOR='⮀'
fg_color() {
local varname=fg_$1
echo ${!varname}
}
bg_color() {
local varname=bg_$1
echo ${!varname}
}
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
local bg fg
[[ -n $1 ]] && bg="$(bg_color $1)" || bg=""
[[ -n $2 ]] && fg="$(fg_color $2)" || fg=""
if [[ $CURRENT_BG != 'none' && $1 != $CURRENT_BG ]]; then
echo -n " $(fg_color $CURRENT_BG)$bg$SEGMENT_SEPARATOR$fg$bg "
else
echo -n "$fg$bg "
fi
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
echo -n " $reset$(fg_color $CURRENT_BG)$SEGMENT_SEPARATOR $reset"
fi
CURRENT_BG=''
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black white "$user@\h"
fi
}
# Git: branch/detached head, dirty status
prompt_git() {
local ref dirty
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
dirty=$(git status --porcelain | tail -n1)
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
if [[ -n $dirty ]]; then
dirty='±'
prompt_segment yellow black
else
prompt_segment green black
fi
echo -n "${ref/refs\/heads\//⭠ }$dirty"
fi
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue black '\w'
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="$fg_red$bg_black✘"
[[ $UID -eq 0 ]] && symbols+="$fg_yellow$bg_black⚡"
[[ -n "$symbols" ]] && prompt_segment black white "$symbols"
}
## Main prompt
RETVAL=$1
DEFAULT_USER=$2
case "$TERM" in
xterm*|rxvt*)
echo -n "\[\e]0;\u@\h: \w\a\]"
;;
*)
;;
esac
prompt_status
prompt_context
prompt_dir
prompt_git
prompt_end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment