irongit
bash-help/highlight-debug.sh
28 lines1018 BBourne Again Shell (bash)
1# Highlight the current directory for all commands
2# WARNING: This changes the success status code to 69
3shopt -s extdebug
4highlight() {
5 if [ "$BASH_LOADED" = false ] || [ "$BASH_COMMAND" = "uh" ] || [ "$BASH_COMMAND" = "hh" ] || [[ "$BASH_COMMAND" =~ "trap" ]]; then
6 return 0 # Return 0 lets the command run how it normally would
7 fi
8 results=$(eval "$BASH_COMMAND" 2>&1)
9 status=$?
10 if [ -n "$results" ]; then # Prevent highlighting if no results
11 echo -e "$results" | sed "s|^.*$(pwd).*$|$(printf '\033[0;34m')\0$(printf '\033[0m')|g"
12 fi
13
14 if [ $status=0 ]; then
15 return 69 # 69 becomes your new success code
16 fi
17 echo "status: $status"
18 return $status # Return original error code
19}
20uh() { # Unset the highlight trap
21 echo -e "\033[31mUnsetting highlight trap\033[0m"
22 trap - DEBUG
23}
24hh() { # Set the highlight trap
25 echo -e "\033[32mSetting highlight trap\033[0m"
26 trap highlight DEBUG
27}
28# trap highlight DEBUG # Set the highlight trap by default