Skip to content

Commit

Permalink
Fix sudofox#4: Compatibility with ZSH
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Melora Hugues authored and aHugues committed Jan 17, 2023
1 parent bbfc233 commit dd12afe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -78,4 +84,4 @@ mommy ls
# Output:
# That's a good kiddo~ ❤️
```
```
34 changes: 23 additions & 11 deletions shell-mommy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd12afe

Please sign in to comment.