From 655884c3f1dd968f67db8864114589429b52ea71 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Wed, 16 Dec 2020 18:21:51 +0100 Subject: [PATCH] bash: fix OSC sequences The function calls where somethimes too slow and the terminal timed out before accepting the entire escape sequence. By constructing the whole string before sending via printf this problem is avoided. I originally noticed this issue when using Alacritty on WSL, where apparently the communication via ConPTY can be a bit slow. --- roles/bash/files/profile | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/roles/bash/files/profile b/roles/bash/files/profile index e972eab..889a3d3 100644 --- a/roles/bash/files/profile +++ b/roles/bash/files/profile @@ -239,23 +239,15 @@ alias dark='_update_colors dark' # In tmux, OSC sequences can be forwarded to the underlying terminal by: # * Wrapping the sequence with `DCS tmux; ST` # * Replacing ESC in with ESC ESC. -_osc_start() { - if [ -n "$TMUX" ]; then - printf "\ePtmux;\e\e]" - else - printf "\e]" - fi -} - # Either ST or BEL (\a) can be used to end an OSC sequence. However, tmux # requires ST to end its wrapping DCS. -_osc_end() { - if [ -n "$TMUX" ]; then - printf "\a\e\x5c" # 0x5c == backslash - else - printf "\a" - fi -} +if [ -n "$TMUX" ]; then + _OSC=$(printf "\ePtmux;\e\e]") + _ST=$(printf "\a\e\\") +else + _OSC=$(printf "\e]") + _ST=$(printf "\a") +fi _update_colors() { export BACKGROUND="$1" @@ -294,9 +286,7 @@ _set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor local fg="$1" bg="$2" cursor="$3" local key rgb while read -r key rgb; do - _osc_start - printf "1337;SetColors=%s=%s" "$key" "${rgb}" - _osc_end + printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "${rgb}" "$_ST" done < 4;c;spec # c -> index of the ANSI color to change [0..15] # spec -> RGB value of color as rgb:RR/GG/BB - _osc_start - printf "4;%s;rgb:%s/%s/%s" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" - _osc_end + printf "%s4;%s;rgb:%s/%s/%s%s" "$_OSC" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST" done < foreground (10), background (11), or cursor (12) # Pt -> RGB value of color as rgb:RR/GG/BB - _osc_start - printf "%s;rgb:%s/%s/%s" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" - _osc_end + printf "%s%s;rgb:%s/%s/%s%s" "$_OSC" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST" done <