aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ui/context.go
diff options
context:
space:
mode:
authorTim Culverhouse <tim@timculverhouse.com>2023-02-27 09:18:14 -0600
committerRobin Jarry <robin@jarry.cc>2023-03-02 22:49:57 +0100
commit49de9b09caccc83adb0d52f0642f2c39a5e6a7e2 (patch)
tree2a9e2cb5bb0d5e87b1b80d07bfef028d402f9fd8 /lib/ui/context.go
parent125137c7b3b53ae621db0fced201181add70d370 (diff)
downloadaerc-49de9b09caccc83adb0d52f0642f2c39a5e6a7e2.tar.gz
ui: parse strings for ansi styles
Parse UI strings for ANSI styles. If there are styles in the string, use those as the display style in tcell. This is in preparation for a template function which can apply arbitrary styles to UI elements. Signed-off-by: Tim Culverhouse <tim@timculverhouse.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ui/context.go')
-rw-r--r--lib/ui/context.go19
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/ui/context.go b/lib/ui/context.go
index 12d65bbf..9ca7cc9d 100644
--- a/lib/ui/context.go
+++ b/lib/ui/context.go
@@ -3,9 +3,9 @@ package ui
import (
"fmt"
+ "git.sr.ht/~rjarry/aerc/lib/parse"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
- "github.com/mattn/go-runewidth"
)
// A context allows you to draw in a sub-region of the terminal
@@ -75,6 +75,9 @@ func (ctx *Context) Printf(x, y int, style tcell.Style,
str := fmt.Sprintf(format, a...)
+ buf := parse.ParseANSI(str)
+ buf.ApplyStyle(style)
+
old_x := x
newline := func() bool {
@@ -82,27 +85,27 @@ func (ctx *Context) Printf(x, y int, style tcell.Style,
y++
return y < height
}
- for _, ch := range str {
- switch ch {
+ for _, sr := range buf.Runes() {
+ switch sr.Value {
case '\n':
if !newline() {
- return runewidth.StringWidth(str)
+ return buf.Len()
}
case '\r':
x = old_x
default:
crunes := []rune{}
- ctx.viewport.SetContent(x, y, ch, crunes, style)
- x += runewidth.RuneWidth(ch)
+ ctx.viewport.SetContent(x, y, sr.Value, crunes, sr.Style)
+ x += sr.Width
if x == old_x+width {
if !newline() {
- return runewidth.StringWidth(str)
+ return buf.Len()
}
}
}
}
- return runewidth.StringWidth(str)
+ return buf.Len()
}
func (ctx *Context) Fill(x, y, width, height int, rune rune, style tcell.Style) {