forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerline.go
171 lines (151 loc) · 4.42 KB
/
powerline.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
package main
import (
"bytes"
"fmt"
"strings"
"golang.org/x/text/width"
"math"
"os"
"strconv"
)
type ShellInfo struct {
rootIndicator string
colorTemplate string
escapedDollar string
escapedBacktick string
escapedBackslash string
}
type powerline struct {
args args
cwd string
theme Theme
shellInfo ShellInfo
reset string
symbolTemplates Symbols
priorities map[string]int
ignoreRepos map[string]bool
Segments []segment
}
func NewPowerline(args args, cwd string, priorities map[string]int) *powerline {
p := new(powerline)
p.args = args
p.cwd = cwd
p.theme = themes[*args.Theme]
p.shellInfo = shellInfos[*args.Shell]
p.reset = fmt.Sprintf(p.shellInfo.colorTemplate, "[0m")
p.symbolTemplates = symbolTemplates[*args.Mode]
p.priorities = priorities
p.ignoreRepos = make(map[string]bool)
for _, r := range strings.Split(*args.IgnoreRepos, ",") {
p.ignoreRepos[r] = true
}
p.Segments = make([]segment, 0)
return p
}
func (p *powerline) color(prefix string, code uint8) string {
if code == p.theme.Reset {
return p.reset
} else {
return fmt.Sprintf(p.shellInfo.colorTemplate, fmt.Sprintf("[%s;5;%dm", prefix, code))
}
}
func (p *powerline) fgColor(code uint8) string {
return p.color("38", code)
}
func (p *powerline) bgColor(code uint8) string {
return p.color("48", code)
}
func (p *powerline) appendSegment(origin string, segment segment) {
if segment.separator == "" {
segment.separator = p.symbolTemplates.Separator
}
if segment.separatorForeground == 0 {
segment.separatorForeground = segment.background
}
priority, _ := p.priorities[origin]
segment.priority = priority
p.Segments = append(p.Segments, segment)
}
func (p *powerline) draw() string {
shellMaxLengthStr, _ := os.LookupEnv("COLUMNS")
shellMaxLength64, _ := strconv.ParseInt(shellMaxLengthStr, 0, 64)
shellMaxLength := int(shellMaxLength64)
shellMaxLength = shellMaxLength * *p.args.MaxWidthPercentage / 100
shellActualLength := 0
if shellMaxLength > 0 {
for _, segment := range p.Segments {
shellActualLength += len(segment.content) + len(segment.separator)
}
for shellActualLength > shellMaxLength {
minPriority := math.MaxInt64
minPrioritySegmentId := -1
for idx, segment := range p.Segments {
if segment.priority < minPriority {
minPriority = segment.priority
minPrioritySegmentId = idx
}
}
if minPrioritySegmentId != -1 {
segment := p.Segments[minPrioritySegmentId]
p.Segments = append(p.Segments[:minPrioritySegmentId], p.Segments[minPrioritySegmentId+1:]...)
shellActualLength -= len(segment.content) + len(segment.separator)
}
}
}
var buffer bytes.Buffer
for idx, segment := range p.Segments {
var separatorBackground string
if idx >= len(p.Segments)-1 {
separatorBackground = p.reset
} else {
nextSegment := p.Segments[idx+1]
separatorBackground = p.bgColor(nextSegment.background)
}
buffer.WriteString(p.fgColor(segment.foreground))
buffer.WriteString(p.bgColor(segment.background))
buffer.WriteString(segment.content)
buffer.WriteString(separatorBackground)
buffer.WriteString(p.fgColor(segment.separatorForeground))
buffer.WriteString(segment.separator)
buffer.WriteString(p.reset)
}
buffer.WriteRune(' ')
drawnResult := buffer.String()
if *p.args.EastAsianWidth {
var spaceBuffer bytes.Buffer
for _, r := range drawnResult {
switch width.LookupRune(r).Kind() {
case width.Neutral:
case width.EastAsianAmbiguous:
spaceBuffer.WriteRune(' ')
case width.EastAsianWide:
case width.EastAsianNarrow:
case width.EastAsianFullwidth:
case width.EastAsianHalfwidth:
}
}
drawnResult += spaceBuffer.String()
}
if *p.args.PromptOnNewLine {
var nextLineBuffer bytes.Buffer
nextLineBuffer.WriteRune('\n')
var foreground, background uint8
if *p.args.PrevError == 0 {
foreground = p.theme.CmdPassedFg
background = p.theme.CmdPassedBg
} else {
foreground = p.theme.CmdFailedFg
background = p.theme.CmdFailedBg
}
nextLineBuffer.WriteString(p.fgColor(foreground))
nextLineBuffer.WriteString(p.bgColor(background))
nextLineBuffer.WriteString(p.shellInfo.rootIndicator)
nextLineBuffer.WriteString(p.reset)
nextLineBuffer.WriteString(p.fgColor(background))
nextLineBuffer.WriteString(p.symbolTemplates.Separator)
nextLineBuffer.WriteString(p.reset)
nextLineBuffer.WriteRune(' ')
drawnResult += nextLineBuffer.String()
}
return drawnResult
}