Skip to content

Commit

Permalink
Improved term detection + make history opt out.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemnos committed Oct 22, 2021
1 parent f35c922 commit f8c28b4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ E.G
sudo curl -o /usr/bin/theme.sh 'https://raw.githubusercontent.com/lemnos/theme.sh/master/theme.sh' && sudo chmod +x /usr/bin/theme.sh
```

or (OSX)

```
sudo curl -o /usr/local/bin/theme.sh 'https://raw.githubusercontent.com/lemnos/theme.sh/master/theme.sh' && sudo chmod +x /usr/local/bin/theme.sh
```

# Usage


Expand Down Expand Up @@ -65,23 +71,23 @@ in your `~/.bashrc`.

# Configuration

If `$THEME_HISTFILE` is set then the path is contains will be used
to store recently selected themes.

## Recommended `~/.bashrc`

To load the most recently selected theme automatically you can put

```
if command -v theme.sh > /dev/null; then
export THEME_HISTFILE=~/.theme_history
[ -e "$THEME_HISTFILE" ] && theme.sh "$(theme.sh -l|tail -n1)"
[ -e ~/.theme_history ] && theme.sh "$(theme.sh -l|tail -n1)"
# Optional
bind -x '"\x0f":"theme.sh $(theme.sh -l|tail -n2|head -n1)"' #Binds C-o to the previously active theme.
alias th='theme.sh -i'
# Interactively load a light theme
alias thl='theme.sh --light -i'
# Interactively load a dark theme
alias thd='theme.sh --dark -i'
fi
```
Expand Down
42 changes: 27 additions & 15 deletions theme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ apply() {
term="iterm"
else if(ENVIRON["TMUX"]) {
"tmux display-message -p \"#{client_termname}\"" | getline term
"tmux display-message -p \"#{client_termtype}\"" | getline termname
if(substr(termname, 1, 5) == "iTerm")
term="iterm"
is_tmux++
} else
term=ENVIRON["TERM"]
Expand Down Expand Up @@ -301,21 +305,31 @@ apply() {
printf bgesc, substr(bg, 2) > "/dev/tty"
printf curesc, substr(cursor, 2) > "/dev/tty"
f=ENVIRON["THEME_HISTFILE"]
if(f) {
while((getline < f) > 0)
histfile=ENVIRON["HOME"]"/.theme_history"
inhibit_hist=ENVIRON["INHIBIT_THEME_HIST"]
if(!inhibit_hist) {
while((getline < histfile) > 0)
if($0 != target)
out = out $0 "\n"
close(f)
close(histfile)
out = out target
print out > f
print out > histfile
}
}
}
' < "$0"
}

isColorTerm() {
if [ -z "$TMUX" ]; then
[ ! -z "$COLORTERM" ]
else
tmux display-message -p '#{client_termfeatures}'|grep -q 256
fi
}

list() {
case "$filterFlag" in
--light) filter=2 ;;
Expand All @@ -325,12 +339,10 @@ list() {

awk -v filter="$filter" -F": " '
BEGIN {
f = ENVIRON["THEME_HISTFILE"]
if(f) {
while((getline < f) > 0) {
mru[nmru++] = $0
mruIndex[$0] = 1
}
histfile=ENVIRON["HOME"]"/.theme_history"
while((getline < histfile) > 0) {
mru[nmru++] = $0
mruIndex[$0] = 1
}
}
Expand Down Expand Up @@ -390,9 +402,9 @@ case "$1" in
"$0" $filterFlag -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 "down:down+execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "up:up+execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "change:execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "ctrl-c:execute($0 {};echo {})+abort"\
--bind "esc:execute($0 {};echo {})+abort"\
--no-sort\
Expand All @@ -405,7 +417,7 @@ case "$1" in
;;
-i|--interactive)
command -v fzf > /dev/null 2>&1 || { echo "ERROR: -i requires fzf" >&2; exit 1; }
if [ -z "$COLORTERM" ]; then
if ! isColorTerm; 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
Expand Down
42 changes: 27 additions & 15 deletions theme_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ apply() {
term="iterm"
else if(ENVIRON["TMUX"]) {
"tmux display-message -p \"#{client_termname}\"" | getline term
"tmux display-message -p \"#{client_termtype}\"" | getline termname
if(substr(termname, 1, 5) == "iTerm")
term="iterm"
is_tmux++
} else
term=ENVIRON["TERM"]
Expand Down Expand Up @@ -301,21 +305,31 @@ apply() {
printf bgesc, substr(bg, 2) > "/dev/tty"
printf curesc, substr(cursor, 2) > "/dev/tty"
f=ENVIRON["THEME_HISTFILE"]
if(f) {
while((getline < f) > 0)
histfile=ENVIRON["HOME"]"/.theme_history"
inhibit_hist=ENVIRON["INHIBIT_THEME_HIST"]
if(!inhibit_hist) {
while((getline < histfile) > 0)
if($0 != target)
out = out $0 "\n"
close(f)
close(histfile)
out = out target
print out > f
print out > histfile
}
}
}
' < "$0"
}

isColorTerm() {
if [ -z "$TMUX" ]; then
[ ! -z "$COLORTERM" ]
else
tmux display-message -p '#{client_termfeatures}'|grep -q 256
fi
}

list() {
case "$filterFlag" in
--light) filter=2 ;;
Expand All @@ -325,12 +339,10 @@ list() {

awk -v filter="$filter" -F": " '
BEGIN {
f = ENVIRON["THEME_HISTFILE"]
if(f) {
while((getline < f) > 0) {
mru[nmru++] = $0
mruIndex[$0] = 1
}
histfile=ENVIRON["HOME"]"/.theme_history"
while((getline < histfile) > 0) {
mru[nmru++] = $0
mruIndex[$0] = 1
}
}
Expand Down Expand Up @@ -390,9 +402,9 @@ case "$1" in
"$0" $filterFlag -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 "down:down+execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "up:up+execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "change:execute-silent(INHIBIT_THEME_HIST=1 $0 {})"\
--bind "ctrl-c:execute($0 {};echo {})+abort"\
--bind "esc:execute($0 {};echo {})+abort"\
--no-sort\
Expand All @@ -405,7 +417,7 @@ case "$1" in
;;
-i|--interactive)
command -v fzf > /dev/null 2>&1 || { echo "ERROR: -i requires fzf" >&2; exit 1; }
if [ -z "$COLORTERM" ]; then
if ! isColorTerm; 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
Expand Down

0 comments on commit f8c28b4

Please sign in to comment.