Skip to content

Commit

Permalink
Add support for iTerm on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
spijet committed Aug 4, 2021
1 parent 138ede6 commit be71ccc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/hashcolor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
13 changes: 11 additions & 2 deletions examples/cssh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit be71ccc

Please sign in to comment.