From dd12afeddff8810ee93b81443a5d591ab8acea04 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sat, 14 Jan 2023 17:30:42 +0100 Subject: [PATCH] Fix #4: Compatibility with ZSH This commit introduces several fixes to make sure the program works in bash and zsh - conditions use the correct [[]] syntax - the random generator has been replaced with the use of /dev/urandom - fixed the '/' separated arrays of words to work in both shells - fixed the way array elements are accessed (0-indexes in bash but 1-indexed in zsh) The PROMPT_COMMAND in the readme has been updated to provide a working example with zsh (@AlonsoCGonzaled suggestion) @mja00 suggested using the LC_CTYPE=C variable for the random generator to ensure compatibility with MacOS --- README.md | 10 ++++++++-- shell-mommy.sh | 34 +++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 37c7668..f0f25f2 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,18 @@ To use the `mommy` function, you can source the `shell-mommy.sh` script in your . /path/to/shell-mommy.sh ``` -If you'd like it to always show a message after each command, you can define a custom `PROMPT_COMMAND` like so: +If you'd like it to always show a message after each command, you can define a custom `PROMPT_COMMAND` like so (bash): ``` export PROMPT_COMMAND="mommy \\$\\(exit \$?\\); $PROMPT_COMMAND" ``` +On zsh, you can add the following to your `~/.zshrc` + +```bash +precmd() { mommy "$PROMPT_COMMAND" } +``` + ## Configuration The `mommy` function has several customizable options that can be set using environment variables: @@ -78,4 +84,4 @@ mommy ls # Output: # That's a good kiddo~ ❤️ -``` \ No newline at end of file +``` diff --git a/shell-mommy.sh b/shell-mommy.sh index 44c1b52..8621990 100644 --- a/shell-mommy.sh +++ b/shell-mommy.sh @@ -84,22 +84,34 @@ mommy() { if [[ -n "${SHELL_MOMMYS_NEGATIVE_RESPONSES:-}" ]]; then NEGATIVE_RESPONSES=("${SHELL_MOMMYS_NEGATIVE_RESPONSES[@]}") fi - + + # generate a random base 10 number using /dev/urandom + random() { + rd_number=$(LC_CTYPE=C tr -cd '[:digit:]' < /dev/urandom | head -c 6) + # 10# is used to force the number to be considered as base 10, otherwise a number starting + # with `08` will fail in bash + echo "10#$rd_number" + } + # split a string on forward slashes and return a random element pick_word() { - IFS='/' read -ra words <<<"$1" - index=$(($RANDOM % ${#words[@]})) - echo "${words[$index]}" + if [[ -n "$ZSH_VERSION" ]]; then + words=(${(@s:/:)1}) + else + words=(${1//\// }) + fi + index=$(($(random) % ${#words[@]})) + echo "${words[@]:$index:1}" } pick_response() { # given a response type, pick an entry from the array - if [ "$1" == "positive" ]; then - index=$(($RANDOM % ${#POSITIVE_RESPONSES[@]})) - element=${POSITIVE_RESPONSES[$index]} - elif [ "$1" == "negative" ]; then - index=$(($RANDOM % ${#NEGATIVE_RESPONSES[@]})) - element=${NEGATIVE_RESPONSES[$index]} + if [[ "$1" == "positive" ]]; then + index=$(($(random) % ${#POSITIVE_RESPONSES[@]})) + element=${POSITIVE_RESPONSES[@]:$index:1} + elif [[ "$1" == "negative" ]]; then + index=$(($(random) % ${#NEGATIVE_RESPONSES[@]})) + element=${NEGATIVE_RESPONSES[@]:$index:1} else echo "Invalid response type: $1" exit 1 @@ -128,7 +140,7 @@ mommy() { success() { ( # if we're only supposed to show negative responses, return - if [ "$DEF_ONLY_NEGATIVE" == "true" ]; then + if [[ "$DEF_ONLY_NEGATIVE" == "true" ]]; then return 0 fi # pick_response for the response type