Really figuring some things out

This commit is contained in:
2025-08-07 11:18:03 -05:00
parent 7c96fbb187
commit b476c74683
23 changed files with 737 additions and 249 deletions

48
text.go
View File

@@ -24,13 +24,16 @@ package widgets
import (
"strings"
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
// Currently the text widget will only split up text on newlines
// TODO: Add 'wrap' mode, which will automatically split it into lines.
type Text struct {
id string
text string
message []string
style tcell.Style
x, y int
w, h int
@@ -52,23 +55,21 @@ func (w *Text) Init(id string, style tcell.Style) {
w.visible = true
w.tabbable = false
}
func (w *Text) Id() string { return w.id }
func (w *Text) HandleResize(ev *tcell.EventResize) {}
func (w *Text) HandleKey(ev *tcell.EventKey) bool { return false }
func (w *Text) HandleTime(ev *tcell.EventTime) {}
func (w *Text) Id() string { return w.id }
func (w *Text) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
w.w = wh.Min(w.w, w.WantW())
w.h = wh.Min(w.h, w.WantH())
}
func (w *Text) HandleKey(ev *tcell.EventKey) bool { return false }
func (w *Text) HandleTime(ev *tcell.EventTime) {}
func (w *Text) Draw(screen tcell.Screen) {
if !w.visible {
return
}
var pts []string
if w.w < len(w.text) {
pts = strings.Split(h.WrapText(w.text, w.w), "\n")
} else {
pts = []string{w.text}
}
y := w.y
for i := range pts {
h.DrawText(w.x, y, pts[i], w.style, screen)
for i := range w.message {
wh.DrawText(w.x-(len(w.message[i])/2), y, w.message[i], w.style, screen)
y++
}
}
@@ -93,14 +94,23 @@ func (w *Text) SetW(x int) { w.w = x }
func (w *Text) SetH(y int) { w.h = y }
func (w *Text) GetW() int { return w.w }
func (w *Text) GetH() int { return w.y }
func (w *Text) WantW() int { return h.Max(w.w, len(w.text)) }
func (w *Text) WantH() int { return h.Max(w.h, 1) }
func (w *Text) WantW() int { return wh.Longest(w.message) }
func (w *Text) WantH() int { return len(w.message) }
func (w *Text) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Text) Focusable() bool { return false }
func (w *Text) SetTabbable(b bool) { w.tabbable = b }
func (w *Text) Tabbable() bool { return w.tabbable }
func (w *Text) MinW() int { return len(w.text) }
func (w *Text) MinH() int { return 1 }
func (w *Text) MinW() int { return wh.Longest(w.message) }
func (w *Text) MinH() int { return len(w.message) }
func (w *Text) SetText(txt string) { w.text = txt }
func (w *Text) GetText() string { return w.text }
func (w *Text) SetText(txt string) {
w.text = txt
if strings.Contains(w.text, "\n") {
w.message = strings.Split(w.text, "\n")
} else {
w.message = []string{w.text}
}
}
func (w *Text) GetText() string { return w.text }
func (w *Text) GetMessage() []string { return w.message }