feat: tx: added erasing line function.

This commit is contained in:
Andrey Parhomenko 2024-08-19 07:47:21 +05:00
parent 2a13fa4ee3
commit cc0131c812
2 changed files with 99 additions and 46 deletions

View file

@ -8,6 +8,7 @@ import "surdeus.su/core/cli/aes"
//import "fmt" //import "fmt"
func main() { func main() {
normalMode := true normalMode := true
term, err := tx.NewTerminal(os.Stdin) term, err := tx.NewTerminal(os.Stdin)
if err != nil { if err != nil {
@ -21,16 +22,30 @@ func main() {
term.EnableAltBuffer() term.EnableAltBuffer()
defer term.Restore() defer term.Restore()
var x, y int
x, y = 1, 2
update := func() {
term.SaveCursor()
term.MoveCursorTo(1, 1)
term.EraseLine()
w, h, _ := term.Size()
term.Print(
aes.ETF("%dx%d %d %d", w, h, x, y).
Bold().
With(aes.EffectRedBG).
With(aes.EffectBlackFG),
)
term.UndoCursor()
}
term.MoveCursorTo(x, y)
update()
MAIN: MAIN:
for { select { for {
select {
// Handling signals. // Handling signals.
case sig := <-signals : case sig := <-signals :
if sig == tx.SignalWinChange { if sig == tx.SignalWinChange {
term.SaveCursor()
term.MoveCursorHome()
w, h, _ := term.Size()
term.Printf("%dx%d", w, h)
term.UndoCursor()
} }
// Handling keys. // Handling keys.
case key := <-keys : case key := <-keys :
@ -44,12 +59,22 @@ func main() {
if normalMode { switch key { if normalMode { switch key {
case 'j': case 'j':
term.MoveCursorDown(1) term.MoveCursorDown(1)
y++
case 'k': case 'k':
if y == 2 {
break
}
term.MoveCursorUp(1) term.MoveCursorUp(1)
y--
case 'h': case 'h':
if x == 1 {
break
}
term.MoveCursorLeft(1) term.MoveCursorLeft(1)
x--
case 'l' : case 'l' :
term.MoveCursorRight(1) term.MoveCursorRight(1)
x++
case tx.KeyESC : case tx.KeyESC :
case 'i' : case 'i' :
normalMode = false normalMode = false
@ -67,6 +92,8 @@ func main() {
Bold(). Bold().
With(aes.EffectRedFG), With(aes.EffectRedFG),
) )
}} }
update()
}
} }

View file

@ -127,16 +127,38 @@ func (t *Terminal) MoveCursorDown(n int) {
t.Printf(aes.MoveCursorDownFormat, n) t.Printf(aes.MoveCursorDownFormat, n)
} }
func (t *Terminal) MoveCursorBy(xd, yd int) {
if xd != 0 {
if yd < 0 {
t.MoveCursorLeft(xd)
} else {
t.MoveCursorRight(xd)
}
}
if yd != 0 {
if yd < 0 {
t.MoveCursorUp(yd)
} else {
t.MoveCursorDown(yd)
}
}
}
func (t *Terminal) MoveCursorTo(x, y int) {
t.Printf(aes.MoveCursorToFormat, y, x)
}
func (t *Terminal) MoveCursorHome() { func (t *Terminal) MoveCursorHome() {
t.Printf("%s", aes.MoveCursorHome) t.Printf("%s", aes.MoveCursorHome)
} }
func (t *Terminal) SaveCursor() { func (t *Terminal) SaveCursor() {
t.Printf("%s", aes.SaveCursorPositionDEC) t.Printf("%s", aes.SaveCursorPositionSCO)
} }
func (t *Terminal) UndoCursor() { func (t *Terminal) UndoCursor() {
t.Printf("%s", aes.UndoCursorPositionDEC) t.Printf("%s", aes.UndoCursorPositionSCO)
} }
func (t *Terminal) NextLine(n int) { func (t *Terminal) NextLine(n int) {
@ -146,6 +168,10 @@ func (t *Terminal) NextLine(n int) {
) )
} }
func (t *Terminal) EraseLine() {
t.Print(aes.EraseEntireLine)
}
func (t *Terminal) EnableAltBuffer() { func (t *Terminal) EnableAltBuffer() {
t.Printf("%s", aes.EnableAltBuffer) t.Printf("%s", aes.EnableAltBuffer)
} }