summaryrefslogtreecommitdiff
path: root/typr.sh
blob: 717715311707d3e7c68e45011c542e95b1279d36 (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
#!/bin/sh

entered_text=""
text="the quick brown fox jumps over the lazy dog"

tty_init () {
    tput clear
    export SAVED_TTY_SETTINGS=$(stty -g)
    stty raw -echo
    trap typr_cleanup 1 2 3 6

    printf "[3 q"

}

tty_cleanup () {
    tput clear
    stty $SAVED_TTY_SETTINGS
}

tty_readc () {
    stty -echo -icanon min 1 time 0
    s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)"
    stty -icanon min 0 time 0
    [ "$s" = "" ]  && {
        s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)"
    }
    printf "$s"
}

typr_draw_text () {
    tput civis
    tput clear
    printf ""

    t="$text"
    e="$entered_text"
    while [ "$t" ] ; do
        ct=${t%${t#?}}
        ce=${e%${e#?}}
        t="${t#?}"
        e="${e#?}"

        [ "$ct" = "$ce" ] \
            && printf "" \
            || {
            [ "$e" ] \
                    && printf "" \
                    || printf ""
            }

        printf "$ct"
    done
    printf "[1;$((${#entered_text} + 1))H"
    tput cnorm
}


typr_main () {
    while true; do
        typr_draw_text
        c="$(tty_readc)"
        case "$c" in
            '
') break;;
            '')
                entered_text="${entered_text%?}"
                ;;
            *)
                echo "$c" >> LOG
                entered_text="$entered_text$c"
                ;;

        esac
    done
}

typr_init () {
    tty_init
    typr_main
    tty_cleanup
}

typr_init