shell-mommy icon indicating copy to clipboard operation
shell-mommy copied to clipboard

Not working on zsh?

Open farfalleflickan opened this issue 3 years ago • 25 comments

I can't seem to get this to work on zsh...

user@theSource:~/.scripts/shell-mommy(master○) » source /home/user/.scripts/shell-mommy/shell-mommy.sh 
user@theSource:~/.scripts/shell-mommy(master○) » mommy ls
.rw-r--r--  51k user 12 Jan 13:55  preview.png
.rw-r--r-- 2.9k user 12 Jan 13:55  README.md
.rw-r--r-- 5.4k user 12 Jan 13:55  shell-mommy.sh
success:3: = not found
user@theSource:~/.scripts/shell-mommy(master○) » 

farfalleflickan avatar Jan 12 '23 13:01 farfalleflickan

I just had to fight a bit on that. There are several issues with zsh, which I manually solved, might propose a better solution if I don't forget.

Anyway the changes I made :

Conditions

Conditions must use the [[ ... ]] syntax. There are a few conditions that only use [ ... ].

  • line 90 and 93 in the pick_response function
  • line 124 in the success function

read function

read -ra doesn't work on zsh, I completely replaced the pick_word function with this snippet

pick_word() {
    raw=$1
    words=(${(@s:/:)raw})
    index=$(($RD_NUMBER % ${#words[@]}))
    echo "${words[$index + 1]}"
  }

Array index

Note that in bash, arrays start at 0, but they start at 1 in zsh, meaning that you would need an offset.

Random generator

The random generator is not reseeded inside a subshell, meaning that you would always have the same result each time. To solve this I added this line to the beginning of the mommy function

RD_NUMBER=$RANDOM

If you want, I can provide my edited file, but I'm pretty sure it only works with zsh, wich makes it more of a quick and dirty hack than an actual solution.

aHugues avatar Jan 12 '23 13:01 aHugues

I don't usually use zsh, but if there's a way to make it work on both bash and zsh, I would be happy to see a PR!

image

sudofox avatar Jan 12 '23 14:01 sudofox

The PROMPT_COMMAND example appears to not be working on zsh as well

Ecorous avatar Jan 13 '23 19:01 Ecorous

I'll try to make a PR on the weekend or next week, depending on my schedule, I should be able to make it work with both zsh and bash

aHugues avatar Jan 13 '23 19:01 aHugues

i manage to solve this by adding this line in the .zshrc file

precmd() { mommy "$PROMPT_COMMAND" } 

instead of

export PROMPT_COMMAND="mommy \\$\\(exit \$?\\); $PROMPT_COMMAND"

DarkShadow76 avatar Jan 16 '23 23:01 DarkShadow76

i manage to solve this by adding this line in the .zshrc file

precmd() { mommy "$PROMPT_COMMAND" } 

instead of

export PROMPT_COMMAND="mommy \\$\\(exit \$?\\); $PROMPT_COMMAND"

Negative responses don't seem to work with this

Ecorous avatar Jan 22 '23 19:01 Ecorous

i manage to solve this by adding this line in the .zshrc file

precmd() { mommy "$PROMPT_COMMAND" } 

instead of

export PROMPT_COMMAND="mommy \\$\\(exit \$?\\); $PROMPT_COMMAND"

Negative responses don't seem to work with this

Did you resolve it?. that line worked for me.

DarkShadow76 avatar Jan 27 '23 14:01 DarkShadow76

i manage to solve this by adding this line in the .zshrc file

precmd() { mommy "$PROMPT_COMMAND" } 

instead of

export PROMPT_COMMAND="mommy \\$\\(exit \$?\\); $PROMPT_COMMAND"

Negative responses don't seem to work with this

Did you resolve it?. that line worked for me.

Positive responses work, but not negative

Ecorous avatar Jan 28 '23 11:01 Ecorous

I tried fixing it using ChatGPT since it's so small for the lols big surprise it didn't work

mommy() {
  MOMMY_DIR="$(cd "$(dirname "${0}")" >/dev/null 2>&1 && pwd)"
  COLORS_LIGHT_PINK='%F{217}'
  COLORS_LIGHT_BLUE='%F{117}'
  COLORS_FAINT='%f'
  COLORS_RESET='%f'
  DEF_WORDS_LITTLE="girl"
  DEF_WORDS_PRONOUNS="her"
  DEF_WORDS_ROLES="mommy"
  DEF_MOMMY_COLOR="${COLORS_LIGHT_PINK}"
  DEF_ONLY_NEGATIVE="false"
  NEGATIVE_RESPONSES=(
    "do you need MOMMYS_ROLE's help~? ❤️"
  )
  POSITIVE_RESPONSES=(
    "*pets your head*"
  )
  if [[ -n "${SHELL_MOMMYS_LITTLE:-}" ]]; then
    DEF_WORDS_LITTLE="${SHELL_MOMMYS_LITTLE}"
  fi
  if [[ -n "${SHELL_MOMMYS_PRONOUNS:-}" ]]; then
    DEF_WORDS_PRONOUNS="${SHELL_MOMMYS_PRONOUNS}"
  fi
  if [[ -n "${SHELL_MOMMYS_ROLES:-}" ]]; then
    DEF_WORDS_ROLES="${SHELL_MOMMYS_ROLES}"
  fi
  if [[ -n "${SHELL_MOMMYS_COLOR:-}" ]]; then
    DEF_MOMMY_COLOR="${SHELL_MOMMYS_COLOR}"
  fi
  if [[ "${SHELL_MOMMYS_ONLY_NEGATIVE:-}" == "true" ]]; then
    DEF_ONLY_NEGATIVE="true"
  fi
  if [[ -n "${SHELL_MOMMYS_POSITIVE_RESPONSES:-}" ]]; then
    POSITIVE_RESPONSES=("${SHELL_MOMMYS_POSITIVE_RESPONSES[@]}")
  fi
  if [[ -n "${SHELL_MOMMYS_NEGATIVE_RESPONSES:-}" ]]; then
    NEGATIVE_RESPONSES=("${SHELL_MOMMYS_NEGATIVE_RESPONSES[@]}")
  fi
  pick_word() {
    IFS='/' read -a words <<<"$1"
    index=$(($RANDOM % ${#words[@]}))
    echo "${words[$index]}"
  }
  pick_response() {
    if [ "$1" == "positive" ]; then
      index=$(($RANDOM % ${#POSITIVE_RESPONSES[@]}))
      element=${POSITIVE_RESPONSES[$index]}
      echo $element
    elif [ "$1" == "negative" ]; then
      index=$(($RANDOM % ${#NEGATIVE_RESPONSES[@]}))
      element=${NEGATIVE_RESPONSES[$index]}
      echo $element
    fi
  }

sub_terms() {
    local response="$1"
    local affectionate_term=$(pick_word "${DEF_WORDS_LITTLE}")
    local pronoun=$(pick_word "${DEF_WORDS_PRONOUNS}")
    local role=$(pick_word "${DEF_WORDS_ROLES}")
    response=${response//AFFECTIONATE_TERM/$affectionate_term}
    response=${response//MOMMYS_PRONOUN/$pronoun}
    response=${response//MOMMYS_ROLE/$role}
    echo "${DEF_MOMMY_COLOR}$response${COLORS_RESET}"
  }
  success() {
    if [ "$DEF_ONLY_NEGATIVE" == "true" ]; then
      return 0
    fi
    local response=$(pick_response "positive")
    sub_terms "$response"
    return 0
  }
  failure() {
    local rc=$?
    local response=$(pick_response "negative")
    sub_terms "$response"
    return $rc
  }
  "$@"
  local cmd_result=$?
  if [ "$cmd_result" == 0 ]; then
    success
  else
    failure
  fi
  return $cmd_result
}

nonetrix avatar Feb 13 '23 00:02 nonetrix

made a small (but awful) workaround that works, at least for me; try:

. /path/to/shell-mommy.sh
precmd() { if (( $? == 0 )); then; echo $(success); else; echo $(failure); fi }
mommy

in .zshrc; only issue is that it echoes twice at zsh's startup this also uses aHugues's patch

thaYt avatar Feb 13 '23 01:02 thaYt

Been getting a fair bit of attention regarding zsh compatibility. I honestly haven't, and please don't rake me over the coals for this, cared enough about it to put in a ton of effort into fixing it.

So far each proposed thing that's been submitted seems to have some kind of issue with it, and my unfamiliarity with zsh means I'm not likely to be able to debug it myself. So I'm kinda waiting for you guys to reach consensus on what works.

Thanks~

edit: my opinion on adding zsh support has improved slightly as a coworker has informed me that it is now the default shell in macOS. can't leave the folks using mac out!

sudofox avatar Feb 13 '23 21:02 sudofox

https://gitlab.com/dwt1/bash-insulter from Derek Taylor does simmilar things and has support for other shells, maybe you guys can figure something out from this repo

y0nei avatar Feb 13 '23 22:02 y0nei

made a small (but awful) workaround that works, at least for me; try:

. /path/to/shell-mommy.sh
precmd() { if (( $? == 0 )); then; echo $(success); else; echo $(failure); fi }
mommy

in .zshrc; only issue is that it echoes twice at zsh's startup this also uses aHugues's patch

I tried this but I get weird division by zero errors

pick_word:read:1: bad option: -a                                                   
pick_word:2: division by zero
pick_word:read:1: bad option: -a
pick_word:2: division by zero
pick_word:read:1: bad option: -a
pick_word:2: division by zero
I'm so proud of you, my love~ ❤️

This seems to happen for both positive and negative responses

Edit: I made a dumb mistake and forgot to switch branches to the patched branch. NVM

avanisubbiah avatar Feb 14 '23 22:02 avanisubbiah

works for me on zsh with this pr https://github.com/sudofox/shell-mommy/pull/13

CorruptedVor avatar Feb 15 '23 18:02 CorruptedVor

works for me on zsh with this pr #13

Same for me, I'm using this precmd instead of any others mentioned here though

precmd() { if (( $? != 0 )); then; mommy false; fi }

this only gives a mommy invocation if my command failed (like SHELL_MOMMYS_ONLY_NEGATIVE)

Minion3665 avatar Feb 19 '23 22:02 Minion3665

works for me on zsh with this pr #13

Same for me, I'm using this precmd instead of any others mentioned here though

precmd() { if (( $? != 0 )); then; mommy false; fi }

this only gives a mommy invocation if my command failed (like SHELL_MOMMYS_ONLY_NEGATIVE)

Can use

precmd() { if (( $? != 0 )); then; mommy false; else; mommy true; fi }

for positive as well

Ecorous avatar Feb 20 '23 15:02 Ecorous

You can also use RPS1 to put mommy's output on the right side, which looks really cute imo :)

Not sure how exactly to integrate it with sudofox' version of mommy, but I recently made my own version of mommy which you can integrate with zsh using the following settings:

set -o PROMPT_SUBST
RPS1='$(mommy -1 -s $?)'

fwdekker mommy with zsh

Unfortunately, I couldn't find a way to get it working with colours, since my version uses tput setaf and it looks like zsh overestimates the size of RPS1 if there's ANSI escape sequences in there :/

(Alternatively, use precmd() { mommy -s $? 2>&1 } in your .zshrc to write output after each command, which does work with colors.)

fwdekker avatar Feb 24 '23 12:02 fwdekker

Unfortunately, I couldn't find a way to get it working with colours, since my version uses tput setaf and it looks like zsh overestimates the size of RPS1 if there's ANSI escape sequences in there :/

If I remember rightly you can put ansi escape sequence in %{brackets like this%} to avoid ZSH including them in the string length estimation (e.g. in this (messy... sorry!) line where I set my rps1). It might be a challenge to get mommy's escape sequences to work without modifying it but you should be able to color the text yourself and have it work fine with zsh

Minion3665 avatar Feb 24 '23 13:02 Minion3665

@Minion3665 Thanks for the tip! I implemented a nice workaround that works for zsh and will deploy it later today :-)

(in the screenshot the font is intentionally super large so it's more HD) image

But I don't want to hijack this issue to be around my own program, so if anyone has further comments, suggestions, issues, etc. for my version of mommy, please visit the discussions for my version of mommy or check the issues for my version of mommy ^^

fwdekker avatar Feb 24 '23 14:02 fwdekker

I don't want to bump this, and my purpose is not to advertise. But seeing these PR and issues, I just wanted to mention that I already had the similar plugin for ZSH. tuhanayim/zsh-mommy

catuhana avatar May 31 '23 08:05 catuhana

This issue is essentially redundant since the changes for POSIX compability.

CorruptedVor avatar May 31 '23 15:05 CorruptedVor

ik this is a dead post but how do I set the pronouns on zsh?

SteamRex25 avatar May 20 '25 19:05 SteamRex25

ik this is a dead post but how do I set the pronouns on zsh? https://youtu.be/-GrfpK5iEm8?si=zfg-x9g2ClQJHEW2

ayushkanekarx avatar May 22 '25 17:05 ayushkanekarx

ik this is a dead post but how do I set the pronouns on zsh? https://youtu.be/-GrfpK5iEm8?si=zfg-x9g2ClQJHEW2

may I ask what this has to do with the question I posted? Brodies video only covers bash, not zsh

SteamRex25 avatar May 22 '25 19:05 SteamRex25

ik this is a dead post but how do I set the pronouns on zsh? https://youtu.be/-GrfpK5iEm8?si=zfg-x9g2ClQJHEW2

may I ask what this has to do with the question I posted? Brodies video only covers bash, not zsh

This project still doesn't support zsh iirc, I have a bash only plugin at https://git.gay/tuhana/zsh-mommy and it has everything you need to know at its README.

catuhana avatar May 22 '25 19:05 catuhana