95d0c4b603e0c880bcf20bc9211b2162e94ed925

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

Message

refactor(theme-and-appearance): reorganize and clean up logic (#11529)

Co-authored-by: Andrew Janke <janke@pobox.com>
Co-authored-by: Marcelo Parada <marcelo.parada@axoninsight.com>
Co-authored-by: Uy Ha <hchanuy@gmail.com>
Co-authored-by: Valentin Uveges <valentin.uveges@gmail.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/theme-and-appearance.zsh b/lib/theme-and-appearance.zsh
  2index 208ab9ce5414325acb5a9165c66ba76c3ae3d52e..985d3bc11b5d1273205f27d2156bc855de19bdf0 100644
  3--- a/lib/theme-and-appearance.zsh
  4+++ b/lib/theme-and-appearance.zsh
  5@@ -1,66 +1,93 @@
  6-# ls colors
  7+# Sets color variable such as $fg, $bg, $color and $reset_color
  8 autoload -U colors && colors
  9 
 10-# Enable ls colors
 11-export LSCOLORS="Gxfxcxdxbxegedabagacad"
 12+# Expand variables and commands in PROMPT variables
 13+setopt prompt_subst
 14 
 15-# TODO organise this chaotic logic
 16+# Prompt function theming defaults
 17+ZSH_THEME_GIT_PROMPT_PREFIX="git:("   # Beginning of the git prompt, before the branch name
 18+ZSH_THEME_GIT_PROMPT_SUFFIX=")"       # End of the git prompt
 19+ZSH_THEME_GIT_PROMPT_DIRTY="*"        # Text to display if the branch is dirty
 20+ZSH_THEME_GIT_PROMPT_CLEAN=""         # Text to display if the branch is clean
 21+ZSH_THEME_RUBY_PROMPT_PREFIX="("
 22+ZSH_THEME_RUBY_PROMPT_SUFFIX=")"
 23 
 24-if [[ "$DISABLE_LS_COLORS" != "true" ]]; then
 25-  if [[ -d "$ZSH" ]]; then
 26-    _test_dir="$ZSH"
 27-  else
 28-    _test_dir="."
 29-  fi
 30-  # Find the option for using colors in ls, depending on the version
 31-  if [[ "$OSTYPE" == netbsd* ]]; then
 32-    # On NetBSD, test if "gls" (GNU ls) is installed (this one supports colors);
 33-    # otherwise, leave ls as is, because NetBSD's ls doesn't support -G
 34-    gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty'
 35-  elif [[ "$OSTYPE" == openbsd* ]]; then
 36-    # On OpenBSD, "gls" (ls from GNU coreutils) and "colorls" (ls from base,
 37-    # with color and multibyte support) are available from ports.  "colorls"
 38-    # will be installed on purpose and can't be pulled in by installing
 39-    # coreutils, so prefer it to "gls".
 40-    gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty'
 41-    colorls -G -d "$_test_dir" &>/dev/null && alias ls='colorls -G'
 42-  elif [[ "$OSTYPE" == (darwin|freebsd)* ]]; then
 43-    # this is a good alias, it works by default just using $LSCOLORS
 44-    ls -G "$_test_dir" &>/dev/null && alias ls='ls -G'
 45 
 46-    # only use coreutils ls if there is a dircolors customization present ($LS_COLORS or .dircolors file)
 47-    # otherwise, gls will use the default color scheme which is ugly af
 48-    [[ -n "$LS_COLORS" || -f "$HOME/.dircolors" ]] && gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty'
 49-  else
 50-    # For GNU ls, we use the default ls color theme. They can later be overwritten by themes.
 51-    if [[ -z "$LS_COLORS" ]]; then
 52-      (( $+commands[dircolors] )) && eval "$(dircolors -b)"
 53+# Use diff --color if available
 54+if command diff --color /dev/null{,} &>/dev/null; then
 55+  function diff {
 56+    command diff --color "$@"
 57+  }
 58+fi
 59+
 60+
 61+# Don't set ls coloring if disabled
 62+[[ "$DISABLE_LS_COLORS" != true ]] || return 0
 63+
 64+function test-ls-args {
 65+  local cmd="$1"          # ls, gls, colorls, ...
 66+  local args="${@[2,-1]}" # arguments except the first one
 67+  command "$cmd" "$args" /dev/null &>/dev/null
 68+}
 69+
 70+# Find the option for using colors in ls, depending on the version
 71+case "$OSTYPE" in
 72+  netbsd*)
 73+    # On NetBSD, test if `gls` (GNU ls) is installed (this one supports colors);
 74+    # otherwise, leave ls as is, because NetBSD's ls doesn't support -G
 75+    test-ls-args gls --color && alias ls='gls --color=tty'
 76+    ;;
 77+  openbsd*)
 78+    # On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base,
 79+    # with color and multibyte support) are available from ports.
 80+    # `colorls` will be installed on purpose and can't be pulled in by installing
 81+    # coreutils (which might be installed for ), so prefer it to `gls`.
 82+    test-ls-args gls --color && alias ls='gls --color=tty'
 83+    test-ls-args colorls -G && alias ls='colorls -G'
 84+    ;;
 85+  (darwin|freebsd)*)
 86+    # This alias works by default just using $LSCOLORS
 87+    test-ls-args ls -G && alias ls='ls -G'
 88+    # Only use GNU ls if installed and there are user defaults for $LS_COLORS,
 89+    # as the default coloring scheme is not very pretty
 90+    [[ -n "$LS_COLORS" || -f "$HOME/.dircolors" ]] \
 91+      && test-ls-args gls --color \
 92+      && alias ls='gls --color=tty'
 93+    ;;
 94+  *)
 95+    if test-ls-args ls --color; then
 96+      alias ls='ls --color=tty'
 97+    elif test-ls-args ls -G; then
 98+      alias ls='ls -G'
 99     fi
100+    ;;
101+esac
102 
103-    ls --color -d "$_test_dir" &>/dev/null && alias ls='ls --color=tty' || { ls -G "$_test_dir" &>/dev/null && alias ls='ls -G' }
104+unfunction test-ls-args