From e132105cc334de949cec721c03186ace9c06ea8b Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 8 Feb 2022 23:17:50 -0500 Subject: [PATCH 1/2] Day two: not much to do here, so I made a fun prompt script. --- day2_exitcodeprompt.sh | 31 +++++++++++++++++++++++++++++++ test_error.sh | 6 ++++++ 2 files changed, 37 insertions(+) create mode 100644 day2_exitcodeprompt.sh create mode 100755 test_error.sh diff --git a/day2_exitcodeprompt.sh b/day2_exitcodeprompt.sh new file mode 100644 index 0000000..661c050 --- /dev/null +++ b/day2_exitcodeprompt.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# +# Day 2 - Generate Inventory +# "Pimp my prompt" with the return value of the last command. +# source this from ~/.bashrc or similar to modify PS1 to show the return value of previous command. +# Use the "demojify" command to restore your previous prompt after sourcing. + +PS1OLD=$PS1 + +exitprompt () +{ + ERROR="$?" + if [ "$ERROR" -eq 0 ] + then + COLORCODE=32 # foreground green + EMOJI="🙂" + else + COLORCODE=31 # foreground red + EMOJI="🙁" + fi + echo -e "\e[${COLORCODE}m${ERROR}${EMOJI}\e[0m" +} + +demojify () +{ + PS1=$PS1OLD +} + +PS1="$PS1 \$(exitprompt)$ " + diff --git a/test_error.sh b/test_error.sh new file mode 100755 index 0000000..effbc7a --- /dev/null +++ b/test_error.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# Returns a non-zero exit code on purpose to test the day 2 error prompt. + +exit 42 + From 198242e1d573bf07c4f32a6437b8a7a1363a065b Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 8 Feb 2022 23:22:57 -0500 Subject: [PATCH 2/2] added newline, I like this alighment better --- day2_exitcodeprompt.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/day2_exitcodeprompt.sh b/day2_exitcodeprompt.sh index 661c050..d1cd9d4 100644 --- a/day2_exitcodeprompt.sh +++ b/day2_exitcodeprompt.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# Day 2 - Generate Inventory +# Day 2 - Exit Code PS1 prompt # "Pimp my prompt" with the return value of the last command. # source this from ~/.bashrc or similar to modify PS1 to show the return value of previous command. # Use the "demojify" command to restore your previous prompt after sourcing. @@ -19,7 +19,7 @@ exitprompt () COLORCODE=31 # foreground red EMOJI="🙁" fi - echo -e "\e[${COLORCODE}m${ERROR}${EMOJI}\e[0m" + echo -e "\e[${COLORCODE}m${ERROR} ${EMOJI} \e[0m" } demojify () @@ -27,5 +27,5 @@ demojify () PS1=$PS1OLD } -PS1="$PS1 \$(exitprompt)$ " +PS1="\$(exitprompt)\n$PS1"