forked from common-nighthawk/go-figure
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_methods.go
151 lines (139 loc) · 3.96 KB
/
public_methods.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
package figure
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
)
// IsOldWindows is older Windows OS below Windows 10
var IsOldWindows bool
// Print stdout
func (fig Figure) Print() {
colorful := fig.color != ""
var w io.Writer
for _, printRow := range fig.Slicify() {
if colorful {
if IsOldWindows {
color.NoColor = false
if w == nil {
w = colorable.NewColorableStdout()
}
_, _ = color.New(colorColors[fig.color]).Fprintln(w, printRow)
continue
} else {
printRow = colors[fig.color] + printRow + colors["reset"]
}
}
fmt.Println(printRow)
}
}
// ColorString returns a colored string
func (fig Figure) ColorString() string {
s := ""
for _, printRow := range fig.Slicify() {
if fig.color != "" {
printRow = colors[fig.color] + printRow + colors["reset"]
}
s += fmt.Sprintf("%s\n", printRow)
}
return s
}
func (fig Figure) String() string {
s := ""
for _, printRow := range fig.Slicify() {
s += fmt.Sprintf("%s\n", printRow)
}
return s
}
// Scroll A figure responds to the func Scroll, taking three arguments.
// duration is the total time the banner will display, in milliseconds.
// stillness is the length of time the text will not move (also in ms).
// Therefore, the lower the stillness the faster the scroll speed.
// direction can be either "right" or "left" (case-insensitive).
// The direction will be left if an invalid option (e.g. "foo") is passed.
// There is no return value.
func (fig Figure) Scroll(duration, stillness int, direction string) {
endTime := time.Now().Add(time.Duration(duration) * time.Millisecond)
fig.phrase = fig.phrase + " "
clearScreen()
for time.Now().Before(endTime) {
var shiftedPhrase string
chars := []byte(fig.phrase)
if strings.HasPrefix(strings.ToLower(direction), "r") {
shiftedPhrase = string(append(chars[len(chars)-1:], chars[0:len(chars)-1]...))
} else {
shiftedPhrase = string(append(chars[1:], chars[0]))
}
fig.phrase = shiftedPhrase
fig.Print()
sleep(stillness)
clearScreen()
}
}
// Blink A figure responds to the func Blink, taking three arguments.
// duration is the total time the banner will display, in milliseconds.
// timeOn is the length of time the text will blink on (also in ms).
// timeOff is the length of time the text will blink off (ms).
// For an even blink, set timeOff to -1 (same as setting timeOff to the value of timeOn).
// There is no return value.
func (fig Figure) Blink(duration, timeOn, timeOff int) {
if timeOff < 0 {
timeOff = timeOn
}
endTime := time.Now().Add(time.Duration(duration) * time.Millisecond)
clearScreen()
for time.Now().Before(endTime) {
fig.Print()
sleep(timeOn)
clearScreen()
sleep(timeOff)
}
}
// Dance A figure responds to the func Dance, taking two arguments.
// duration is the total time the banner will display, in milliseconds.
// freeze is the length of time between dance moves (also in ms).
// Therefore, the lower the freeze the faster the dancing.
// There is no return value.
func (fig Figure) Dance(duration, freeze int) {
endTime := time.Now().Add(time.Duration(duration) * time.Millisecond)
font := fig.font //TODO: change to deep copy
font.evenLetters()
figures := []Figure{{font: font}, {font: font}}
clearScreen()
for i, c := range fig.phrase {
appends := []string{" ", " "}
appends[i%2] = string(c)
for f := range figures {
figures[f].phrase = figures[f].phrase + appends[f]
}
}
for p := 0; time.Now().Before(endTime); p ^= 1 {
figures[p].Print()
figures[1-p].Print()
sleep(freeze)
clearScreen()
}
}
// Write writers
func Write(w io.Writer, fig Figure) {
for _, printRow := range fig.Slicify() {
_, _ = fmt.Fprintf(w, "%v\n", printRow)
}
}
// helpers
func clearScreen() {
if IsOldWindows {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
_ = cmd.Run()
} else {
fmt.Print("\033[H\033[2J")
}
}
func sleep(milliseconds int) {
time.Sleep(time.Duration(milliseconds) * time.Millisecond)
}