-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.go
205 lines (176 loc) · 4.15 KB
/
segment.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package powerline
import (
"fmt"
"hash/crc32"
"strings"
"sync"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/jedib0t/go-prompter/prompt"
"github.com/muesli/termenv"
)
// Segment contains the contents for a "segment" of data in the Powerline
// prompt.
type Segment struct {
color *prompt.Color
content string
contentColor *prompt.Color
hasChanges bool
icon string
mutex sync.Mutex
paddingLeft *string
paddingRight *string
rendered string
width int
}
// Color returns the color that will be used for the segment.
func (s *Segment) Color() prompt.Color {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.color != nil {
return *s.color
}
if s.contentColor != nil {
return *s.contentColor
}
return prompt.Color{
Foreground: termenv.ForegroundColor(),
Background: termenv.BackgroundColor(),
}
}
// HasChanges returns true if Render is going to return a different result
// compared to its last invocation.
func (s *Segment) HasChanges() bool {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.hasChanges
}
// Render returns the segment rendered in appropriate colors.
func (s *Segment) Render() string {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.content == "" {
return ""
}
if s.hasChanges {
color := s.color
if color == nil {
color = s.contentColor
}
out := strings.Builder{}
if s.paddingLeft != nil {
out.WriteString(*s.paddingLeft)
} else {
out.WriteRune(' ')
}
if s.icon != "" {
out.WriteString(s.icon)
out.WriteRune(' ')
}
out.WriteString(s.content)
if s.paddingRight != nil {
out.WriteString(*s.paddingRight)
} else {
out.WriteRune(' ')
}
s.rendered = color.Sprint(out.String())
}
s.hasChanges = false
return s.rendered
}
// ResetColor resets the color of the content to defaults.
func (s *Segment) ResetColor() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.color = nil
}
// SetColor sets the colors to be used for the segment. If not set, the hash of
// the content determines the colors.
func (s *Segment) SetColor(color prompt.Color) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.color != nil && *s.color == color {
return
}
s.hasChanges = true
s.color = &color
}
// SetContent sets the text of the segment.
//
// Normally, the client can set the color for the content using SetColor.
// However, in case the client doesn't do it, the color is determined
// automatically by hashing one of the following:
// - the "tags" values
// - the "content" value if no "tags" provided
func (s *Segment) SetContent(content string, tags ...string) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.content == content {
return // if there are no changes, ignore request
}
defer func() {
s.width = s.calculateWidth()
}()
s.hasChanges = true
s.content = content
// determine the content color based on hash value of content
if s.color == nil {
h := crc32.NewIEEE()
if len(tags) > 0 {
_, _ = h.Write([]byte(fmt.Sprint(tags)))
} else {
_, _ = h.Write([]byte(s.content))
}
hash := h.Sum32()
bg := (hash % (231 - 16)) + 16
fg := 16 // black
if (bg-16)%36 < 18 {
fg = 7 // white
}
s.contentColor = &prompt.Color{
Foreground: termenv.ANSI256Color(fg),
Background: termenv.ANSI256Color(bg),
}
}
}
// SetIcon sets the optional Icon/Emoji to be rendered before the text.
func (s *Segment) SetIcon(icon string) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.icon == icon {
return
}
defer func() {
s.width = s.calculateWidth()
}()
s.hasChanges = true
s.icon = icon
}
// Width returns the width of the segment when printed on screen.
func (s *Segment) Width() int {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.width
}
func (s *Segment) calculateWidth() int {
w := 1 + // separator
1 + // left margin
text.RuneWidthWithoutEscSequences(s.content) +
1 // right margin
if s.icon != "" {
w += 1 + // left margin
text.RuneWidthWithoutEscSequences(s.icon)
}
return w
}
func (s *Segment) setPaddingLeft(p string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.hasChanges = true
s.paddingLeft = &p
}
func (s *Segment) setPaddingRight(p string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.hasChanges = true
s.paddingRight = &p
}