-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
203 lines (181 loc) · 4.17 KB
/
term.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
const TermWidthDefault = 80
const TermHeightDefault = 24
const ProgressBarWidth = 22
type State uint8
const (
Done State = 0
Failed State = 1
Warning State = 2
Sending State = 3
Receiving State = 4
)
type TtyColor string
const (
ColorReset TtyColor = "\033[0m"
ColorRed TtyColor = "\033[31m"
ColorGreen TtyColor = "\033[32m"
ColorYellow TtyColor = "\033[33m"
ColorBlue TtyColor = "\033[34m"
ColorPurple TtyColor = "\033[35m"
ColorCyan TtyColor = "\033[36m"
ColorWhite TtyColor = "\033[37m"
)
type TtyProgress struct {
relDir string
fileName string
fileSize int64
bytes int64
human int64
percent uint
state State
err error
}
var ErrTtySizeInvalidFormat = errors.New("term: invalid format")
func GetTermSize() (uint, uint) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
return TermWidthDefault, TermHeightDefault
}
width, height, err := parseTermSize(string(out))
if width <= 0 || height <= 0 || err != nil {
return TermWidthDefault, TermHeightDefault
}
return width, height
}
func parseTermSize(input string) (uint, uint, error) {
parts := strings.Split(input, " ")
if len(parts) != 2 {
return 0, 0, ErrTtySizeInvalidFormat
}
height, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, err
}
width, err := strconv.Atoi(strings.Replace(parts[1], "\n", "", 1))
if err != nil {
return 0, 0, err
}
return uint(width), uint(height), nil
}
func NewProgress(relDir string, fileName string, fileSize int64, direction State) *TtyProgress {
return &TtyProgress{
relDir: relDir,
fileName: fileName,
fileSize: fileSize,
bytes: 0,
human: 0,
percent: 0,
state: direction,
}
}
func (p *TtyProgress) Done() {
p.state = Done
p.percent = 100
p.Draw(p.bytes)
}
func (p *TtyProgress) Failed(err error) {
p.state = Failed
p.err = err
p.Draw(p.bytes)
}
func (p *TtyProgress) Warning(err error) {
p.state = Warning
p.err = err
p.Draw(p.bytes)
}
func (p *TtyProgress) Draw(bytes int64) {
p.bytes = bytes
human, metrics := getSizeMetrics(bytes)
var percent uint
if p.fileSize > 0 {
percent = uint((bytes * 100) / p.fileSize)
} else {
percent = 100
}
if p.human != human || percent != p.percent || p.state == Done || p.state == Failed || p.state == Warning {
p.human = human
p.percent = percent
progress := getProgressBar(p.percent)
ttyWidth, _ := GetTermSize()
stateSymbol, stateColor := getStateAttrs(p.state)
stateSymbol = Colored(stateSymbol, stateColor)
filePath := filepath.Join(p.relDir, p.fileName)
switch p.state {
case Failed, Warning:
fileNameWithErr := fixedLengthString(filePath+" → "+p.err.Error(), int(ttyWidth-2))
fmt.Printf("%s %s\r", stateSymbol, fileNameWithErr)
default:
fileName := fixedLengthString(filePath, int(ttyWidth-ProgressBarWidth-21))
fmt.Printf("%s %s %4d %4s [%s] %3d %%\r", stateSymbol, fileName, human, metrics, progress, percent)
}
}
}
func getStateAttrs(direction State) (string, TtyColor) {
switch direction {
case Sending:
return "↓", ColorReset
case Receiving:
return "↑", ColorReset
case Done:
return "✔", ColorGreen
case Warning:
return "↯", ColorYellow
default:
return "✘", ColorRed
}
}
func getProgressBar(percent uint) (bar string) {
for c := 0; c < ProgressBarWidth; c += 1 {
if uint(c*100/ProgressBarWidth) <= percent {
bar += "█"
} else {
bar += "░"
}
}
return
}
func fixedLengthString(str string, length int) string {
orgLen := len(str)
if orgLen <= length {
return str + strings.Repeat(" ", length-orgLen)
}
return "..." + str[orgLen-length+3:orgLen]
}
func getSizeMetrics(size int64) (int64, string) {
var metrics string
if size < 1024 {
metrics = "Byte"
}
if size >= 1024 {
size /= 1024
metrics = "KiB "
}
if size >= 1024 {
size /= 1024
metrics = "MiB "
}
if size >= 1024 {
size /= 1024
metrics = "GiB "
}
if size >= 1024 {
size /= 1024
metrics = "TiB "
}
return size, metrics
}
func Colored(format string, color TtyColor, a ...any) string {
return fmt.Sprintf(string(color)+format+string(ColorReset), a...)
}