From be71ccc42ead76a3f2fbd2646f3786fac4e58903 Mon Sep 17 00:00:00 2001 From: Serge Tkatchouk Date: Thu, 5 Aug 2021 01:05:06 +0800 Subject: [PATCH] Add support for iTerm on MacOS --- cmd/hashcolor/main.go | 9 +++++++-- examples/cssh | 13 +++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/hashcolor/main.go b/cmd/hashcolor/main.go index 3c3cee0..024ddb6 100644 --- a/cmd/hashcolor/main.go +++ b/cmd/hashcolor/main.go @@ -11,11 +11,16 @@ import ( func hex(c color.Color) string { rgba := color.RGBAModel.Convert(c).(color.RGBA) - return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B) + return fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B) } func main() { c := utils.New(strings.Join(os.Args[1:], " ")) t, s := utils.Tint(c), utils.Shade(c) - fmt.Printf("\033]10;%s\007\033]11;%s\007", hex(t), hex(s)) + term := os.Getenv("TERM_PROGRAM") + if (term == "iTerm.app") { + fmt.Printf("\033]1337;SetColors=fg=rgb:%s\033\\\033]1337;SetColors=bg=rgb:%s\033\\", hex(t), hex(s)) + } else { + fmt.Printf("\033]10;#%s\007\033]11;#%s\007", hex(t), hex(s)) + } } diff --git a/examples/cssh b/examples/cssh index 7b051b3..fdfaa22 100755 --- a/examples/cssh +++ b/examples/cssh @@ -10,6 +10,15 @@ if ! type hashcolor &>/dev/null || ! [[ -t 0 && -t 1 && -t 2 ]]; then exec ssh "${@}" fi +set_colors() { + local _fg="${1}" _bg="${2}" + if [[ "${TERM_PROGRAM}" == 'iTerm.app' ]]; then + echo -e "\033]1337;SetColors=fg=rgb:${_fg}\033\\\033]1337;SetColors=bg=rgb:${_bg}\033\\" + else + echo -e "\033]10;${_fg}\007\033]11;${_bg}\007" + fi +} + ## Set your FG/BG colors here: DEFAULT_BG="#000000" DEFAULT_FG="#EAEAEA" @@ -24,8 +33,8 @@ if [[ "${PARENT_COMMAND}" =~ sh ]]; then ## Capture SSH exit code here, to use it as our own later: SSH_EC="$?" ## Reset terminal colors to defaults: - echo -e "\033]10;${DEFAULT_FG}\007\033]11;${DEFAULT_BG}\007" + set_colors "${DEFAULT_FG}" "${DEFAULT_BG}" exit "${SSH_EC}" else exec ssh "${SSH_OPTIONS[@]}" "${@}" -fi +fi