summaryrefslogtreecommitdiff
path: root/src/write-tty
blob: 60046e1f6ca7fb0cfc0e73916f00d19142c43a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
BASH_ARGV0=write-tty
echo -n "$0" >/proc/$BASHPID/comm

output_filter()
{
        tokenize | colorize | soft_cursor
}

chr()
{
        declare -i n="$*"
        printf "$(printf '\%o' "$n")"
}

colorize()
{
        BASH_ARGV0=colorize
        echo -n "$0" >/proc/$BASHPID/comm
        while read -r
        do
                case "$REPLY" in
                        \\[0-7][0-7][0-7] )
                                declare -i c=8#"${REPLY#?}"
                        ;;
                        \\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] )
                                declare -i c=16#"${REPLY#??}"
                        ;;
                        * )
                                printf '0 %s\n' "$REPLY"
                                continue
                                ;;
                esac
                if (( c > 128 + 127 ))
                then
                        :
                elif (( c > 128 + 32 ))
                then
                        REPLY=M-$(chr c - 128)
                elif (( c > 127 ))
                then
                        REPLY=M-^$(chr c - 128 + 64)
                elif (( c < 32 ))
                then
                        REPLY=^$(chr c + 64)
                fi
                printf '106 %s\n' "$REPLY"
        done
}

tokenize()
{
        BASH_ARGV0=tokenize
        echo -n "$0" >/proc/$BASHPID/comm
        while read -r -n1 -d ''
        do
                if [[ "$REPLY" =~ [[:print:]] ]]
                then
                        # Output one printable character per line. It may be a
                        # multibyte unicode character.
                        printf '%s\n' "$REPLY"
                        continue
                elif [ "$REPLY" ]
                then
                        # If it is a non-printable, then we output a
                        # multi-character line. In this case we colorize it
                        # later so that it won't be confused with multiple
                        # printable characters.
                        bytelen=$(LC_ALL=C; echo ${#REPLY})
                        if (( bytelen == 1 ))
                        then
                                printf '\\%.3o\n' "'$REPLY"
                        else
                                hexdigits=$(LC_ALL=C; for ((i=0; i<$bytelen; ++i))
                                do
                                        printf '%.2x' "'${REPLY:$i:1}"
                                        printf '%.2x\n' "'${REPLY:$i:1}" >&2
                                done)
                                printf '%s%.8x\n' '\U' 0x"$hexdigits"
                        fi
                else
                        printf '\\%.3o\n' 0
                fi
        done
}

padding()
{
        for ((i=0; i<$1; ++i))
        do
                echo -n ' '
        done
        for ((i=0; i<$1; ++i))
        do
                echo -en '\b'
        done
}

soft_cursor()
{
        BASH_ARGV0=soft_cursor
        echo -n "$0" >/proc/$BASHPID/comm
        FMT=$'\e[m %s\b%s\e[%sm \b\e[m'
        REPLY=
        declare -i replylen=0 replycolor
        promptcolor=105
        while printf "$FMT" "$(padding $replylen)" "$REPLY" "$promptcolor"
        do
                read -r replycolor REPLY || break
                replylen=${#REPLY}
                case "$replycolor" in
                        0 )
                        ;;
                        * )
                                REPLY=$'\e['"$replycolor"'m'"$REPLY"$'\e[m'
                        ;;
                esac
        done
}

output_filter