-
Notifications
You must be signed in to change notification settings - Fork 1
/
bw.go
160 lines (135 loc) · 2.94 KB
/
bw.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
package bw
import (
"fmt"
"math"
"strconv"
"github.com/fatih/color"
)
type BubbleWrap struct {
numbers int
column int
bubbles []bool
before string
after string
beforeColor color.Color
afterColor color.Color
interrupted bool
completed bool
}
const cursorHide = "\033[?25l"
const cursorShow = "\033[?25h"
func NewBubbleWrap(numbers int) *BubbleWrap {
bw := new(BubbleWrap)
bw.printCopyRight()
if numbers <= 0 {
return nil
} else {
bw.numbers = numbers
}
bw.column = 50
bw.before = "*"
bw.after = "."
bw.bubbles = make([]bool, bw.numbers)
return bw
}
func (bw *BubbleWrap) SetColumn(column int) *BubbleWrap {
bw.column = column
return bw
}
func (bw *BubbleWrap) ChangeBubbleShape(before string, after string) *BubbleWrap {
bw.before = before
bw.after = after
return bw
}
func (bw *BubbleWrap) ChangeBubbleColor(before *color.Color, after *color.Color) *BubbleWrap {
bw.beforeColor = *before
bw.afterColor = *after
return bw
}
func (bw *BubbleWrap) Pop(i int) {
if bw.interrupted || bw.completed {
return
}
bw.bubbles[i] = true
}
func (bw *BubbleWrap) Display() {
if bw.interrupted || bw.completed {
return
}
// display bubbles
var c, comp int
for _, v := range bw.bubbles {
if v {
if &bw.beforeColor != nil {
bw.beforeColor.Printf(cursorHide+"%v", bw.before)
} else {
fmt.Printf(cursorHide+"%v", bw.before)
}
comp++
} else {
if &bw.afterColor != nil {
bw.afterColor.Printf(cursorHide+"%v", bw.after)
} else {
fmt.Printf(cursorHide+"%v", bw.after)
}
}
c++
if c == bw.column {
fmt.Printf(cursorHide + "\n")
c = 0
} else if bw.numbers < bw.column && c == bw.numbers {
break
}
}
// display progress
fmt.Printf(cursorHide + "\n")
var space int
if bw.numbers < bw.column {
space = bw.numbers
} else {
space = bw.column
}
for i := 0; i < space-4; i++ {
fmt.Printf(cursorHide + " ")
}
persent := (float64(comp) / float64(bw.numbers)) * 100.0
trunc := math.Trunc(persent)
fmt.Printf(cursorHide+"%3v%% \n", trunc)
for i := 0; i < space-len(strconv.Itoa(comp))-len(strconv.Itoa(bw.numbers))-3; i++ {
fmt.Printf(" ")
}
fmt.Printf(cursorHide+"%d / %d", comp, bw.numbers)
// output result
if trunc == 100 {
bw.completed = true
fmt.Printf(cursorShow + "\n\nBubble Wrape finished.\n")
}
}
func (bw *BubbleWrap) Clear() {
if bw.interrupted || bw.completed {
return
}
var c, r int
r = int(bw.numbers / bw.column)
c = bw.numbers + 2
// move cursor to start point
fmt.Printf(cursorHide+"\033[%dA", r+2)
fmt.Printf(cursorHide+"\033[%dD", c)
}
func (bw *BubbleWrap) Redisplay() {
bw.Clear()
bw.Display()
}
func (bw *BubbleWrap) Interrupt() {
if bw.interrupted || bw.completed {
return
}
bw.interrupted = true
fmt.Printf(cursorShow + "\n\nBubble Wrape interrupted.\n")
}
func (bw *BubbleWrap) printCopyRight() {
fmt.Printf("Bubble Wrap %v by kura.\n\n", bw.version())
}
func (bw *BubbleWrap) version() string {
return "1.0.7"
}