From 0a58e68ad438ff43fa5bbecdb8914aa00cab5099 Mon Sep 17 00:00:00 2001 From: citrons Date: Sat, 31 May 2025 07:16:32 -0500 Subject: support cursor in TUI layouts --- tui/draw.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'tui/draw.go') diff --git a/tui/draw.go b/tui/draw.go index 3772aa0..37a678e 100644 --- a/tui/draw.go +++ b/tui/draw.go @@ -29,6 +29,7 @@ type screen struct { prevSize ScreenSize writer *bufio.Writer cursor pos + showCursor bool } var ( @@ -73,10 +74,16 @@ func WriteAt(x int, y int, text string, style Style) int { func Clear() { scr.front = make(surface) + ClearCursor() } -func MoveCursor(x int, y int) { +func ShowCursor(x int, y int) { scr.cursor = pos {x, y} + scr.showCursor = true +} + +func ClearCursor() { + scr.showCursor = false } var saved *term.State @@ -120,7 +127,12 @@ func End() { saved = nil } +func writeClearCursor() { + scr.writer.WriteString(terminfo.Get(caps.CursorInvisible)) +} + func writeCursor(x int, y int) { + scr.writer.WriteString(terminfo.Get(caps.CursorNormal)) scr.writer.WriteString(terminfo.Get(caps.CursorAddress, y, x)) } @@ -143,6 +155,8 @@ func writeStyle(style Style) { } func Present() error { + writeClearCursor() + s := Size() reset := true style := DefaultStyle @@ -179,7 +193,9 @@ func Present() error { p.x += cw } } - writeCursor(scr.cursor.x, scr.cursor.y) + if scr.showCursor { + writeCursor(scr.cursor.x, scr.cursor.y) + } scr.prevSize = s f := scr.front -- cgit v1.2.3