summaryrefslogtreecommitdiff
path: root/tui/builder.go
diff options
context:
space:
mode:
Diffstat (limited to 'tui/builder.go')
-rw-r--r--tui/builder.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/tui/builder.go b/tui/builder.go
new file mode 100644
index 0000000..e969cec
--- /dev/null
+++ b/tui/builder.go
@@ -0,0 +1,39 @@
+package tui
+
+import (
+ "github.com/rivo/uniseg"
+)
+
+type builder struct {
+ runs []textRun
+ width int
+}
+
+func (b *builder) add(s string, style *Style) {
+ b.width += uniseg.StringWidth(s)
+ if len(b.runs) == 0 || style != b.runs[len(b.runs) - 1].style {
+ b.runs = append(b.runs, textRun {s, style, false})
+ } else {
+ run := b.runs[len(b.runs) - 1]
+ run.text += s
+ b.runs[len(b.runs) - 1] = run
+ }
+}
+
+func (b *builder) addRun(run textRun) {
+ b.width += uniseg.StringWidth(run.text)
+ b.runs = append(b.runs, run)
+}
+
+func (b *builder) addRuns(runs []textRun) {
+ for _, run := range runs {
+ b.addRun(run)
+ }
+}
+
+func (b *builder) flush() []textRun {
+ runs := b.runs
+ b.width = 0
+ b.runs = nil
+ return runs
+}