~mna/zztermtest

Test command for the zzterm package.
continue on timeout error
add timeout and close-after flags

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~mna/zztermtest
read/write
git@git.sr.ht:~mna/zztermtest

You can also use your local clone with git send-email.

zztermtest

This is a test command for the zzterm package. It provides examples of using zzterm in a very memory-efficient way by supporting a variety of modes.

echo mode

The echo mode runs a simple echo program where each rune is printed as-is and Enter starts a new line. ESC and Ctrl-C end the program, and all other keys are ignored. Regardless of the amount of input, this program uses zero allocation for all input and output.

// run with go run main.go -echo

var newLine = []byte("\r\n")

func echoMode(rw io.ReadWriter) {
    input := zzterm.NewInput()

    var start, last runtime.MemStats
    var count int
    runtime.ReadMemStats(&start)
    defer func() {
        runtime.ReadMemStats(&last)
        alloc := last.TotalAlloc - start.TotalAlloc
        fmt.Fprintf(rw, "\r\nMemory(%dB; %dKB)\r\n", alloc, alloc/1024)
        fmt.Fprintf(rw, "Keys received: %d\r\n", count)
    }()

    for {
        k, err := input.ReadKey(rw)
        if err != nil {
            log.Panic(err)
        }
        count++

        switch k.Type() {
        case zzterm.KeyRune:
            if _, err := rw.Write(input.Bytes()); err != nil {
                log.Panic(err)
            }
        case zzterm.KeyEnter:
            if _, err := rw.Write(newLine); err != nil {
                log.Panic(err)
            }
        case zzterm.KeyESC, zzterm.KeyCtrlC:
            // quit on ESC or Ctrl-C
            return
        }
    }
}

info mode

The info mode runs a debug-style mode where each key is printed and mouse and memory usage are tracked. The memory allocations in this mode come from the calls to key.String() and mouse.String(), otherwise it reuses a 1KB output buffer to minimize write memory usage, with strconv.Append... family of functions to print numbers in an efficient way.

// run with go run main.go
// or go run main.go -info
// (this is the default mode)

License

The BSD 3-Clause license.