Skip to content

Instantly share code, notes, and snippets.

@alhoo
Last active May 11, 2019 05:04
Show Gist options
  • Save alhoo/db7ac8163bbbea8ce4c758f27928009c to your computer and use it in GitHub Desktop.
Save alhoo/db7ac8163bbbea8ce4c758f27928009c to your computer and use it in GitHub Desktop.
bashrc PS1
# support cd .....
cd () {
if [ $# -eq 1 ] && [ "${1:0:3}" == "..." ]; then
for ((i=1; i<=${#1} - 1; i++)); do
builtin cd ..;
done;
pwd;
else
builtin cd "$@"
fi
}
EXIT_VALUE=0
__prompt_command() {
# local EXIT="$?"
# Define colors for cleaner code
local Red='\[\e[0;31m\]'
local Gre='\[\e[0;32m\]'
local LGre='\[\033[01;32m\]'
local BYel='\[\e[1;33m\]'
local BBlu='\[\e[1;34m\]'
local Pur='\[\e[0;35m\]'
local RCol='\[\e[0m\]'
# Add general status
local hasjobs=$(jobs|wc -l)
local state=""
if [ "$EUID" -eq 0 ]; then
state="$state${BYel}! "
fi
if [ "$EXIT_VALUE" != "0" ]; then
state="$state${Red}🔥 "
# state="$state${Red}🗶 "
else
state="$state${Gre}✓ "
fi
if [ $hasjobs -gt 1 ]; then
state="${Gre}⊛ (background jobs: $hasjobs)\n $state"
elif [ $hasjobs -gt 0 ]; then
state="${Gre}⊛ (background job pid: $!)\n $state"
fi
# Add virtualenv status
local virtualenv=""
if [ "$VIRTUAL_ENV" != "" ]; then
virtualenv=" (`basename \"$VIRTUAL_ENV\"`) "
fi
# Add git status
local gitstatus=""
if [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == true ]]; then
gitbranch=$(__git_ps1)
gitstatus="$gitbranch"
if [[ -z $(git log origin/master..HEAD 2>/dev/null) ]]; then
gitstatus="${Gre}$gitstatus"
else
gitstatus="${Red}$gitstatus"
fi
fi
# Build the actual PS1 string
PS1="${virtualenv}${state}${RCol}${debian_chroot:+($debian_chroot)}${LGre}\u@\h${BYel}:${BBlu}\w${gitstatus} ${RCol}\n\$ "
}
export PROMPT_COMMAND=__prompt_command
function command_exec_handle() {
STARTTIME=$(date --iso-8601=seconds)
SECONDS=0
eval $1;
EXIT_VALUE="$?"
CMD=$(echo $1|tr '[\r\n]' ' '|sed -e 's/^ *[0-9]\+ \+//' -e 's/\x22/\\\x22/g'|sed 's/ \+$//')
ENDTIME=$(date --iso-8601=seconds)
if [ $SECONDS -gt 30 ]; then notify-send "valmis" "$CMD\n\n$EXIT_VALUE"; fi
if [ $SECONDS -gt 600 ]; then echo "$CMD\n\n$EXIT_VALUE"|mail -s "Execution finished!" "lasse.hyyrynen@pm.me"; fi
echo "{\"start\": \"$STARTTIME\", \"end\": \"$ENDTIME\", \"bashpid\": $$, \"cmd\": \"$CMD\", \"pwd\": \"$PWD\", \"return\": $EXIT_VALUE}" >> ~/.bash_fullhistory.jsonl;
}
@alhoo
Copy link
Author

alhoo commented Apr 15, 2019

Use custom alhoo/bash functionality

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