forked from Code-Hex/battery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery.go
214 lines (180 loc) · 3.9 KB
/
battery.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
206
207
208
209
210
211
212
213
214
package battery
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
"github.com/fatih/color"
)
// LENGTH: 8
var chars = []string{
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█",
}
type Bar struct {
buffer bytes.Buffer
gauge string
gaugeWidth int
width int
nowVal int
totalVal int
charLen int
format string
prefix string
postfix string
charge string
ShowPercent bool
ShowCounter bool
Showthunder bool
EnableColor bool
EnableTmux bool
}
func digit(num int) string {
return strconv.Itoa(int(math.Log10(float64(num))) + 1)
}
func New(total int) *Bar {
if total <= 0 {
panic(errors.New("Please specify total size that is greater than zero"))
}
bar := &Bar{
buffer: bytes.Buffer{},
totalVal: total,
nowVal: -1,
charLen: len(chars),
format: "%s",
prefix: "|",
postfix: "|",
charge: "ϟ",
ShowPercent: true,
ShowCounter: true,
Showthunder: false,
EnableColor: false,
EnableTmux: true,
}
return bar.SetWidth(3)
}
func (bar *Bar) SetPrefix(char rune) *Bar {
bar.prefix = string(char)
return bar
}
func (bar *Bar) SetPostfix(char rune) *Bar {
bar.postfix = string(char)
return bar
}
func (bar *Bar) SetWidth(width int) *Bar {
bar.width = width
// +1 for postfix
bar.gaugeWidth = width + 1
return bar
}
func (bar *Bar) Set(set int) *Bar {
bar.nowVal = set
return bar
}
func (bar *Bar) Run() {
bar.writer()
}
func (bar *Bar) writer() {
if bar.ShowPercent {
bar.format = "%3d%%" + bar.format
}
if bar.ShowCounter {
digit := digit(bar.totalVal)
bar.format += " %" + digit + "d/%" + digit + "d"
}
if bar.nowVal <= bar.totalVal {
bar.print()
}
}
func (bar *Bar) print() {
frac := float64(bar.nowVal) / float64(bar.totalVal)
if bar.Showthunder {
// append prefix
bar.gauge += bar.prefix
mid := int(math.Ceil(float64(bar.gaugeWidth / 2)))
for i := 1; i < mid; i++ {
bar.gauge += " "
}
// Middle
bar.gauge += bar.charge
for i := mid + 1; i < bar.gaugeWidth; i++ {
bar.gauge += " "
}
// append postfix
bar.gauge += bar.postfix
bar.write(frac)
return
}
barLen, fracBarLen := bar.divmod(frac)
// append prefix
bar.gauge += bar.prefix
// append full block
for i := 1; i < barLen; i++ {
bar.gauge += chars[bar.charLen-1]
}
// append lower block
bar.gauge += chars[fracBarLen]
// padding with whitespace
for i := barLen + 1; i < bar.gaugeWidth; i++ {
bar.gauge += " "
}
// append postfix
bar.gauge += bar.postfix
bar.write(frac)
}
func (bar *Bar) write(frac float64) {
var args []interface{}
percent := int(frac * 100)
if bar.ShowPercent {
args = append(args, percent)
}
args = append(args, string(bar.gauge))
if bar.ShowCounter {
args = append(args, bar.nowVal)
args = append(args, bar.totalVal)
}
if bar.EnableColor {
if bar.EnableTmux {
bar.colorTmuxPrint(percent, args...)
} else {
bar.colorPrint(percent, args...)
}
} else {
fmt.Fprintf(&bar.buffer, bar.format, args...)
}
color.Output.Write(bar.buffer.Bytes())
}
func (bar *Bar) colorTmuxPrint(percent int, args ...interface{}) {
if percent >= 60 {
bar.format = "#[fg=green]" + bar.format
} else if 20 <= percent && percent < 60 {
bar.format = "#[fg=yellow]" + bar.format
} else {
bar.format = "#[fg=red]" + bar.format
}
fmt.Fprintf(&bar.buffer, bar.format+"#[default]", args...)
}
func (bar *Bar) colorPrint(percent int, args ...interface{}) {
if percent >= 60 {
bar.buffer.WriteString(color.GreenString(bar.format, args...))
} else if 20 <= percent && percent < 60 {
bar.buffer.WriteString(color.YellowString(bar.format, args...))
} else {
bar.buffer.WriteString(color.RedString(bar.format, args...))
}
}
func (bar *Bar) divmod(frac float64) (int, int) {
// Over 100%
if frac >= 1.0 {
return bar.width, bar.charLen - 1
}
pre := int(frac * float64(bar.width) * float64(bar.charLen))
return pre/bar.charLen + 1, pre % bar.charLen
}