### aniputs.tcl  -*- Tcl -*-
## Simplistic puts-based tty animations.

### Ivan Shmakov, 2020

## To the extent possible under law, the author(s) have dedicated
## all copyright and related and neighboring rights to this software
## to the public domain worldwide.  This software is distributed
## without any warranty.

## You should have received a copy of the CC0 Public Domain Dedication
## along with this software.  If not, see
## <http://creativecommons.org/publicdomain/zero/1.0/>.

### History:

## 0.1  2020-05-09
##      Initial revision.

### Example:

## package require aniputs
## alias aniputs_u aniputs_uni_str
## alias aniputs_c aniputs_uni_chr
## set ev ""
## aniputs_u ev 250 {[jrh@ghost] ~$ }
## aniputs_c ev  30 "hello \n" 150
## aniputs_u ev 150 {Hello, world!
## [jrh@ghost] ~$ }
## aniputs_play $ev
## vwait xit
## gets stdin

### See also:

## terminfo(5), infocmp(1)

### Code:

proc aniputs_wait { varn ms } {
    upvar 1 $varn ev
    lappend ev $ms ""
}

proc aniputs_uni_str { varn min s { max "" } } {
    upvar 1 $varn ev
    set m [ switch -- $max {
                "" { set min }
                default { rand $min $max } } ]
    ## .
    lappend ev $m $s
}

proc aniputs_uni_chr { varn min s { max "" } } {
    upvar 1 $varn ev
    for { set i 0 } { $i < [ string length $s ] } { incr i } {
        aniputs_uni_str ev $min [ string range $s $i $i ] $max
    }
    ## .
    set ev
}

proc aniputs_1 { s } {
    switch -- $s { "" return }
    puts -nonewline $s
    flush stdout
}

proc aniputs_play { ev { s "" } } {
    aniputs_1 $s
    foreach { ms s } [ lrange $ev 0 1 ] {
        set ev [ lreplace $ev 0 1 ]
        after $ms [ list aniputs_play $ev $s ]
        break
    }
}

## .
package provide aniputs 0.1

### aniputs.tcl ends here
