Skip to content

Instantly share code, notes, and snippets.

@zeekhuge
Last active September 8, 2022 14:18
Show Gist options
  • Select an option

  • Save zeekhuge/f8b40ba32eb0f23a83930978a3ed2a9e to your computer and use it in GitHub Desktop.

Select an option

Save zeekhuge/f8b40ba32eb0f23a83930978a3ed2a9e to your computer and use it in GitHub Desktop.
Zbar
#!/usr/bin/env bash
#
# Instruction :
# - Save this file locally
# - Open terminal
# - execute command "source </path/to/this/file>
# - To get the smallest possible promt, execute "zbar 0"
# - Otherwise, execute command "zbar"
#
gen_z_prompt () {
readonly Z_RED_COLOR="\[$(tput setaf 1)\]"
readonly Z_GREEN_COLOR="\[$(tput setaf 2)\]"
readonly Z_YELLOW_COLOR="\[$(tput setaf 3)\]"
readonly Z_BLUE_COLOR="\[$(tput setaf 4)\]"
readonly Z_RESET_COLOR="\[$(tput sgr0)\]"
function zbar_gen_dir () {
local DIR=${Z_RED_COLOR}
DIR+=$(pwd | sed 's/\/.*\///')
DIR+=${Z_RESET_COLOR}
echo ${DIR}
}
function zbar_is_git () {
git log -1 2>/dev/null 1>&2
}
function zbar_gen_git () {
zbar_is_git \
&& (\
echo "${Z_YELLOW_COLOR}$(\
basename `git rev-parse --show-toplevel`\
)${Z_BLUE_COLOR}:${Z_GREEN_COLOR}$(\
git branch | grep "*" | sed 's/\*\s\+//'\
)${Z_RESET_COLOR}") \
|| echo ""
}
function zbar_gen_dot () {
local lastExitStatus=$1
$(exit ${lastExitStatus}) && local SYMBOL=${Z_GREEN_COLOR} || local SYMBOL=${Z_RED_COLOR}
SYMBOL+=$'\u25cf'
SYMBOL+=${Z_RESET_COLOR}
echo ${SYMBOL}
}
function zbar_ranger_shell () {
if [[ ! -z $RANGER_LEVEL ]]; then
echo "${Z_YELLOW_COLOR}(ranger)${Z_RESET_COLOR} "
fi
}
function zbar_python_venv () {
if [[ ! -z $VIRTUAL_ENV ]] ; then
echo "${Z_YELLOW_COLOR}(venv)${Z_RESET_COLOR} "
fi
}
function zbar-dir-git-gen () {
local lastExitStatus=$?
PS1="$(zbar_ranger_shell)$(zbar_python_venv)$(zbar_gen_dir) $(zbar_gen_dot ${lastExitStatus}) "
$(zbar_is_git) && PS1+="$(zbar_gen_git) $(zbar_gen_dot ${lastExitStatus}) "
}
zbar-gen () {
local lastExitStatus=$?
PS1="$(zbar_python_venv)$(zbar_gen_dot ${lastExitStatus}) "
}
function zbar() {
if [ "$1" == "0" ]; then
PROMPT_COMMAND=zbar-gen
else
PROMPT_COMMAND=zbar-dir-git-gen
fi
}
}
gen_z_prompt
unset gen_z_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment