aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ui/string.go
blob: 407c99630c1645db5132541c6c06d89cc1600829 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ui

import (
	"git.sr.ht/~rockorager/vaxis"
)

func StyledString(s string) *vaxis.StyledString {
	return state.vx.NewStyledString(s, vaxis.Style{})
}

// Applies a style to a string. Any currently applied styles will not be overwritten
func ApplyStyle(style vaxis.Style, str string) string {
	ss := StyledString(str)
	d := vaxis.Style{}
	for i, sr := range ss.Cells {
		if sr.Style == d {
			sr.Style = style
			ss.Cells[i] = sr
		}
	}
	return ss.Encode()
}

// PadLeft inserts blank spaces at the beginning of the StyledString to produce
// a string of the provided width
func PadLeft(ss *vaxis.StyledString, width int) {
	w := ss.Len()
	if w >= width {
		return
	}
	cell := vaxis.Cell{
		Character: vaxis.Character{
			Grapheme: " ",
			Width:    1,
		},
	}
	w = width - w
	cells := make([]vaxis.Cell, 0, len(ss.Cells)+w)
	for w > 0 {
		cells = append(cells, cell)
		w -= 1
	}
	cells = append(cells, ss.Cells...)
	ss.Cells = cells
}

// PadLeft inserts blank spaces at the end of the StyledString to produce
// a string of the provided width
func PadRight(ss *vaxis.StyledString, width int) {
	w := ss.Len()
	if w >= width {
		return
	}
	cell := vaxis.Cell{
		Character: vaxis.Character{
			Grapheme: " ",
			Width:    1,
		},
	}
	w = width - w
	for w > 0 {
		w -= 1
		ss.Cells = append(ss.Cells, cell)
	}
}

// ApplyAttrs applies the style, and if another style is present ORs the
// attributes
func ApplyAttrs(ss *vaxis.StyledString, style vaxis.Style) {
	for i, cell := range ss.Cells {
		if style.Foreground != 0 {
			cell.Style.Foreground = style.Foreground
		}
		if style.Background != 0 {
			cell.Style.Background = style.Background
		}
		cell.Style.Attribute |= style.Attribute
		if style.UnderlineColor != 0 {
			cell.Style.UnderlineColor = style.UnderlineColor
		}
		if style.UnderlineStyle != vaxis.UnderlineOff {
			cell.Style.UnderlineStyle = style.UnderlineStyle
		}
		ss.Cells[i] = cell
	}
}

// Truncates the styled string on the right and inserts a '…' as the last
// character
func Truncate(ss *vaxis.StyledString, width int) {
	if ss.Len() <= width {
		return
	}
	cells := make([]vaxis.Cell, 0, len(ss.Cells))
	total := 0
	for _, cell := range ss.Cells {
		if total+cell.Width >= width {
			// we can't fit this cell so put in our truncator
			cells = append(cells, vaxis.Cell{
				Character: vaxis.Character{
					Grapheme: "…",
					Width:    1,
				},
				Style: cell.Style,
			})
			break
		}
		total += cell.Width
		cells = append(cells, cell)
	}
	ss.Cells = cells
}

// TruncateHead truncates the left side of the string and inserts '…' as the
// first character
func TruncateHead(ss *vaxis.StyledString, width int) {
	l := ss.Len()
	if l <= width {
		return
	}
	offset := l - width
	cells := make([]vaxis.Cell, 0, len(ss.Cells))
	cells = append(cells, vaxis.Cell{
		Character: vaxis.Character{
			Grapheme: "…",
			Width:    1,
		},
	})
	total := 0
	for _, cell := range ss.Cells {
		total += cell.Width
		if total < offset {
			// we always have at least one for our truncator. We
			// copy this cells style to it so that it retains the
			// style information from the first printed cell
			cells[0].Style = cell.Style
			continue
		}
		cells = append(cells, cell)
	}
	ss.Cells = cells
}