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