toolbox/tui/tui.go

50 lines
596 B
Go
Raw Normal View History

2021-03-31 08:27:54 +00:00
package tui
import (
"io"
"os"
"sync"
"golang.org/x/term"
)
var (
lock = sync.Mutex{}
t *term.Terminal
oldState *term.State
)
func Start() {
lock.Lock()
defer lock.Unlock()
if t != nil {
return
}
screen := struct {
io.Reader
io.Writer
}{os.Stdin, os.Stdout}
var err error
oldState, err = term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
t = term.NewTerminal(screen, "")
t.SetSize(80, 24)
}
func Stop() {
lock.Lock()
defer lock.Unlock()
if t == nil {
return
}
term.Restore(int(os.Stdin.Fd()), oldState)
t = nil
oldState = nil
}