diff --git a/btest.sh b/btest.sh new file mode 100755 index 0000000..42a009f --- /dev/null +++ b/btest.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +go build -o ./exe/ ./cmd/* + diff --git a/cmd/termsize/main.go b/cmd/termsize/main.go new file mode 100644 index 0000000..fac7dad --- /dev/null +++ b/cmd/termsize/main.go @@ -0,0 +1,34 @@ +package main + +import "surdeus.su/core/cli/tx" +import "fmt" +import "log" +import "os" + +func main() { + term, ok := tx.NewTerminal(os.Stdin) + if !ok { + // Some not terminal handling. + log.Fatal("not terminal") + } + term.MakeRaw() + defer term.Restore() + + w, h, err := term.GetSize() + if err != nil { + log.Fatalf("term.GetSize(...): %s\n", err) + } + fmt.Printf("%d\n%d\n", w, h) + + for { + key, _, err := term.ReadKey() + if err != nil { + log.Fatalf("term.ReadLine(...): %s\n", err) + } + if key == tx.KeyControlC { + break + } + term.Printf("shit: %q\r\n", key) + } +} + diff --git a/go.mod b/go.mod index c500fec..db0c779 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module surdeus.su/core/cli go 1.18 + +require ( + golang.org/x/sys v0.21.0 // indirect + golang.org/x/term v0.21.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6208220 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= +golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= diff --git a/taskfile.yml b/taskfile.yml deleted file mode 100644 index 13095cb..0000000 --- a/taskfile.yml +++ /dev/null @@ -1,7 +0,0 @@ -version: '3' - -tasks: - btest: - cmds: - - go build ./cmd/test - diff --git a/tx/aes.go b/tx/aes.go new file mode 100644 index 0000000..aa7a446 --- /dev/null +++ b/tx/aes.go @@ -0,0 +1,8 @@ +package tx + +type Key rune + +const ( + KeyControlC Key = '\x03' +) + diff --git a/tx/term.go b/tx/term.go new file mode 100644 index 0000000..5942d99 --- /dev/null +++ b/tx/term.go @@ -0,0 +1,86 @@ +package tx + +import "golang.org/x/term" +import "bufio" +import "fmt" +import "os" + +type Terminal struct { + x *term.Terminal + file *os.File + input *bufio.Reader + fd int + prevState *term.State +} + +func NewTerminal(input *os.File) (*Terminal, bool) { + if input == nil { + return nil, false + } + + fd := int(input.Fd()) + if !term.IsTerminal(fd) { + return nil, false + } + + + ret := Terminal{} + ret.file = input + ret.input = bufio.NewReader(input) + ret.fd = fd + ret.x = term.NewTerminal(input, "") + + return &ret, true +} + +func (t *Terminal) MakeRaw() error { + state, err := term.MakeRaw(t.fd) + if err != nil { + return err + } + + t.prevState = state + return nil +} + +func (t *Terminal) Restore() error { + if t.prevState == nil { + return nil + } + + err := term.Restore(t.fd, t.prevState) + if err != nil { + return err + } + + return nil +} + +func (t *Terminal) GetSize() (int, int, error) { + return term.GetSize(t.fd) +} + +func (t *Terminal) ReadLine() (string, error) { + return t.x.ReadLine() +} + +func (t *Terminal) ReadKey() (Key, int, error) { + r, size, err := t.input.ReadRune() + return Key(r), size, err +} + +func (t *Terminal) Write(bts []byte) (int, error) { + return t.x.Write(bts) +} + +func (t *Terminal) Printf( + format string, v ...any, +) (int, error) { + str := fmt.Sprintf(format, v...) + return t.Write([]byte(str)) +} + +func (t *Terminal) Print(v ...any) (int, error) { + str := fmt.Sprint(v...) + return t.Write([]byte(str)) +}