diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2023-11-04 18:38:46 -0700 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2023-11-04 18:38:46 -0700 |
commit | 4d908094fdc2a0c0e9a0a072eba213fab7adef43 (patch) | |
tree | 7c17e70bcdeebbe96c84d849bdf17882007480d8 /tools | |
parent | 4b0bbc0b263a150eb9a9b59f196914629be06a9b (diff) | |
parent | 632ed413a9ce62747ded83d7736491b081be4b49 (diff) | |
download | zsh-master.tar.gz zsh-master.tar.bz2 zsh-master.zip |
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/changelog.sh | 101 | ||||
-rw-r--r-- | tools/check_for_upgrade.sh | 230 | ||||
-rwxr-xr-x | tools/install.sh | 4 | ||||
-rw-r--r-- | tools/uninstall.sh | 23 | ||||
-rwxr-xr-x | tools/upgrade.sh | 78 |
5 files changed, 328 insertions, 108 deletions
diff --git a/tools/changelog.sh b/tools/changelog.sh index a5cc468f2..6d768963e 100755 --- a/tools/changelog.sh +++ b/tools/changelog.sh @@ -106,6 +106,9 @@ function parse-commit { message="${match[1]}" # remove CR characters (might be inserted in GitHub UI commit description form) message="${message//$'\r'/}" + # remove lines containing only whitespace + local nlnl=$'\n\n' + message="${message//$'\n'[[:space:]]##$'\n'/$nlnl}" # skip next paragraphs (separated by two newlines or more) message="${message%%$'\n\n'*}" # ... and replace newlines with spaces @@ -157,6 +160,89 @@ function parse-commit { fi } +################################ +# SUPPORTS HYPERLINKS FUNCTION # +################################ + +# The code for checking if a terminal supports hyperlinks is copied from install.sh + +# The [ -t 1 ] check only works when the function is not called from +# a subshell (like in `$(...)` or `(...)`, so this hack redefines the +# function at the top level to always return false when stdout is not +# a tty. +if [ -t 1 ]; then + is_tty() { + true + } +else + is_tty() { + false + } +fi + +# This function uses the logic from supports-hyperlinks[1][2], which is +# made by Kat Marchán (@zkat) and licensed under the Apache License 2.0. +# [1] https://github.com/zkat/supports-hyperlinks +# [2] https://crates.io/crates/supports-hyperlinks +# +# Copyright (c) 2021 Kat Marchán +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +supports_hyperlinks() { + # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass) + if [ -n "$FORCE_HYPERLINK" ]; then + [ "$FORCE_HYPERLINK" != 0 ] + return $? + fi + + # If stdout is not a tty, it doesn't support hyperlinks + is_tty || return 1 + + # DomTerm terminal emulator (domterm.org) + if [ -n "$DOMTERM" ]; then + return 0 + fi + + # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc) + if [ -n "$VTE_VERSION" ]; then + [ $VTE_VERSION -ge 5000 ] + return $? + fi + + # If $TERM_PROGRAM is set, these terminals support hyperlinks + case "$TERM_PROGRAM" in + Hyper|iTerm.app|terminology|WezTerm) return 0 ;; + esac + + # kitty supports hyperlinks + if [ "$TERM" = xterm-kitty ]; then + return 0 + fi + + # Windows Terminal also supports hyperlinks + if [ -n "$WT_SESSION" ]; then + return 0 + fi + + # Konsole supports hyperlinks, but it's an opt-in setting that can't be detected + # https://github.com/ohmyzsh/ohmyzsh/issues/10964 + # if [ -n "$KONSOLE_VERSION" ]; then + # return 0 + # fi + + return 1 +} + ############################# # RELEASE CHANGELOG DISPLAY # ############################# @@ -208,7 +294,13 @@ function display-release { local hash="${1:-$hash}" case "$output" in raw) printf '%s' "$hash" ;; - text) printf '\e[33m%s\e[0m' "$hash" ;; # red + text) + local text="\e[33m$hash\e[0m"; # red + if supports_hyperlinks; then + printf "\e]8;;%s\a%s\e]8;;\a" "https://github.com/ohmyzsh/ohmyzsh/commit/$hash" $text; + else + echo $text; + fi ;; md) printf '[`%s`](https://github.com/ohmyzsh/ohmyzsh/commit/%s)' "$hash" "$hash" ;; esac } @@ -272,7 +364,12 @@ function display-release { case "$output" in raw) printf '%s' "$subject" ;; # In text mode, highlight (#<issue>) and dim text between `backticks` - text) sed -E $'s|#([0-9]+)|\e[32m#\\1\e[0m|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" ;; + text) + if supports_hyperlinks; then + sed -E $'s|#([0-9]+)|\e]8;;https://github.com/ohmyzsh/ohmyzsh/issues/\\1\a\e[32m#\\1\e[0m\e]8;;\a|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" + else + sed -E $'s|#([0-9]+)|\e[32m#\\1\e[0m|g;s|`([^`]+)`|`\e[2m\\1\e[0m`|g' <<< "$subject" + fi ;; # In markdown mode, link to (#<issue>) issues md) sed -E 's|#([0-9]+)|[#\1](https://github.com/ohmyzsh/ohmyzsh/issues/\1)|g' <<< "$subject" ;; esac diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh index 734714c94..1cc193bde 100644 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -9,6 +9,7 @@ fi # - prompt (default): the user is asked before updating when it's time to update # - auto: the update is performed automatically when it's time # - reminder: a reminder is shown to the user when it's time to update +# - background-alpha: an experimental update-on-the-background option # - disabled: automatic update is turned off zstyle -s ':omz:update' mode update_mode || { update_mode=prompt @@ -91,12 +92,37 @@ function is_update_available() { } function update_last_updated_file() { - echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update" + local exit_status="$1" error="$2" + + if [[ -z "${1}${2}" ]]; then + echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update" + return + fi + + cat >! "${ZSH_CACHE_DIR}/.zsh-update" <<EOD +LAST_EPOCH=$(current_epoch) +EXIT_STATUS=${exit_status} +ERROR='${error//\'/’}' +EOD } function update_ohmyzsh() { - if ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive; then + local verbose_mode + zstyle -s ':omz:update' verbose verbose_mode || verbose_mode=default + + if [[ "$update_mode" != background-alpha ]] \ + && LANG= ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" -i -v $verbose_mode; then update_last_updated_file + return $? + fi + + local exit_status error + if error=$(LANG= ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" -i -v silent 2>&1); then + update_last_updated_file 0 "Update successful" + else + exit_status=$? + update_last_updated_file $exit_status "$error" + return $exit_status fi } @@ -125,87 +151,145 @@ function has_typed_input() { } } -() { - emulate -L zsh +function handle_update() { + () { + emulate -L zsh - local epoch_target mtime option LAST_EPOCH + local epoch_target mtime option LAST_EPOCH - # Remove lock directory if older than a day - zmodload zsh/datetime - zmodload -F zsh/stat b:zstat - if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then - if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then - command rm -rf "$ZSH/log/update.lock" + # Remove lock directory if older than a day + zmodload zsh/datetime + zmodload -F zsh/stat b:zstat + if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then + if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then + command rm -rf "$ZSH/log/update.lock" + fi fi - fi - # Check for lock directory - if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then - return - fi + # Check for lock directory + if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then + return + fi - # Remove lock directory on exit. `return $ret` is important for when trapping a SIGINT: - # The return status from the function is handled specially. If it is zero, the signal is - # assumed to have been handled, and execution continues normally. Otherwise, the shell - # will behave as interrupted except that the return status of the trap is retained. - # This means that for a CTRL+C, the trap needs to return the same exit status so that - # the shell actually exits what it's running. - trap " - ret=\$? - unset update_mode - unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh 2>/dev/null - command rm -rf '$ZSH/log/update.lock' - return \$ret - " EXIT INT QUIT - - # Create or update .zsh-update file if missing or malformed - if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then - update_last_updated_file - return - fi + # Remove lock directory on exit. `return $ret` is important for when trapping a SIGINT: + # The return status from the function is handled specially. If it is zero, the signal is + # assumed to have been handled, and execution continues normally. Otherwise, the shell + # will behave as interrupted except that the return status of the trap is retained. + # This means that for a CTRL+C, the trap needs to return the same exit status so that + # the shell actually exits what it's running. + trap " + ret=\$? + unset update_mode + unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh handle_update 2>/dev/null + command rm -rf '$ZSH/log/update.lock' + return \$ret + " EXIT INT QUIT - # Number of days before trying to update again - zstyle -s ':omz:update' frequency epoch_target || epoch_target=${UPDATE_ZSH_DAYS:-13} - # Test if enough time has passed until the next update - if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then - return - fi + # Create or update .zsh-update file if missing or malformed + if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then + update_last_updated_file + return + fi - # Test if Oh My Zsh directory is a git repository - if ! (builtin cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then - echo >&2 "[oh-my-zsh] Can't update: not a git repository." - return - fi + # Number of days before trying to update again + zstyle -s ':omz:update' frequency epoch_target || epoch_target=${UPDATE_ZSH_DAYS:-13} + # Test if enough time has passed until the next update + if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then + return + fi - # Check if there are updates available before proceeding - if ! is_update_available; then - return - fi + # Test if Oh My Zsh directory is a git repository + if ! (builtin cd -q "$ZSH" && LANG= git rev-parse &>/dev/null); then + echo >&2 "[oh-my-zsh] Can't update: not a git repository." + return + fi - # If in reminder mode or user has typed input, show reminder and exit - if [[ "$update_mode" = reminder ]] || has_typed_input; then - printf '\r\e[0K' # move cursor to first column and clear whole line - echo "[oh-my-zsh] It's time to update! You can do that by running \`omz update\`" - return 0 - fi + # Check if there are updates available before proceeding + if ! is_update_available; then + update_last_updated_file + return + fi - # Don't ask for confirmation before updating if in auto mode - if [[ "$update_mode" = auto ]]; then - update_ohmyzsh - return $? - fi + # If in reminder mode or user has typed input, show reminder and exit + if [[ "$update_mode" = reminder ]] || { [[ "$update_mode" != background-alpha ]] && has_typed_input }; then + printf '\r\e[0K' # move cursor to first column and clear whole line + 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 + # Don't ask for confirmation before updating if in auto mode + if [[ "$update_mode" = (auto|background-alpha) ]]; then + update_ohmyzsh + return $? + 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 + unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh handle_update } -unset update_mode -unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh +case "$update_mode" in + background-alpha) + autoload -Uz add-zsh-hook + + _omz_bg_update() { + # do the update in a subshell + (handle_update) &| + + # register update results function + add-zsh-hook precmd _omz_bg_update_status + + # deregister background function + add-zsh-hook -d precmd _omz_bg_update + unset -f _omz_bg_update + } + + _omz_bg_update_status() { + { + local LAST_EPOCH EXIT_STATUS ERROR + if [[ ! -f "$ZSH_CACHE_DIR"/.zsh-update ]]; then + return 1 + fi + + # check update results until timeout is reached + . "$ZSH_CACHE_DIR/.zsh-update" + if [[ -z "$EXIT_STATUS" || -z "$ERROR" ]]; then + return 1 + fi + + if [[ "$EXIT_STATUS" -eq 0 ]]; then + print -P "\n%F{green}[oh-my-zsh] Update successful.%f" + return 0 + elif [[ "$EXIT_STATUS" -ne 0 ]]; then + print -P "\n%F{red}[oh-my-zsh] There was an error updating:%f" + printf "\n${fg[yellow]}%s${reset_color}" "$ERROR" + return 0 + fi + } always { + if (( TRY_BLOCK_ERROR == 0 )); then + # if last update results have been handled, remove them from the status file + update_last_updated_file + + # deregister background function + add-zsh-hook -d precmd _omz_bg_update_status + unset -f _omz_bg_update_status + fi + } + } + + add-zsh-hook precmd _omz_bg_update + ;; + *) + handle_update ;; +esac diff --git a/tools/install.sh b/tools/install.sh index f4ef16a0c..fcfbcf778 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -84,6 +84,10 @@ command_exists() { user_can_sudo() { # Check if sudo is installed command_exists sudo || return 1 + # Termux can't run sudo, so we can detect it and exit the function early. + case "$PREFIX" in + *com.termux*) return 1 ;; + esac # The following command has 3 parts: # # 1. Run `sudo` with `-v`. Does the following: diff --git a/tools/uninstall.sh b/tools/uninstall.sh index 6a0e7b4c7..6e3df7aca 100644 --- a/tools/uninstall.sh +++ b/tools/uninstall.sh @@ -1,3 +1,15 @@ +if hash chsh >/dev/null 2>&1 && [ -f ~/.shell.pre-oh-my-zsh ]; then + old_shell=$(cat ~/.shell.pre-oh-my-zsh) + echo "Switching your shell back to '$old_shell':" + if chsh -s "$old_shell"; then + rm -f ~/.shell.pre-oh-my-zsh + else + echo "Could not change default shell. Change it manually by running chsh" + echo "or editing the /etc/passwd file." + exit + fi +fi + read -r -p "Are you sure you want to remove Oh My Zsh? [y/N] " confirmation if [ "$confirmation" != y ] && [ "$confirmation" != Y ]; then echo "Uninstall cancelled" @@ -25,16 +37,5 @@ else echo "No original zsh config found" fi -if hash chsh >/dev/null 2>&1 && [ -f ~/.shell.pre-oh-my-zsh ]; then - old_shell=$(cat ~/.shell.pre-oh-my-zsh) - echo "Switching your shell back to '$old_shell':" - if chsh -s "$old_shell"; then - rm -f ~/.shell.pre-oh-my-zsh - else - echo "Could not change default shell. Change it manually by running chsh" - echo "or editing the /etc/passwd file." - fi -fi - echo "Thanks for trying out Oh My Zsh. It's been uninstalled." echo "Don't forget to restart your terminal!" diff --git a/tools/upgrade.sh b/tools/upgrade.sh index 596a59302..f7a263d66 100755 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -1,4 +1,7 @@ #!/usr/bin/env zsh +set +u # disable nounset + +local ret=0 # exit code # Protect against running with shells other than zsh if [ -z "$ZSH_VERSION" ]; then @@ -12,6 +15,23 @@ esac cd "$ZSH" +verbose_mode="default" +interactive=false + +while getopts "v:i" opt; do + case $opt in + v) + if [[ $OPTARG == default || $OPTARG == minimal || $OPTARG == silent ]]; then + verbose_mode=$OPTARG + else + echo "[oh-my-zsh] update verbosity '$OPTARG' is not valid" + echo "[oh-my-zsh] valid options are 'default', 'minimal' and 'silent'" + fi + ;; + i) interactive=true ;; + esac +done + # Use colors, but only if connected to a terminal # and that terminal supports them. @@ -164,17 +184,23 @@ fi # Update upstream remote to ohmyzsh org git remote -v | while read remote url extra; do case "$url" in + git://github.com/robbyrussell/oh-my-zsh(|.git)) + # Update out-of-date "unauthenticated git protocol on port 9418" to https + git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;; https://github.com/robbyrussell/oh-my-zsh(|.git)) - git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" - break ;; + git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" ;; git@github.com:robbyrussell/oh-my-zsh(|.git)) - git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git" - break ;; - # Update out-of-date "unauthenticated git protocol on port 9418" to https - git://github.com/robbyrussell/oh-my-zsh(|.git)) - git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git" - break ;; + git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git" ;; + https://github.com/ohmyzsh/ohmyzsh(|.git)) ;; + git@github.com:ohmyzsh/ohmyzsh(|.git)) ;; + *) continue ;; esac + + # If we reach this point we have found the proper ohmyzsh upstream remote. If we don't, + # we'll only update from the set remote if `oh-my-zsh.remote` has been set to a remote, + # as when installing from a fork. + git config --local oh-my-zsh.remote "$remote" + break done # Set git-config values known to fix git errors @@ -203,7 +229,9 @@ git checkout -q "$branch" -- || exit 1 last_commit=$(git rev-parse "$branch") # Update Oh My Zsh -printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh" +if [[ $verbose_mode != silent ]]; then + printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh" +fi if LANG= git pull --quiet --rebase $remote $branch; then # Check if it was really updated or not if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then @@ -215,24 +243,30 @@ if LANG= git pull --quiet --rebase $remote $branch; then git config oh-my-zsh.lastVersion "$last_commit" # Print changelog to the terminal - if [[ "$1" = --interactive ]]; then + if [[ $interactive == true && $verbose_mode == default ]]; then "$ZSH/tools/changelog.sh" HEAD "$last_commit" fi - printf "${BLUE}%s \`${BOLD}%s${RESET}${BLUE}\`${RESET}\n" "You can see the changelog with" "omz changelog" + if [[ $verbose_mode != silent ]]; then + printf "${BLUE}%s \`${BOLD}%s${RESET}${BLUE}\`${RESET}\n" "You can see the changelog with" "omz changelog" + fi fi - printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET - printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET - printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $RAINBOW $RESET - printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET - printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET - printf '%s %s %s %s /____/ %s %s %s %s\n' $RAINBOW $RESET - printf '\n' - printf "${BLUE}%s${RESET}\n\n" "$message" - printf "${BLUE}${BOLD}%s %s${RESET}\n" "To keep up with the latest news and updates, follow us on Twitter:" "$(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)" - printf "${BLUE}${BOLD}%s %s${RESET}\n" "Want to get involved in the community? Join our Discord:" "$(fmt_link "Discord server" https://discord.gg/ohmyzsh)" - printf "${BLUE}${BOLD}%s %s${RESET}\n" "Get your Oh My Zsh swag at:" "$(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)" + if [[ $verbose_mode == default ]]; then + printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET + printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET + printf '%s / __ \\%s/ __ \\ %s / __ `__ \\%s/ / / / %s /_ / %s/ ___/%s __ \\ %s\n' $RAINBOW $RESET + printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET + printf '%s\\____/%s_/ /_/ %s /_/ /_/ /_/%s\\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET + printf '%s %s %s %s /____/ %s %s %s %s\n' $RAINBOW $RESET + printf '\n' + printf "${BLUE}%s${RESET}\n\n" "$message" + printf "${BLUE}${BOLD}%s %s${RESET}\n" "To keep up with the latest news and updates, follow us on Twitter:" "$(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)" + printf "${BLUE}${BOLD}%s %s${RESET}\n" "Want to get involved in the community? Join our Discord:" "$(fmt_link "Discord server" https://discord.gg/ohmyzsh)" + printf "${BLUE}${BOLD}%s %s${RESET}\n" "Get your Oh My Zsh swag at:" "$(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)" + elif [[ $verbose_mode == minimal ]]; then + printf "${BLUE}%s${RESET}\n" "$message" + fi else ret=$? printf "${RED}%s${RESET}\n" 'There was an error updating. Try again later?' |