cli/cmd/termsize/main.go

100 lines
1.6 KiB
Go
Raw Normal View History

2024-06-05 11:45:03 +03:00
package main
import "surdeus.su/core/cli/tx"
import "log"
import "os"
//import "io"
2024-06-05 14:18:54 +03:00
import "surdeus.su/core/cli/aes"
2024-06-05 12:49:23 +03:00
//import "fmt"
2024-06-05 11:45:03 +03:00
func main() {
2024-08-19 05:47:21 +03:00
normalMode := true
2024-06-05 12:49:23 +03:00
term, err := tx.NewTerminal(os.Stdin)
if err != nil {
2024-06-05 11:45:03 +03:00
// Some not terminal handling.
log.Fatalf("tx.NewTerminal(...): %s\n", err)
2024-06-05 11:45:03 +03:00
}
keys, signals, err := term.MakeRaw()
2024-06-05 11:45:03 +03:00
if err != nil {
log.Fatalf("term.MakeRaw(...) %s\n", err)
2024-06-05 11:45:03 +03:00
}
term.EnableAltBuffer()
defer term.Restore()
2024-08-19 05:47:21 +03:00
var x, y int
x, y = 1, 2
update := func() {
term.SaveCursor()
term.MoveCursorTo(1, 1)
term.EraseLine()
w, h, _ := term.Size()
2024-06-05 14:18:54 +03:00
term.Print(
2024-08-19 05:47:21 +03:00
aes.ETF("%dx%d %d %d", w, h, x, y).
Bold().
2024-08-19 05:47:21 +03:00
With(aes.EffectRedBG).
With(aes.EffectBlackFG),
2024-06-05 14:18:54 +03:00
)
2024-08-19 05:47:21 +03:00
term.UndoCursor()
}
term.MoveCursorTo(x, y)
update()
MAIN:
for {
select {
// Handling signals.
case sig := <-signals :
if sig == tx.SignalWinChange {
}
// Handling keys.
case key := <-keys :
if key == tx.KeyControlC {
break MAIN
}
if !normalMode && key == tx.KeyESC {
normalMode = true
break
}
if normalMode { switch key {
case 'j':
term.MoveCursorDown(1)
y++
case 'k':
if y == 2 {
break
}
term.MoveCursorUp(1)
y--
case 'h':
if x == 1 {
break
}
term.MoveCursorLeft(1)
x--
case 'l' :
term.MoveCursorRight(1)
x++
case tx.KeyESC :
case 'i' :
normalMode = false
}
break }
if key == tx.KeyEnter {
term.NextLine(1)
//term.Print("enter")
break
}
term.Print(
aes.ETF("%s", string(key)).
Bold().
With(aes.EffectRedFG),
)
}
update()
}
2024-06-05 11:45:03 +03:00
}