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
import argparse
import os
import sys
import textwrap
from subprocess import Popen, PIPE, check_output
from typing import Tuple
@ -42,6 +43,17 @@ def get_terminal_profile(force=False):
if not force and TERM_VAR in os.environ:
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")
if term_program == "Apple_Terminal":
tty = check_output(["tty"]).strip()
@ -53,7 +65,10 @@ def get_terminal_profile(force=False):
raise SystemError("Could not get results from applescript")
return stdout
elif term_program == "iTerm.app":
if "ITERM_PROFILE" in os.environ:
return os.environ["ITERM_PROFILE"]
else:
raise ValueError("Using iTerm but no profile found")
elif term_program == "Alacritty":
return "Alacritty"
@ -202,6 +217,12 @@ def print_env(var: str, val: str, export=False, fish=False):
print(f'set -g {var} "{val}";')
def is_ssh() -> bool:
if os.environ.get("SSH_TTY"):
return True
return False
if __name__ == "__main__":
args = parse_args()
if args.print_term:

View File

@ -6,6 +6,8 @@ set -gx XDG_CACHE_HOME "$HOME/.cache"
# Increase memory sizes for java using Ant
set -gx ANT_OPTS "-Xmx2048m -Xms512m"
# Interractive env variables
if status --is-interactive
# FZF
if type -q rg
set -gx FZF_DEFAULT_COMMAND 'rg --files'
@ -24,3 +26,4 @@ set -gx FZF_DEFAULT_OPTS "
# Export colors
eval ($HOME/bin/derive_colors.py --export --fish)
end