diff options
author | Andrew Cady <d@jerkface.net> | 2024-08-21 08:13:09 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2024-08-21 20:47:18 -0400 |
commit | 0f255a500235df5c9d59b735836f6106537cf452 (patch) | |
tree | a7983a2316134d80c34c4cb606b047182afa657a /src/write-tty | |
parent | cd1a4ac7692ec73a35cfdb86334a5e9655d9a609 (diff) |
separate write-tty command
Diffstat (limited to 'src/write-tty')
-rw-r--r-- | src/write-tty | 89 |
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 | ||
2 | BASH_ARGV0=write-tty | ||
3 | echo -n "$0" >/proc/$BASHPID/comm | ||
4 | |||
5 | output_filter() | ||
6 | { | ||
7 | tokenize | colorize | soft_cursor | ||
8 | } | ||
9 | |||
10 | chr() | ||
11 | { | ||
12 | declare -i n="$*" | ||
13 | printf "\\$(printf %o "$n")" | ||
14 | } | ||
15 | |||
16 | colorize() | ||
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 | |||
47 | tokenize() | ||
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 | |||
70 | soft_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 | |||
84 | if [ -t 1 ] | ||
85 | then | ||
86 | output_filter | ||
87 | else | ||
88 | exec -a write-tty-raw socat - - | ||
89 | fi | ||