-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.go
76 lines (48 loc) · 1.39 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
package main
import (
"io/ioutil"
"encoding/json"
"fmt"
"time"
"flag"
"strings"
)
func printStringsWithInterval(interval float64, pattern []string) {
for _, e := range pattern {
duration := time.Duration(interval) * time.Millisecond
time.Sleep(time.Duration(duration))
fmt.Println(e)
}
}
func convertToStringArray(t []interface{}) []string {
s := make([]string, len(t))
for i, v := range t {
s[i] = fmt.Sprint(v)
}
return s
}
//If path is nil, return the spinners.json file in the same directory
func getJsonPath(path string) string {
path = strings.TrimSpace(path)
if path == "" {
path = "./spinners.json"
}
return path
}
func printSpinner(jsonPath string, spinnerPattern string) {
path := getJsonPath(jsonPath)
theJson, _ := ioutil.ReadFile(path)
var data map[string]interface{}
json.Unmarshal([]byte(theJson), &data)
spinnerData := data[spinnerPattern].(map[string]interface{})
interval := spinnerData["interval"].(float64)
frames := spinnerData["frames"].([]interface{})
f := convertToStringArray(frames)
printStringsWithInterval(interval, f)
}
func main() {
jsonPath := flag.String("path", "", "Path of the json file. If argument is nil, return the path of the spinners.json file in the same directory")
spinnerPattern := flag.String("pattern", "", "Use one of the pattern in the .json file")
flag.Parse()
printSpinner(*jsonPath, *spinnerPattern)
}