From ff41d9acc805099f27f1cf060ca9db72d2fdb74f Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 30 Jan 2023 12:24:02 -0600 Subject: Bash! --- bash/prompt.bash | 56 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) (limited to 'bash/prompt.bash') diff --git a/bash/prompt.bash b/bash/prompt.bash index 06ca638..f09d3ab 100644 --- a/bash/prompt.bash +++ b/bash/prompt.bash @@ -1,8 +1,32 @@ # bash prompt -if [[ -n "$INSIDE_EMACS" ]]; then - PS1="\n\[\e[34m\]\$ \[\e[0m\]" -else +MY_BASH_PROMPT=smiley_prompt + +# terminal title (current directory) +__prompt_term_title() { # __prompt_term_title TITLE... + printf '\033]0;%s\007\n' "$*" +} + +__prompt_exit_code() { # __prompt_exit_code CODES... + # Each CODE is of the form N=STR. If the code doesn't match one of CODES, + # its number will be printed. + local ec=$? + for code; do + if ((ec == ${code%=*})); then + printf '%s\n' "${code#*=}" + return + elif ((ec == 0)) && [[ "${code%=*}" = ok ]]; then + printf '%s\n' "${code#*=}" + return + elif ((ec > 0)) && [[ "${code%=*}" = fail ]]; then + printf '%s\n' "${code#*=}" + return + fi + done + printf '%s\n' "$ec" +} + +two_line_prompt() { PS1= # user, host, and cwd @@ -21,7 +45,7 @@ else for file in "${possible_git_prompt_locations[@]}"; do if [[ -f "$file" ]]; then source "$file" && - PS1+='\[\e[35m\]$(__git_ps1)' + PS1+='\[\e[0m\]\[\e[35m\]$(__git_ps1)' break fi done @@ -29,20 +53,18 @@ else # newline PS1+='\[\e[0m\]\n' - # exit code (only if error) - __prompt_exit_code() { - local ec=$? - (($ec > 0)) && - printf "$ec" - } - PS1+='\[\e[31m\]$(__prompt_exit_code)\[\e[0m\]' - - # terminal title (current directory) - __prompt_term_title() { - printf '\033]0;%s\007\n' "$*" - } + PS1+='\[\e[31m\]$(__prompt_exit_code 0=)\[\e[0m\]' PS1+='\[$(__prompt_term_title "$USER@$(hostname): $PWD")\]' # delimiter PS1+='; ' -fi +} + +smiley_prompt() { + __prompt_ec() { case $? in 0) echo '^_^' ;; *) echo ';_;' ;; esac } + PS1='\[\e[1m\]$(__prompt_exit_code "ok=^_^" "fail=;_;") \w\[\e[0m\] ' + PS1+='\[$(__prompt_term_title "$USER@$(hostname): $PWD")\]' + PS1+='\$ ' +} + +eval "$MY_BASH_PROMPT" -- cgit 1.4.1-21-gabe81