-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
228 lines (184 loc) · 5.11 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/fatih/color"
"github.com/inancgumus/screen"
"golang.org/x/crypto/ssh/terminal"
)
type window struct {
columns int
rows int
fd int
}
type row struct {
rowNumber int
char rune
}
func (r *row) incrementRowNumber() {
r.rowNumber += 1
}
func getTerminalAttr() window {
defTerminal := window{80, 20, int(os.Stdin.Fd())}
width, height, err := terminal.GetSize(defTerminal.fd)
if err != nil {
return defTerminal
}
defTerminal.columns = width
defTerminal.rows = height
return defTerminal
}
func getFlake(c rune) rune {
if c != 0 {
return c
}
flakes := []int{10048, 10049, 10050, 10051, 10053, 10054, 10056}
if runtime.GOOS != "windows" {
flakes = append(flakes, 10052, 10055)
}
flake := flakes[rand.Intn(len(flakes))]
return rune(flake)
}
func printSnowflakeCol(snowflakes map[int]*row, col int, colour string) {
printCharacter(col, snowflakes[col].rowNumber, snowflakes[col].char, colour)
}
func printCharacter(x int, y int, character rune, colour string) {
output := fmt.Sprintf("\033[%d;%dH%c", y, x, character)
c := color.New(selectColour(colour))
c.Print(output)
fmt.Print("\033[1;1H")
fmt.Print("\033[0m")
}
func getRandomColour() string {
colours := []string{
"blue",
"cyan",
"green",
"magenta",
"red",
"white",
"yellow",
}
return colours[rand.Intn(len(colours))]
}
func selectColour(colour string) color.Attribute {
c := strings.ToLower(colour)
if c == "rainbow" {
c = getRandomColour()
}
switch c {
case "blue":
return color.FgHiBlue
case "cyan":
return color.FgHiCyan
case "green":
return color.FgHiGreen
case "magenta":
return color.FgHiMagenta
case "red":
return color.FgHiRed
case "white":
return color.FgHiWhite
case "yellow":
return color.FgHiYellow
default:
return color.FgHiWhite
}
}
func moveFlake(snowflakes map[int]*row, currentRows map[int]int, col int, stack bool, color string, term window, particle rune) {
currentRow := term.rows
if stack {
if _, found := currentRows[col]; !found {
currentRows[col] = currentRow
}
currentRow = currentRows[col]
if currentRow == 1 {
currentRow = term.rows
currentRows[col] = currentRow
}
if snowflakes[col].rowNumber+1 == currentRow {
currentRows[col]--
}
}
// If next row is the end, lets start a new snow flake
if snowflakes[col].rowNumber+1 == currentRow {
char := getFlake(particle)
snowflakes[col] = &row{0, char}
printSnowflakeCol(snowflakes, col, color)
} else {
fmt.Printf("\033[%d;%dH ", snowflakes[col].rowNumber, col)
snowflakes[col].incrementRowNumber()
printSnowflakeCol(snowflakes, col, color)
}
}
func setParticle(p *string) rune {
if len(*p) > 0 {
return []rune(*p)[0]
}
return rune(0)
}
func getMode(m []string) (string, error) {
var retMode string
if len(m[1:]) > 0 {
retMode = strings.ToLower(m[1])
if retMode == "tree" || retMode == "snow" {
return retMode, nil
}
}
return "", fmt.Errorf("supported commands are 'snow' and 'tree'")
}
func main() {
snowCmd := flag.NewFlagSet("snow", flag.ExitOnError)
stackFlag := snowCmd.Bool("stack", false, "Set snow to pile up.")
speedFlag := snowCmd.Int("speed", 14, "Increase to make it snow faster.")
particleFlag := snowCmd.String("particle", "", "Change the particle used.")
colourFlag := snowCmd.String("colour", "white", "Change the colour of the particles. [red|green|blue|magenta|cyan|yellow]")
treeCmd := flag.NewFlagSet("tree", flag.ExitOnError)
lightDelayFlag := treeCmd.Int("light-delay", 1, "Seconds between light changes")
treeColourFlag := treeCmd.String("colour", "green", "Change the colour of the snow particles. [red|green|blue|magenta|cyan|yellow]")
lightColourFlag := treeCmd.String("light-colour", "rainbow", "Change the color of the lights. [red|green|blue|magenta|cyan|yellow]")
treeParticleFlag := treeCmd.String("particle", "*", "Change the particle used for the tree.")
snowFlag := treeCmd.Bool("snow", true, "Whether snow should fall.")
snowColourFlag := treeCmd.String("snow-colour", "white", "Change the colour of the snow particles. [red|green|blue|magenta|cyan|yellow]")
snowParticleFlag := treeCmd.String("snow-particle", "", "Change the snow particle used.")
snowSpeedFlag := treeCmd.Int("snow-speed", 14, "Increase to make it snow faster.")
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
screen.Clear()
os.Exit(0)
}()
mode, err := getMode(os.Args)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
switch mode {
case "tree":
treeCmd.Parse(os.Args[2:])
particle := setParticle(snowParticleFlag)
treeParticle := setParticle(treeParticleFlag)
tree := &tree{
lightDelay: *lightDelayFlag,
colour: *treeColourFlag,
lightsColour: *lightColourFlag,
particle: treeParticle,
snow: *snowFlag,
snowColour: *snowColourFlag,
snowSpeed: *snowSpeedFlag,
snowParticle: particle}
tree.show()
default:
snowCmd.Parse(os.Args[2:])
particle := setParticle(particleFlag)
snow := &snow{*speedFlag, *stackFlag, particle, *colourFlag}
snow.show()
}
}