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>
master
Marc Cornellà 3 years ago committed by GitHub
parent 5bf7f9c833
commit 95d0c4b603
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 129
      lib/theme-and-appearance.zsh

@ -1,66 +1,93 @@
# ls colors # Sets color variable such as $fg, $bg, $color and $reset_color
autoload -U colors && colors autoload -U colors && colors
# Enable ls colors # Expand variables and commands in PROMPT variables
export LSCOLORS="Gxfxcxdxbxegedabagacad" setopt prompt_subst
# TODO organise this chaotic logic # Prompt function theming defaults
ZSH_THEME_GIT_PROMPT_PREFIX="git:(" # Beginning of the git prompt, before the branch name
ZSH_THEME_GIT_PROMPT_SUFFIX=")" # End of the git prompt
ZSH_THEME_GIT_PROMPT_DIRTY="*" # Text to display if the branch is dirty
ZSH_THEME_GIT_PROMPT_CLEAN="" # Text to display if the branch is clean
ZSH_THEME_RUBY_PROMPT_PREFIX="("
ZSH_THEME_RUBY_PROMPT_SUFFIX=")"
if [[ "$DISABLE_LS_COLORS" != "true" ]]; then
if [[ -d "$ZSH" ]]; then # Use diff --color if available
_test_dir="$ZSH" if command diff --color /dev/null{,} &>/dev/null; then
else function diff {
_test_dir="." command diff --color "$@"
}
fi fi
# Don't set ls coloring if disabled
[[ "$DISABLE_LS_COLORS" != true ]] || return 0
function test-ls-args {
local cmd="$1" # ls, gls, colorls, ...
local args="${@[2,-1]}" # arguments except the first one
command "$cmd" "$args" /dev/null &>/dev/null
}
# Find the option for using colors in ls, depending on the version # Find the option for using colors in ls, depending on the version
if [[ "$OSTYPE" == netbsd* ]]; then case "$OSTYPE" in
# On NetBSD, test if "gls" (GNU ls) is installed (this one supports colors); netbsd*)
# On NetBSD, test if `gls` (GNU ls) is installed (this one supports colors);
# otherwise, leave ls as is, because NetBSD's ls doesn't support -G # otherwise, leave ls as is, because NetBSD's ls doesn't support -G
gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty' test-ls-args gls --color && alias ls='gls --color=tty'
elif [[ "$OSTYPE" == openbsd* ]]; then ;;
# On OpenBSD, "gls" (ls from GNU coreutils) and "colorls" (ls from base, openbsd*)
# with color and multibyte support) are available from ports. "colorls" # On OpenBSD, `gls` (ls from GNU coreutils) and `colorls` (ls from base,
# will be installed on purpose and can't be pulled in by installing # with color and multibyte support) are available from ports.
# coreutils, so prefer it to "gls". # `colorls` will be installed on purpose and can't be pulled in by installing
gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty' # coreutils (which might be installed for ), so prefer it to `gls`.
colorls -G -d "$_test_dir" &>/dev/null && alias ls='colorls -G' test-ls-args gls --color && alias ls='gls --color=tty'
elif [[ "$OSTYPE" == (darwin|freebsd)* ]]; then test-ls-args colorls -G && alias ls='colorls -G'
# this is a good alias, it works by default just using $LSCOLORS ;;
ls -G "$_test_dir" &>/dev/null && alias ls='ls -G' (darwin|freebsd)*)
# This alias works by default just using $LSCOLORS
# only use coreutils ls if there is a dircolors customization present ($LS_COLORS or .dircolors file) test-ls-args ls -G && alias ls='ls -G'
# otherwise, gls will use the default color scheme which is ugly af # Only use GNU ls if installed and there are user defaults for $LS_COLORS,
[[ -n "$LS_COLORS" || -f "$HOME/.dircolors" ]] && gls --color -d "$_test_dir" &>/dev/null && alias ls='gls --color=tty' # as the default coloring scheme is not very pretty
else [[ -n "$LS_COLORS" || -f "$HOME/.dircolors" ]] \
# For GNU ls, we use the default ls color theme. They can later be overwritten by themes. && test-ls-args gls --color \
if [[ -z "$LS_COLORS" ]]; then && alias ls='gls --color=tty'
(( $+commands[dircolors] )) && eval "$(dircolors -b)" ;;
*)
if test-ls-args ls --color; then
alias ls='ls --color=tty'
elif test-ls-args ls -G; then
alias ls='ls -G'
fi fi
;;
esac
ls --color -d "$_test_dir" &>/dev/null && alias ls='ls --color=tty' || { ls -G "$_test_dir" &>/dev/null && alias ls='ls -G' } unfunction test-ls-args
# Take advantage of $LS_COLORS for completion as well.
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" # Default coloring for BSD-based ls
export LSCOLORS="Gxfxcxdxbxegedabagacad"
# Default coloring for GNU-based ls
if [[ -z "$LS_COLORS" ]]; then
# Define LS_COLORS via dircolors if available. Otherwise, set a default
# equivalent to LSCOLORS (generated via https://geoff.greer.fm/lscolors)
if (( $+commands[dircolors] )); then
[[ -f "$HOME/.dircolors" ]] \
&& source <(dircolors -b "$HOME/.dircolors") \
|| source <(dircolors -b)
else
export LS_COLORS="di=34:ln=35:so=32:pi=33:ex=31:bd=34;46:cd=34;43:su=37;41:sg=30;43:tw=30;42:ow=34;42:"
fi fi
fi fi
# enable diff color if possible. # Take advantage of $LS_COLORS for completion as well.
if command diff --color /dev/null /dev/null &>/dev/null; then function omz_set_completion_colors {
function color-diff { zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
command diff --color $@ add-zsh-hook -d precmd omz_set_completion_colors
unfunction omz_set_completion_colors
} }
alias diff="color-diff"
compdef _diff color-diff # compdef is already loaded by this point
fi
setopt prompt_subst
[[ -n "$WINDOW" ]] && SCREEN_NO="%B$WINDOW%b " || SCREEN_NO=""
# git theming default: Variables for theming the git info prompt autoload -Uz add-zsh-hook
ZSH_THEME_GIT_PROMPT_PREFIX="git:(" # Prefix at the very beginning of the prompt, before the branch name add-zsh-hook precmd omz_set_completion_colors
ZSH_THEME_GIT_PROMPT_SUFFIX=")" # At the very end of the prompt
ZSH_THEME_GIT_PROMPT_DIRTY="*" # Text to display if the branch is dirty
ZSH_THEME_GIT_PROMPT_CLEAN="" # Text to display if the branch is clean
ZSH_THEME_RUBY_PROMPT_PREFIX="("
ZSH_THEME_RUBY_PROMPT_SUFFIX=")"

Loading…
Cancel
Save