summaryrefslogtreecommitdiff
path: root/typr.sh
blob: fcfa0f95b76929062a36a5b9887998f3f57f2ea4 (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
122
123
124
125
126
127
128
#!/bin/sh

words="the be of and a to in he have it that for they I with as not on she at by this we you do but from or which one would all will there say who make when can more if no man out other so what time up go about than into could state only new year some take come these know see use get like then first any work now may such give over think most even find day also after way many must look before great back through long where much should well people down own just because good each those feel seem how high too place little world very still nation hand old life tell write become here show house both between need mean call develop under last right move thing general school never same another begin while number part turn real leave might want point form off child few small since against ask late home interest large person end open public follow during present without again hold govern around possible head consider word program problem however lead system set order eye plan run keep face fact group play stand increase early course change help line"
entered_text=""
text=""

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

    printf "[6 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 () {

    cols="$(tput cols)"
    lines="$(tput lines)"

    startcol=$((cols / 3))
    line=$((lines / 3))
    cpos="0;0"

    color=""

    tput civis

    cpos="${line};${startcol}"
    printf "[${cpos}H${color}"

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


        [ "$ct" != "$ce" ] \
            && newcolor="" \
            || newcolor=""

        [ ! "$ce" ] && newcolor="" \

        [ "$color" != "$newcolor" ] && {
            color="$newcolor"
            printf "$newcolor"
            [ "$color" = "" ] && cpos="${line};$((i+$startcol))"
        }


        [ "$i" -gt "$startcol" ] && {
            line=$((line+1))
            printf "[${line};${startcol}H"
            i=0
        }

        [ "$ct" = " " ] && [ "$color" = "" ] \
         && printf "_" \
         || printf "$ct"

        i=$((i+1))
    done
    printf "[${cpos}H"
    tput cnorm
}

typr_generate_text () {
    words="$(printf "%s " $words | shuf)"
    wordcount=100

    set -- $words
    text=""

    i=0
    while [ "$i" -lt "$wordcount" ]; do
        text="$1 $text"
        shift
        i=$((i+1))
    done
    text="${text% }"
}


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_generate_text
    typr_main
    tty_cleanup
}

typr_init