19f9b6f1ade7788b07480be685cf3fcb117cad84

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(updater): add support for terminal hyperlinks

Diff

This diff is truncated to protect this page.

  1diff --git a/tools/upgrade.sh b/tools/upgrade.sh
  2index 1424d1433bd576cce15b4f6a50777a695b964c76..7642858fe42f393093c34ed5f3267d8e85f17750 100755
  3--- a/tools/upgrade.sh
  4+++ b/tools/upgrade.sh
  5@@ -15,11 +15,98 @@ cd "$ZSH"
  6 # Use colors, but only if connected to a terminal
  7 # and that terminal supports them.
  8 
  9+# The [ -t 1 ] check only works when the function is not called from
 10+# a subshell (like in `$(...)` or `(...)`, so this hack redefines the
 11+# function at the top level to always return false when stdout is not
 12+# a tty.
 13+if [ -t 1 ]; then
 14+  is_tty() {
 15+    true
 16+  }
 17+else
 18+  is_tty() {
 19+    false
 20+  }
 21+fi
 22+
 23+# This function uses the logic from supports-hyperlinks[1][2], which is
 24+# made by Kat Marchán (@zkat) and licensed under the Apache License 2.0.
 25+# [1] https://github.com/zkat/supports-hyperlinks
 26+# [2] https://crates.io/crates/supports-hyperlinks
 27+#
 28+# Copyright (c) 2021 Kat Marchán
 29+#
 30+# Licensed under the Apache License, Version 2.0 (the "License");
 31+# you may not use this file except in compliance with the License.
 32+# You may obtain a copy of the License at
 33+#
 34+#     http://www.apache.org/licenses/LICENSE-2.0
 35+#
 36+# Unless required by applicable law or agreed to in writing, software
 37+# distributed under the License is distributed on an "AS IS" BASIS,
 38+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 39+# See the License for the specific language governing permissions and
 40+# limitations under the License.
 41+supports_hyperlinks() {
 42+  # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass)
 43+  if [ -n "$FORCE_HYPERLINK" ]; then
 44+    [ "$FORCE_HYPERLINK" != 0 ]
 45+    return $?
 46+  fi
 47+
 48+  # If stdout is not a tty, it doesn't support hyperlinks
 49+  is_tty || return 1
 50+
 51+  # DomTerm terminal emulator (domterm.org)
 52+  if [ -n "$DOMTERM" ]; then
 53+    return 0
 54+  fi
 55+
 56+  # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc)
 57+  if [ -n "$VTE_VERSION" ]; then
 58+    [ $VTE_VERSION -ge 5000 ]
 59+    return $?
 60+  fi
 61+
 62+  # If $TERM_PROGRAM is set, these terminals support hyperlinks
 63+  case "$TERM_PROGRAM" in
 64+  Hyper|iTerm.app|terminology|WezTerm) return 0 ;;
 65+  esac
 66+
 67+  # kitty supports hyperlinks
 68+  if [ "$TERM" = xterm-kitty ]; then
 69+    return 0
 70+  fi
 71+
 72+  # Windows Terminal or Konsole also support hyperlinks
 73+  if [ -n "$WT_SESSION" ] || [ -n "$KONSOLE_VERSION" ]; then
 74+    return 0
 75+  fi
 76+
 77+  return 1
 78+}
 79+
 80+fmt_link() {
 81+  # $1: text, $2: url, $3: fallback mode
 82+  if supports_hyperlinks; then
 83+    printf '\033]8;;%s\a%s\033]8;;\a\n' "$2" "$1"
 84+    return
 85+  fi
 86+
 87+  case "$3" in
 88+  --text) printf '%s\n' "$1" ;;
 89+  --url|*) fmt_underline "$2" ;;
 90+  esac
 91+}
 92+
 93+fmt_underline() {
 94+  is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*"
 95+}
 96+
 97 setopt typeset_silent
 98 typeset -a RAINBOW
 99-typeset RED GREEN YELLOW BLUE BOLD DIM UNDER RESET
100 
101-if [ -t 1 ]; then
102+if is_tty; then
103   RAINBOW=(
104     "$(printf '\033[38;5;196m')"