-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
149 lines (121 loc) · 2.82 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"time"
)
func captureSigint() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
// Reset the TERM and exit
fmt.Println("\033[0m\033c")
os.Exit(0)
}()
}
func termSize() (int, int) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
size, _ := cmd.Output()
termWidth, _ := strconv.Atoi(strings.TrimSpace(strings.Split(string(size), " ")[1]))
termHeight, _ := strconv.Atoi(strings.Split(string(size), " ")[0])
return termWidth, termHeight
}
func playAudio() {
audio, _ := filepath.Abs("assets/audio.mp3")
cmd := exec.Command("mpg123", "-loop 0", "-q", audio)
cmd.Start()
}
func printTime(startTime time.Time, animWidth int) {
message := fmt.Sprintf("You have nyaned for %.f seconds!", time.Since(startTime).Seconds())
padding := (animWidth - (len(message) + 4)) / 2
fmt.Print(strings.Repeat(" ", padding))
fmt.Printf("\033[1;37;17m%s", message)
}
func main() {
var mute bool
var hideTime bool
// Parse options
flag.BoolVar(&mute, "m", false, "Don't play audio")
flag.BoolVar(&hideTime, "n", false, "Don't show the time nyaned")
flag.Parse()
// Set output character
const outputChar = " "
// Set colors
colors := map[string]string{
"+": "226",
"@": "223",
",": "17",
"-": "205",
"#": "82",
".": "15",
"$": "219",
"%": "217",
";": "99",
"&": "214",
"=": "39",
"'": "0",
">": "196",
"*": "245",
}
// Import frames from data file
framesFile, _ := filepath.Abs("assets/frames.json")
data, _ := ioutil.ReadFile(framesFile)
var frames [][]string
json.Unmarshal(data, &frames)
// Get TTY size
termWidth, termHeight := termSize()
// Calculate the width in terms of the output char
termWidth = termWidth / len(outputChar)
minRow := 0
maxRow := len(frames[0])
minCol := 0
maxCol := len(frames[0][0])
if maxRow > termHeight {
minRow = (maxRow - termHeight) / 2
maxRow = minRow + termHeight
}
if maxCol > termWidth {
minCol = (maxCol - termWidth) / 2
maxCol = minCol + termWidth
}
// Calculate the final animation width
animWidth := (maxCol - minCol) * len(outputChar)
// Initialize term
fmt.Print("\033[H\033[2J\033[?25l")
// Get start time
startTime := time.Now()
// Capture SIGINT
captureSigint()
if !mute {
// Play music
playAudio()
}
for {
for _, frame := range frames {
// Print the next frame
for _, line := range frame[minRow:maxRow] {
for _, char := range line[minCol:maxCol] {
fmt.Printf("\033[48;5;%sm%s", colors[string(char)], outputChar)
}
fmt.Println("\033[m")
}
if !hideTime {
// Print the time so far
printTime(startTime, animWidth)
}
// Reset the frame and sleep
fmt.Print("\033[H")
time.Sleep(90 * time.Millisecond)
}
}
}