-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
174 lines (155 loc) · 3.69 KB
/
utils.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aqatl/mal/anilist"
"github.com/aqatl/mal/mal"
"github.com/fatih/color"
"log"
"os"
"os/user"
"time"
)
func basicAuth(username, password string) string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
}
func reverseAnimeSlice(s []*mal.Anime) {
last := len(s) - 1
for i := 0; i < len(s)/2; i++ {
s[i], s[last-i] = s[last-i], s[i]
}
}
func homeDir() string {
usr, err := user.Current()
if err != nil {
log.Printf("Error getting current user: %v", err)
return ""
}
return usr.HomeDir
}
func chooseStrFromSlice(alts []string) string {
if length := len(alts); length == 1 {
return alts[0]
} else if length == 0 {
return ""
}
for i, synonym := range alts {
fmt.Printf("%2d. %s\n", i+1, synonym)
}
idx := 0
scan := func() {
fmt.Scan(&idx)
}
for scan(); idx <= 0 || idx > len(alts); {
fmt.Print("\rInvalid input. Try again: ")
scan()
}
return alts[idx-1]
}
func printEntryDetails(title, status string, watchedEps, eps, score int, lastUpdated time.Time) {
titleStr := color.HiYellowString("%s", title)
episodesStr := color.HiRedString("%d/%d", watchedEps, eps)
scoreStr := color.HiRedString("%d", score)
statusStr := color.HiRedString("%s", status)
lastUpdatedStr := color.HiRedString("%v", lastUpdated)
fmt.Fprintf(
color.Output,
"Title: %s\n"+
"Episodes: %s\n"+
"Score: %s\n"+
"Status: %v\n"+
"Last updated: %v\n",
titleStr,
episodesStr,
scoreStr,
statusStr,
lastUpdatedStr,
)
}
func printEntryDetailsAfterUpdatedEpisodes(title, status string, epsBefore, epsNow, eps, score int, lastUpdated time.Time) {
titleStr := color.HiYellowString("%s", title)
episodesBeforeStr := color.HiRedString("%d/%d", epsBefore, eps)
episodesAfterStr := color.HiRedString("%d/%d", epsNow, eps)
scoreStr := color.HiRedString("%d", score)
statusStr := color.HiRedString("%s", status)
lastUpdatedStr := color.HiRedString("%v", lastUpdated)
fmt.Fprintf(
color.Output,
"Title: %s\n"+
"Episodes: %s -> %s\n"+
"Score: %s\n"+
"Status: %v\n"+
"Last updated: %v\n",
titleStr,
episodesBeforeStr,
episodesAfterStr,
scoreStr,
statusStr,
lastUpdatedStr,
)
}
func malPrintEntryDetails(entry *mal.Anime) {
printEntryDetails(
entry.Title,
entry.MyStatus.String(),
entry.WatchedEpisodes,
entry.Episodes,
int(entry.MyScore),
time.Unix(entry.LastUpdated, 0))
}
func malPrintEntryDetailsAfterUpdatedEpisodes(entry *mal.Anime, epsBefore int) {
printEntryDetailsAfterUpdatedEpisodes(
entry.Title,
entry.MyStatus.String(),
epsBefore,
entry.WatchedEpisodes,
entry.Episodes,
int(entry.MyScore),
time.Unix(entry.LastUpdated, 0))
}
func alPrintEntryDetails(entry *anilist.MediaListEntry) {
printEntryDetails(entry.Title.UserPreferred,
entry.Status.String(),
entry.Progress,
entry.Episodes,
entry.Score,
time.Unix(int64(entry.UpdatedAt), 0))
}
func alPrintEntryDetailsAfterUpdatedEpisodes(entry *anilist.MediaListEntry, epsBefore int) {
printEntryDetailsAfterUpdatedEpisodes(
entry.Title.UserPreferred,
entry.Status.String(),
epsBefore,
entry.Progress,
entry.Episodes,
entry.Score,
time.Unix(int64(entry.UpdatedAt), 0))
}
//Returns true if file was loaded correctly
func LoadJsonFile(file string, i interface{}) bool {
f, err := os.Open(file)
defer f.Close()
if err == nil {
err = json.NewDecoder(f).Decode(i)
if err == nil {
return true
} else {
panic(err)
return false
}
}
if os.IsNotExist(err) {
return false
}
panic(err)
return false
}
func SaveJsonFile(file string, i interface{}) error {
f, err := os.Create(file)
defer f.Close()
if err != nil {
return err
}
return json.NewEncoder(f).Encode(i)
}