Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on zsh? #4

Open
farfalleflickan opened this issue Jan 12, 2023 · 21 comments
Open

Not working on zsh? #4

farfalleflickan opened this issue Jan 12, 2023 · 21 comments

Comments

@farfalleflickan
Copy link

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○) » 
@aHugues
Copy link

aHugues commented Jan 12, 2023

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.

@sudofox
Copy link
Owner

sudofox commented Jan 12, 2023

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

@Ecorous
Copy link

Ecorous commented Jan 13, 2023

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

@aHugues
Copy link

aHugues commented Jan 13, 2023

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 pushed a commit to aHugues/shell-mommy that referenced this issue Jan 14, 2023
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)

A comment has been added to the `PROMPT_COMMAND` example, wich only
works in bash since the feature doesn't exist in zsh.
@DarkShadow76
Copy link

DarkShadow76 commented Jan 16, 2023

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"

aHugues pushed a commit to aHugues/shell-mommy that referenced this issue Jan 17, 2023
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
@Ecorous
Copy link

Ecorous commented Jan 22, 2023

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

@DarkShadow76
Copy link

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.

@Ecorous
Copy link

Ecorous commented Jan 28, 2023

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

@nonetrix
Copy link

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
}

@thaYt
Copy link

thaYt commented Feb 13, 2023

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

@sudofox
Copy link
Owner

sudofox commented Feb 13, 2023

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!

@y0nei
Copy link

y0nei commented Feb 13, 2023

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

@avanisubbiah
Copy link

avanisubbiah commented Feb 14, 2023

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

@CorruptedVor
Copy link
Contributor

works for me on zsh with this pr #13

@Minion3665
Copy link

Minion3665 commented Feb 19, 2023

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)

@Ecorous
Copy link

Ecorous commented Feb 20, 2023

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

@FWDekker
Copy link

FWDekker commented Feb 24, 2023

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.)

@Minion3665
Copy link

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

@FWDekker
Copy link

@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 ^^

@catuhana
Copy link

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

@CorruptedVor
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests