continue on timeout error
update deps
add timeout and close-after flags
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".
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
}
}
}
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)
The BSD 3-Clause license.