-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgopher.go
More file actions
194 lines (170 loc) · 3.69 KB
/
gopher.go
File metadata and controls
194 lines (170 loc) · 3.69 KB
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
// Package gopher implements little gophers for the command-line.
// The gophers can be used as loadings spinners. Gophers can be
// drawn in different colors and activities.
package gopher
import (
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/mattn/go-colorable"
)
// Gopher template string `( ◔ ౪◔)´
const (
gopher = "%s \033[%dm`( %s ౪%s )´\033[m %s"
)
// Activity is a type for the gopher activity.
type Activity uint8
//go:generate stringer -type=Activity
const (
Waiting Activity = iota
Wondering
Boring
Loving
)
// Character sets for activities.
const (
waiting = "◔●"
wondering = "⊙●"
boring = "◷◶◵◴"
loving = "♡❤"
)
// State is a type for the gopher status.
type state uint8
const (
stopped state = iota
running
)
// Color defines a single SGR Code.
type Color int
// Supported gopher colors.
const (
Black Color = iota + 30
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
const escape = "\x1b"
// Gopher holds the acting gopher.
type Gopher struct {
mu sync.RWMutex
delay time.Duration // motion delay
prefix string // prefix message
suffix string // suffix message
activity Activity // gopher activity
color Color // gopher color
state state // current state
w io.Writer // writer interface
done chan struct{} // channel to stop the gopher
}
// New creates a new gopher with default values.
func New() *Gopher {
g := &Gopher{
delay: 1000 * time.Millisecond,
activity: Waiting,
color: White,
w: colorable.NewColorableStdout(),
done: make(chan struct{}, 1),
}
return g
}
// Start starts the gopher.
func (g *Gopher) Start() {
if g.state == running {
return
}
g.state = running
go func() {
for {
runes, err := g.runes()
if err != nil {
panic(err)
}
for i := 0; i < len(runes); i++ {
select {
case <-g.done:
return
default:
g.clearOutput()
fmt.Fprintf(g.w, ("\r" + gopher), g.prefix, g.color, string(runes[i]), string(runes[i]), g.suffix)
time.Sleep(g.delay)
}
}
}
}()
}
// Stop stops the gopher.
func (g *Gopher) Stop() {
if g.state == running {
g.done <- struct{}{}
g.state = stopped
g.finalize()
}
}
// SetDelay sets the gophers spinning delay.
func (g *Gopher) SetDelay(d time.Duration) {
g.mu.Lock()
defer g.mu.Unlock()
g.delay = d
}
// SetActivity sets the gophers activity.
func (g *Gopher) SetActivity(a Activity) {
g.mu.Lock()
defer g.mu.Unlock()
g.activity = a
}
// SetColor sets the gophers color.
func (g *Gopher) SetColor(c Color) {
g.mu.Lock()
defer g.mu.Unlock()
g.color = c
}
// SetPrefix sets the prepended text.
func (g *Gopher) SetPrefix(s string) {
g.mu.Lock()
defer g.mu.Unlock()
g.prefix = s
}
// SetSuffix sets the appended text.
func (g *Gopher) SetSuffix(s string) {
g.mu.Lock()
defer g.mu.Unlock()
g.suffix = s
}
// String prints a gopher.
func (g *Gopher) String() string { return "`( ◔ ౪◔)´" }
// Returns the runes for the current activity.
func (g *Gopher) runes() ([]rune, error) {
g.mu.RLock()
defer g.mu.RUnlock()
switch g.activity {
case Waiting:
return []rune(waiting), nil
case Wondering:
return []rune(wondering), nil
case Boring:
return []rune(boring), nil
case Loving:
return []rune(loving), nil
default:
return nil, errors.New("unknown activity")
}
}
// Resets all escape attributes and clears the output.
func (g *Gopher) clearOutput() {
g.mu.RLock()
defer g.mu.RUnlock()
// clear output
fmt.Fprintf(g.w, "\033[%dD", len(g.prefix+gopher+g.suffix))
// reset escape attributes
fmt.Fprintf(g.w, "%s[K", escape)
}
// Finalizes output.
func (g *Gopher) finalize() {
fmt.Fprintf(g.w, "\r `( ◔ ౪◔)´ I'm done ...\n")
}