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,52 +22,78 @@ func main() {
term.EnableAltBuffer() term.EnableAltBuffer()
defer term.Restore() defer term.Restore()
MAIN: var x, y int
for { select { x, y = 1, 2
// Handling signals. update := func() {
case sig := <-signals : term.SaveCursor()
if sig == tx.SignalWinChange { term.MoveCursorTo(1, 1)
term.SaveCursor() term.EraseLine()
term.MoveCursorHome() w, h, _ := term.Size()
w, h, _ := term.Size()
term.Printf("%dx%d", w, h)
term.UndoCursor()
}
// 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)
case 'k':
term.MoveCursorUp(1)
case 'h':
term.MoveCursorLeft(1)
case 'l' :
term.MoveCursorRight(1)
case tx.KeyESC :
case 'i' :
normalMode = false
}
break }
if key == tx.KeyEnter {
term.NextLine(1)
//term.Print("enter")
break
}
term.Print( term.Print(
aes.ETF("%s", string(key)). aes.ETF("%dx%d %d %d", w, h, x, y).
Bold(). Bold().
With(aes.EffectRedFG), With(aes.EffectRedBG).
With(aes.EffectBlackFG),
) )
}} 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()
}
} }

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