Skip to content

Commit 1cb891a

Browse files
committed
Add script to show reported session title.
1 parent 34b9cfb commit 1cb891a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/it2title.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
function ctrl_c() {
4+
stty "$saved_stty"
5+
exit 1
6+
}
7+
8+
# Read some bytes from stdin. Pass the number of bytes to read as the first argument.
9+
function read_bytes() {
10+
numbytes=$1
11+
dd bs=1 count=$numbytes 2>/dev/null
12+
}
13+
14+
function init() {
15+
# Make sure stdin and stdout are a tty.
16+
if [ ! -t 0 ] ; then
17+
exit 1
18+
fi
19+
if [ ! -t 1 ] ; then
20+
exit 1
21+
fi
22+
23+
# Save the tty's state.
24+
saved_stty=$(stty -g)
25+
26+
# Trap ^C to fix the tty.
27+
trap ctrl_c INT
28+
29+
# Enter raw mode and turn off echo so the terminal and I can chat quietly.
30+
stty -echo -icanon raw
31+
}
32+
33+
# Request the profile name
34+
function send_title_request() {
35+
printf '\e[20t'
36+
}
37+
38+
function read_osc() {
39+
boilerplate=$(read_bytes 2)
40+
result=''
41+
b=""
42+
tab=$(printf "\e")
43+
while :
44+
do
45+
last="$b"
46+
b=$(read_bytes 1)
47+
if [[ $last == $tab && $b == "\\" ]]
48+
then
49+
break
50+
elif [[ $last == $tab ]]
51+
then
52+
result="$result$last$b"
53+
elif [[ $b != $tab ]]
54+
then
55+
result="$result$b"
56+
fi
57+
done
58+
echo -n "$result"
59+
}
60+
61+
function read_title_report() {
62+
body=$(read_osc)
63+
echo -n ${body:1}
64+
}
65+
66+
init
67+
68+
# Request a report with the profile name
69+
send_title_request
70+
71+
# Read teh response
72+
value=$(read_title_report)
73+
74+
# Restore the terminal to cooked mode.
75+
stty "$saved_stty"
76+
77+
# Print the profile name
78+
echo "$value"

0 commit comments

Comments
 (0)