From 721244896cf2b315d081f9303b1332080963c8d3 Mon Sep 17 00:00:00 2001 From: home Date: Mon, 20 Nov 2023 18:39:04 +0300 Subject: [PATCH] PS1 : show exit codes. --- .config/bash/module/checksum.sh | 2 +- .config/bash/module/convert.sh | 4 +-- .config/bash/module/fix.sh | 2 +- .config/bash/module/git.sh | 2 +- .config/bash/module/name.sh | 6 ++-- .config/bash/module/pack.sh | 6 ++-- .config/bash/module/ps1.sh | 57 +++++++++++++++++++++++++++++++-- .config/bash/module/vdl.sh | 4 +-- 8 files changed, 68 insertions(+), 15 deletions(-) diff --git a/.config/bash/module/checksum.sh b/.config/bash/module/checksum.sh index c360329..fc9444f 100644 --- a/.config/bash/module/checksum.sh +++ b/.config/bash/module/checksum.sh @@ -25,7 +25,7 @@ checksum() # error on wrong action. echo "supported actions: new (n), check (c)." - return 1 + return 2 } # autocomplete. diff --git a/.config/bash/module/convert.sh b/.config/bash/module/convert.sh index 13cc51f..9433a89 100644 --- a/.config/bash/module/convert.sh +++ b/.config/bash/module/convert.sh @@ -9,7 +9,7 @@ convert() { # Report no output. if [[ "${to}" = "" ]]; then echo -e "${color_bred}no output file/format.${color_default}" - return 1 + return 2 fi # Report no input. @@ -28,7 +28,7 @@ convert() { ;; *) echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}" - return 3 + return 1 ;; esac diff --git a/.config/bash/module/fix.sh b/.config/bash/module/fix.sh index 877ef84..7974f11 100644 --- a/.config/bash/module/fix.sh +++ b/.config/bash/module/fix.sh @@ -8,7 +8,7 @@ fix_ethernet_speed() if [[ "${device}" = "" || "${speed}" = "" ]]; then echo "usage: fix_ethernet_speed " - return 1 + return 2 fi ethtool -s "${device}" speed "${speed}" diff --git a/.config/bash/module/git.sh b/.config/bash/module/git.sh index 83ee75c..e4294cc 100644 --- a/.config/bash/module/git.sh +++ b/.config/bash/module/git.sh @@ -60,7 +60,7 @@ gu() if [[ "${name}" = "" || "${email}" = "" ]]; then echo "usage: gu [EMAIL]" - return 1 + return 2 fi git config user.name "${name}" diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 7c41dfb..8769048 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -181,7 +181,7 @@ name_series() # error when no season number specified. if [[ "${season}" = "" ]]; then echo "usage: name_series [FILES]" - return 1 + return 2 fi # all targets by default. @@ -239,7 +239,7 @@ name_manga() # error when no season number specified. if [[ "${season}" = "" ]]; then echo "usage: name_manga [FILES]" - return 1 + return 2 fi # all targets by default. @@ -295,7 +295,7 @@ name_ext() # error when no new extension specified. if [[ "${extension}" = "" ]]; then echo "usage: name_ext [FILES]" - return 1 + return 2 fi # all targets by default. diff --git a/.config/bash/module/pack.sh b/.config/bash/module/pack.sh index ff8bb3e..e25e5be 100644 --- a/.config/bash/module/pack.sh +++ b/.config/bash/module/pack.sh @@ -12,7 +12,7 @@ pack() # report no output. if [[ "${output}" = "" ]]; then echo "Usage: pack " - return 1 + return 2 fi # report no format. @@ -25,7 +25,7 @@ pack() # report no targets. if [[ "${targets}" = "" ]]; then echo "Usage: pack " - return 3 + return 2 fi # process. @@ -120,7 +120,7 @@ unpack() ;; *) echo -e "${color_yellow}${target}: format not supported.${color_default}" - return 2 + return 1 ;; esac diff --git a/.config/bash/module/ps1.sh b/.config/bash/module/ps1.sh index c3b3c60..8c5ef90 100644 --- a/.config/bash/module/ps1.sh +++ b/.config/bash/module/ps1.sh @@ -19,7 +19,7 @@ __prompt_command() # set error red if $is_error; then - PS1+="\[${color_red}\]" + PS1+="\[${color_bred}\]" PS1+="[" else PS1+="\[${color_default}\]" @@ -44,13 +44,22 @@ __prompt_command() # set error red if $is_error; then - PS1+="\[${color_red}\]" + PS1+="\[${color_bred}\]" PS1+="] " else PS1+="\[${color_default}\]" PS1+="] " fi + # If error, show code. + if ${is_error}; then + PS1+="${color_red}(" + PS1+="${last_status}" + local error_type="$(_ps1error ${last_status})" + [[ "${error_type}" != "" ]] && PS1+=" ${error_type}" + PS1+=")${color_default} " + fi + # command on new line PS1+="\n" PS1+="\[${color_default}\]" @@ -62,3 +71,47 @@ __prompt_command() PS1+="$ " fi } + +_ps1error() { + local type + case ${1} in + 1) type="GENERAL" ;; + 2) type="MISUSE" ;; + 126) type="CANTEXEC" ;; + 127) type="CMDNF" ;; + 129) type="SIGHUP" ;; + 130) type="SIGINT" ;; + 131) type="SIGQUIT" ;; + 132) type="SIGILL" ;; + 133) type="SIGTRAP" ;; + 134) type="SIGABRT" ;; + 135) type="SIGBUS" ;; + 136) type="SIGFPE" ;; + 137) type="SIGKILL" ;; + 138) type="SIGUSR1" ;; + 139) type="SIGSEGV" ;; + 140) type="SIGUSR2" ;; + 141) type="SIGPIPE" ;; + 142) type="SIGALRM" ;; + 143) type="SIGTERM" ;; + 144) type="" ;; + 145) type="SIGCHLD" ;; + 146) type="SIGCONT" ;; + 147) type="SIGSTOP" ;; + 148) type="SIGTSTP" ;; + 149) type="SIGTTIN" ;; + 150) type="SIGTTOU" ;; + 151) type="SIGURG" ;; + 152) type="SIGXCPU" ;; + 153) type="SIGXFSZ" ;; + 154) type="SIGVTALRM" ;; + 155) type="SIGPROF" ;; + 156) type="SIGWINCH" ;; + 157) type="SIGIO" ;; + 158) type="SIGPWR" ;; + 159) type="SIGSYS" ;; + *) type="" ;; + esac + + echo -n "${type}" +} diff --git a/.config/bash/module/vdl.sh b/.config/bash/module/vdl.sh index 0d17004..1d962a8 100644 --- a/.config/bash/module/vdl.sh +++ b/.config/bash/module/vdl.sh @@ -5,7 +5,7 @@ vdl() # Check that ffmpeg and ffprobe are available. if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}" - return 2 + return 1 fi local target="${@}" # What to download/update. @@ -17,7 +17,7 @@ vdl() if [[ "${target}" = "" ]]; then echo -e "${color_red}Could not determine [LINK] to download.${color_default}" echo "Usage: vdl [LINK]" - return 1 + return 2 fi # Save [LINK] for later use.