feat: started implementing the tx.

This commit is contained in:
Andrey Parhomenko 2024-06-05 13:45:03 +05:00
parent f7f3374698
commit 7c8f818b0f
7 changed files with 141 additions and 7 deletions

4
btest.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
go build -o ./exe/ ./cmd/*

34
cmd/termsize/main.go Normal file
View file

@ -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)
}
}

5
go.mod
View file

@ -1,3 +1,8 @@
module surdeus.su/core/cli module surdeus.su/core/cli
go 1.18 go 1.18
require (
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
)

4
go.sum Normal file
View file

@ -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=

View file

@ -1,7 +0,0 @@
version: '3'
tasks:
btest:
cmds:
- go build ./cmd/test

8
tx/aes.go Normal file
View file

@ -0,0 +1,8 @@
package tx
type Key rune
const (
KeyControlC Key = '\x03'
)

86
tx/term.go Normal file
View file

@ -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))
}