commit 8846bcc

Hubert Hirtz  ·  2021-10-20 13:21:21 +0000 UTC
parent aa0fb28
Fix ui.StyledStringBuilder
2 files changed,  +31, -4
+30, -4
 1@@ -98,6 +98,23 @@ func (s StyledString) String() string {
 2 	return s.string
 3 }
 4 
 5+func (s StyledString) Truncate(w int, tail StyledString) StyledString {
 6+	if stringWidth(s.string) < w {
 7+		return s
 8+	}
 9+	var sb StyledStringBuilder
10+	truncated := truncate(s.string, w-stringWidth(tail.string), "")
11+	sb.WriteString(truncated)
12+	for _, style := range s.styles {
13+		if len(truncated) <= style.Start {
14+			break
15+		}
16+		sb.styles = append(sb.styles, style)
17+	}
18+	sb.WriteStyledString(tail)
19+	return sb.StyledString()
20+}
21+
22 func isDigit(c byte) bool {
23 	return '0' <= c && c <= '9'
24 }
25@@ -213,6 +230,11 @@ type StyledStringBuilder struct {
26 	styles []rangedStyle
27 }
28 
29+func (sb *StyledStringBuilder) Reset() {
30+	sb.Builder.Reset()
31+	sb.styles = sb.styles[:0]
32+}
33+
34 func (sb *StyledStringBuilder) WriteStyledString(s StyledString) {
35 	start := len(sb.styles)
36 	sb.styles = append(sb.styles, s.styles...)
37@@ -250,12 +272,16 @@ func (sb *StyledStringBuilder) SetStyle(style tcell.Style) {
38 }
39 
40 func (sb *StyledStringBuilder) StyledString() StyledString {
41-	styles := sb.styles
42-	if len(sb.styles) != 0 && sb.styles[len(sb.styles)-1].Start == sb.Len() {
43-		styles = sb.styles[:len(sb.styles)-1]
44+	string := sb.String()
45+	styles := make([]rangedStyle, 0, len(sb.styles))
46+	for _, style := range sb.styles {
47+		if len(string) <= style.Start {
48+			break
49+		}
50+		styles = append(styles, style)
51 	}
52 	return StyledString{
53-		string: sb.String(),
54+		string: string,
55 		styles: styles,
56 	}
57 }
+1, -0
1@@ -341,6 +341,7 @@ func (ui *UI) drawStatusBar(x0, y, width int) {
2 	x += 2
3 
4 	s.Reset()
5+	s.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGray))
6 	s.WriteString(ui.status)
7 
8 	printString(ui.screen, &x, y, s.StyledString())