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.
This commit is contained in:
Fernando Schauenburg 2020-12-16 18:21:51 +01:00
parent 42d7fdf143
commit 655884c3f1

View file

@ -239,23 +239,15 @@ alias dark='_update_colors dark'
# In tmux, OSC sequences can be forwarded to the underlying terminal by: # In tmux, OSC sequences can be forwarded to the underlying terminal by:
# * Wrapping the sequence with `DCS tmux; <sequence> ST` # * Wrapping the sequence with `DCS tmux; <sequence> ST`
# * Replacing ESC in <sequence> with ESC ESC. # * Replacing ESC in <sequence> 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 # Either ST or BEL (\a) can be used to end an OSC sequence. However, tmux
# requires ST to end its wrapping DCS. # requires ST to end its wrapping DCS.
_osc_end() {
if [ -n "$TMUX" ]; then if [ -n "$TMUX" ]; then
printf "\a\e\x5c" # 0x5c == backslash _OSC=$(printf "\ePtmux;\e\e]")
_ST=$(printf "\a\e\\")
else else
printf "\a" _OSC=$(printf "\e]")
_ST=$(printf "\a")
fi fi
}
_update_colors() { _update_colors() {
export BACKGROUND="$1" export BACKGROUND="$1"
@ -294,9 +286,7 @@ _set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor
local fg="$1" bg="$2" cursor="$3" local fg="$1" bg="$2" cursor="$3"
local key rgb local key rgb
while read -r key rgb; do while read -r key rgb; do
_osc_start printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "${rgb}" "$_ST"
printf "1337;SetColors=%s=%s" "$key" "${rgb}"
_osc_end
done <<EOL done <<EOL
black $Base02_RGB black $Base02_RGB
red $Red_RGB red $Red_RGB
@ -323,9 +313,7 @@ _set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor
curbg $cursor curbg $cursor
link $Cyan_RGB link $Cyan_RGB
EOL EOL
_osc_start printf "%s1337;CursorShape=2%s" "$_OSC" "$_ST" # set cursor shape to underline
printf "1337;CursorShape=2" # set cursor shape to underline
_osc_end
} }
# Documentation for xterm at https://www.xfree86.org/4.8.0/ctlseqs.html # Documentation for xterm at https://www.xfree86.org/4.8.0/ctlseqs.html
@ -338,9 +326,7 @@ _set_xterm_colors() { # $1 foreground, $2 background, $3 cursor
# where Ps -> 4;c;spec # where Ps -> 4;c;spec
# c -> index of the ANSI color to change [0..15] # c -> index of the ANSI color to change [0..15]
# spec -> RGB value of color as rgb:RR/GG/BB # spec -> RGB value of color as rgb:RR/GG/BB
_osc_start printf "%s4;%s;rgb:%s/%s/%s%s" "$_OSC" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST"
printf "4;%s;rgb:%s/%s/%s" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}"
_osc_end
done <<EOL done <<EOL
0 $Base02_RGB 0 $Base02_RGB
1 $Red_RGB 1 $Red_RGB
@ -363,9 +349,7 @@ EOL
# Send sequence OSC Ps ; Pt BEL # Send sequence OSC Ps ; Pt BEL
# where Ps -> foreground (10), background (11), or cursor (12) # where Ps -> foreground (10), background (11), or cursor (12)
# Pt -> RGB value of color as rgb:RR/GG/BB # Pt -> RGB value of color as rgb:RR/GG/BB
_osc_start printf "%s%s;rgb:%s/%s/%s%s" "$_OSC" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST"
printf "%s;rgb:%s/%s/%s" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}"
_osc_end
done <<EOL done <<EOL
10 $fg 10 $fg
11 $bg 11 $bg