summaryrefslogtreecommitdiff
path: root/src/write-tty
diff options
context:
space:
mode:
Diffstat (limited to 'src/write-tty')
-rw-r--r--src/write-tty46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/write-tty b/src/write-tty
index fbe62ea..8fdb78f 100644
--- a/src/write-tty
+++ b/src/write-tty
@@ -10,7 +10,7 @@ output_filter()
10chr() 10chr()
11{ 11{
12 declare -i n="$*" 12 declare -i n="$*"
13 printf "\\$(printf %o "$n")" 13 printf "$(printf '\%o' "$n")"
14} 14}
15 15
16colorize() 16colorize()
@@ -20,25 +20,29 @@ colorize()
20 while read -r 20 while read -r
21 do 21 do
22 case "$REPLY" in 22 case "$REPLY" in
23 \\[0-7][0-7][0-7] ) ;; 23 \\[0-7][0-7][0-7] )
24 declare -i c=8#"${REPLY#?}"
25 ;;
26 \\U[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] )
27 declare -i c=16#"${REPLY#??}"
28 ;;
24 * ) 29 * )
25 printf '%s\n' "$REPLY" 30 printf '%s\n' "$REPLY"
26 continue 31 continue
27 ;; 32 ;;
28 esac 33 esac
29 declare -i c=8#"${REPLY#?}"
30 if (( c > 128 + 127 )) 34 if (( c > 128 + 127 ))
31 then 35 then
32 : 36 :
33 elif (( c > 128 + 32 )) 37 elif (( c > 128 + 32 ))
34 then 38 then
35 printf -v REPLY "M-$(chr c - 128)" 39 REPLY=M-$(chr c - 128)
36 elif (( c > 127 )) 40 elif (( c > 127 ))
37 then 41 then
38 printf -v REPLY "M-^$(chr c - 128 + 64)" 42 REPLY=M-^$(chr c - 128 + 64)
39 elif (( c < 32 )) 43 elif (( c < 32 ))
40 then 44 then
41 printf -v REPLY "^$(chr c + 64)" 45 REPLY=^$(chr c + 64)
42 fi 46 fi
43 printf $'\e[106m%s\e[m\n' "$REPLY" 47 printf $'\e[106m%s\e[m\n' "$REPLY"
44 done 48 done
@@ -48,7 +52,7 @@ tokenize()
48{ 52{
49 BASH_ARGV0=tokenize 53 BASH_ARGV0=tokenize
50 echo -n "$0" >/proc/$BASHPID/comm 54 echo -n "$0" >/proc/$BASHPID/comm
51 while read -r -N1 55 while read -r -n1 -d ''
52 do 56 do
53 if [[ "$REPLY" =~ [[:print:]] ]] 57 if [[ "$REPLY" =~ [[:print:]] ]]
54 then 58 then
@@ -56,13 +60,26 @@ tokenize()
56 # multibyte unicode character. 60 # multibyte unicode character.
57 printf '%s\n' "$REPLY" 61 printf '%s\n' "$REPLY"
58 continue 62 continue
59 else 63 elif [ "$REPLY" ]
64 then
60 # If it is a non-printable, then we output a 65 # If it is a non-printable, then we output a
61 # multi-character line. In this case we colorize it 66 # multi-character line. In this case we colorize it
62 # later so that it won't be confused with multiple 67 # later so that it won't be confused with multiple
63 # printable characters in the . 68 # printable characters.
64 printf '\\%.3o\n' "'$REPLY" 69 bytelen=$(LC_ALL=C; echo ${#REPLY})
65 continue 70 if (( bytelen == 1 ))
71 then
72 printf '\\%.3o\n' "'$REPLY"
73 else
74 hexdigits=$(LC_ALL=C; for ((i=0; i<$bytelen; ++i))
75 do
76 printf '%.2x' "'${REPLY:$i:1}"
77 printf '%.2x\n' "'${REPLY:$i:1}" >&2
78 done)
79 printf '%s%.8x\n' '\U' 0x"$hexdigits"
80 fi
81 else
82 printf '\\%.3o\n' 0
66 fi 83 fi
67 done 84 done
68} 85}
@@ -81,9 +98,4 @@ soft_cursor()
81 done 98 done
82} 99}
83 100
84if [ -t 1 ] 101output_filter
85then
86 output_filter
87else
88 exec -a write-tty-raw socat - -
89fi