#!/bin/sh # Written by Aetnaeus. # Source: https://github.com/lemnos/theme.sh. # Licensed under the WTFPL provided this notice is preserved. # Find a broken theme? Want to add a missing one? PRs are welcome. themes=$(cat < /etc/symlink", fgesc(a[6]), fgesc(fg), bgesc(bg))) while(nr > 0) puts("") printf "\x1b[0m" } ' } preview2() { printf '\033[30mColor 0\n' printf '\033[31mColor 1\n' printf '\033[32mColor 2\n' printf '\033[33mColor 3\n' printf '\033[34mColor 4\n' printf '\033[35mColor 5\n' printf '\033[36mColor 6\n' printf '\033[37mColor 7\n' printf '\033[90mColor 8\n' printf '\033[91mColor 9\n' printf '\033[92mColor 10\n' printf '\033[93mColor 11\n' printf '\033[94mColor 12\n' printf '\033[95mColor 13\n' printf '\033[96mColor 14\n' printf '\033[97mColor 15\n' printf '\n\033[0m' printf '# ls --color -lF\n' printf ' total 4\n' printf ' -rw-r--r-- 1 user user 0 Jan 0 02:39 file\n' printf ' drwxr-xr-x 2 user user 4096 Jan 0 02:39 \033[01;34mdir/\033[0m\n' printf ' -rwxr-xr-x 1 user user 0 Jan 0 02:39 \033[01;32mexecutable\033[0m*\n' printf ' lrwxrwxrwx 1 user user 15 Jan 0 02:40 \033[01;36msymlink\033[0m -> /etc/symlink\n' printf '\033[0m' } apply() { echo "$themes"| awk -F": " -v target="$1" ' function tmuxesc(s) { return sprintf("\033Ptmux;\033%s\033\\", s) } function normalize_term() { # Term detection voodoo if(ENVIRON["TERM_PROGRAM"] == "iTerm.app") term="iterm" else if(ENVIRON["TMUX"]) { "tmux display-message -p \"#{client_termname}\"" | getline term is_tmux++ } else term=ENVIRON["TERM"] } BEGIN { normalize_term() if(term == "iterm") { bgesc="\033]Ph%s\033\\" fgesc="\033]Pg%s\033\\" colesc="\033]P%x%s\033\\" curesc="\033]Pl%s\033\\" } else if(term ~ /st-.*/) { fgesc="\033]4;7;#%s\007" bgesc="\033]4;0;#%s\007" colesc="\033]4;%d;#%s\007" curesc="\033]4;256;#%s\007" } else { #Terms that play nice :) fgesc="\033]10;#%s\007" bgesc="\033]11;#%s\007" curesc="\033]12;#%s\007" colesc="\033]4;%d;#%s\007" } if(is_tmux) { fgesc=tmuxesc(fgesc) bgesc=tmuxesc(bgesc) curesc=tmuxesc(curesc) colesc=tmuxesc(colesc) } } $0 == target {found++} found && /^foreground:/ {fg=$2} found && /^background:/ {bg=$2} found && /^[0-9]+:/ {colors[int($1)]=$2} found && /^cursorColor:/ {cursor=$2} found && /^ *$/ { exit } END { if(found) { for(c in colors) { if(!(term ~ /st-.*/ && (c == 0 || c == 7))) printf colesc, c, substr(colors[c], 2) > "/dev/tty" } printf fgesc, substr(fg, 2) > "/dev/tty" printf bgesc, substr(bg, 2) > "/dev/tty" printf curesc, substr(cursor, 2) > "/dev/tty" f=ENVIRON["THEME_HISTFILE"] if(f) { while((getline < f) > 0) if($0 != target) out = out $0 "\n" close(f) out = out target print out > f } } } ' } list() { echo "$themes"| awk ' BEGIN { f = ENVIRON["THEME_HISTFILE"] if(f) { while((getline < f) > 0) { mru[nmru++] = $0 seen[$0] = 1 } } s = 1 } /^ *$/ { s=1; next } s { if(!seen[$0]) print s = 0 } END { for(i = 0;i < nmru;i++) print mru[i] } ' } if [ -z "$1" ]; then echo "Usage: $(basename "$0") [-l|--list] [-i|--interactive] [-i2|--interactive2] [-r|--random] " exit fi case "$1" in -i2|--interactive2) command -v fzf > /dev/null 2>&1 || { echo "ERROR: -i requires fzf" >&2; exit 1; } "$0" -l|fzf\ --tac\ --bind "enter:execute-silent($0 {})"\ --bind "down:down+execute-silent(THEME_HISTFILE= $0 {})"\ --bind "up:up+execute-silent(THEME_HISTFILE= $0 {})"\ --bind "change:execute-silent(THEME_HISTFILE= $0 {})"\ --bind "ctrl-c:execute($0 {};echo {})+abort"\ --bind "esc:execute($0 {};echo {})+abort"\ --no-sort\ --preview "$0 --preview2" ;; -r|--random) theme=$($0 -l|sort -R|head -n1) $0 "$theme" echo "Theme: $theme" ;; -i|--interactive) command -v fzf > /dev/null 2>&1 || { echo "ERROR: -i requires fzf" >&2; exit 1; } if [ -z "$COLORTERM" ]; then echo "This does not appear to be a truecolor terminal, try -i2 instead or set COLORTERM if your terminal has truecolor support." exit 1 else "$0" -l|fzf\ --tac\ --bind "ctrl-c:execute(echo {})+abort"\ --bind "esc:execute(echo {})+abort"\ --bind "enter:execute-silent($0 {})"\ --no-sort\ --preview "$0 --preview {}" fi ;; -l|--list) list ;; --preview2) preview2 "$2" ;; --preview) preview "$2" ;; *) apply "$1" ;; esac