Improve color detection over ssh

This commit is contained in:
ViViDboarder 2020-10-23 11:45:34 -07:00
parent 06e3aea526
commit 96a2dc9381
2 changed files with 42 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
import argparse import argparse
import os import os
import sys
import textwrap import textwrap
from subprocess import Popen, PIPE, check_output from subprocess import Popen, PIPE, check_output
from typing import Tuple from typing import Tuple
@ -42,6 +43,17 @@ def get_terminal_profile(force=False):
if not force and TERM_VAR in os.environ: if not force and TERM_VAR in os.environ:
return os.environ[TERM_VAR] return os.environ[TERM_VAR]
# If over SSH, quit. Colorschemes should instead be forwarded
if is_ssh():
print(
("Warning: Cannot derive colors. "
"SSH sessions won't allow detecting the terminal profile."
f" Instead forward {TERM_VAR} from your source machine."),
file=sys.stderr,
)
exit(0)
term_program = os.environ.get("TERM_PROGRAM") term_program = os.environ.get("TERM_PROGRAM")
if term_program == "Apple_Terminal": if term_program == "Apple_Terminal":
tty = check_output(["tty"]).strip() tty = check_output(["tty"]).strip()
@ -53,7 +65,10 @@ def get_terminal_profile(force=False):
raise SystemError("Could not get results from applescript") raise SystemError("Could not get results from applescript")
return stdout return stdout
elif term_program == "iTerm.app": elif term_program == "iTerm.app":
return os.environ["ITERM_PROFILE"] if "ITERM_PROFILE" in os.environ:
return os.environ["ITERM_PROFILE"]
else:
raise ValueError("Using iTerm but no profile found")
elif term_program == "Alacritty": elif term_program == "Alacritty":
return "Alacritty" return "Alacritty"
@ -202,6 +217,12 @@ def print_env(var: str, val: str, export=False, fish=False):
print(f'set -g {var} "{val}";') print(f'set -g {var} "{val}";')
def is_ssh() -> bool:
if os.environ.get("SSH_TTY"):
return True
return False
if __name__ == "__main__": if __name__ == "__main__":
args = parse_args() args = parse_args()
if args.print_term: if args.print_term:

View File

@ -6,21 +6,24 @@ set -gx XDG_CACHE_HOME "$HOME/.cache"
# Increase memory sizes for java using Ant # Increase memory sizes for java using Ant
set -gx ANT_OPTS "-Xmx2048m -Xms512m" set -gx ANT_OPTS "-Xmx2048m -Xms512m"
# FZF # Interractive env variables
if type -q rg if status --is-interactive
set -gx FZF_DEFAULT_COMMAND 'rg --files' # FZF
# set -gx FZF_DEFAULT_COMMAND 'rg --files --no-ignore-vcs --hidden' if type -q rg
else if type -q ag set -gx FZF_DEFAULT_COMMAND 'rg --files'
set -gx FZF_DEFAULT_COMMAND 'ag -g ""' # set -gx FZF_DEFAULT_COMMAND 'rg --files --no-ignore-vcs --hidden'
end else if type -q ag
if [ -n "$FZF_DEFAULT_COMMAND" ] set -gx FZF_DEFAULT_COMMAND 'ag -g ""'
set -gx FZF_CTRL_T_COMMAND "$FZF_DEFAULT_COMMAND \$dir" end
end if [ -n "$FZF_DEFAULT_COMMAND" ]
set -gx FZF_DEFAULT_OPTS " set -gx FZF_CTRL_T_COMMAND "$FZF_DEFAULT_COMMAND \$dir"
--preview-window=:hidden end
--preview='__fzf_preview {}' set -gx FZF_DEFAULT_OPTS "
--bind '?:toggle-preview' --preview-window=:hidden
" --preview='__fzf_preview {}'
--bind '?:toggle-preview'
"
# Export colors # Export colors
eval ($HOME/bin/derive_colors.py --export --fish) eval ($HOME/bin/derive_colors.py --export --fish)
end