Browse Source

feat(updater): do not update when user already typed some characters (#9699)

Fixes #9699
Marc Cornellà 2 years ago
parent
commit
f0f42828fa
1 changed files with 25 additions and 16 deletions
  1. 25 16
      tools/check_for_upgrade.sh

+ 25 - 16
tools/check_for_upgrade.sh

@@ -146,26 +146,35 @@ function update_ohmyzsh() {
     return
   fi
 
-  # Ask for confirmation before updating unless in auto mode
+  # Don't ask for confirmation before updating if in auto mode
   if [[ "$update_mode" = auto ]]; then
     update_ohmyzsh
-  elif [[ "$update_mode" = reminder ]]; then
+    return $?
+  fi
+
+  # If in reminder mode show reminder and exit
+  if [[ "$update_mode" = reminder ]]; then
     echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
-  else
-    # input sink to swallow all characters typed before the prompt
-    # and add a newline if there wasn't one after characters typed
-    while read -t -k 1 option; do true; done
-    [[ "$option" != ($'\n'|"") ]] && echo
-
-    echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
-    read -r -k 1 option
-    [[ "$option" != $'\n' ]] && echo
-    case "$option" in
-      [yY$'\n']) update_ohmyzsh ;;
-      [nN]) update_last_updated_file ;&
-      *) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
-    esac
+    return 0
   fi
+
+  # If user has typed input, show reminder and exit
+  if read -t -k 1; then
+    echo
+    echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`"
+    return 0
+  fi
+
+  # Ask for confirmation and only update on 'y', 'Y' or Enter
+  # Otherwise just show a reminder for how to update
+  echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
+  read -r -k 1 option
+  [[ "$option" = $'\n' ]] || echo
+  case "$option" in
+    [yY$'\n']) update_ohmyzsh ;;
+    [nN]) update_last_updated_file ;&
+    *) echo "[oh-my-zsh] You can update manually by running \`omz update\`" ;;
+  esac
 }
 
 unset update_mode