diff --git a/cmd/termsize/main.go b/cmd/termsize/main.go index adccd15..9d0732e 100644 --- a/cmd/termsize/main.go +++ b/cmd/termsize/main.go @@ -8,6 +8,7 @@ import "surdeus.su/core/cli/aes" //import "fmt" func main() { + normalMode := true term, err := tx.NewTerminal(os.Stdin) if err != nil { @@ -21,52 +22,78 @@ func main() { term.EnableAltBuffer() defer term.Restore() - MAIN: - for { select { - // Handling signals. - case sig := <-signals : - if sig == tx.SignalWinChange { - term.SaveCursor() - term.MoveCursorHome() - 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 - } - + 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("%s", string(key)). + aes.ETF("%dx%d %d %d", w, h, x, y). 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() + } } diff --git a/tx/term.go b/tx/term.go index f646257..66eda52 100644 --- a/tx/term.go +++ b/tx/term.go @@ -127,16 +127,38 @@ func (t *Terminal) MoveCursorDown(n int) { 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() { t.Printf("%s", aes.MoveCursorHome) } func (t *Terminal) SaveCursor() { - t.Printf("%s", aes.SaveCursorPositionDEC) + t.Printf("%s", aes.SaveCursorPositionSCO) } func (t *Terminal) UndoCursor() { - t.Printf("%s", aes.UndoCursorPositionDEC) + t.Printf("%s", aes.UndoCursorPositionSCO) } 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() { t.Printf("%s", aes.EnableAltBuffer) }