-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
207 lines (184 loc) · 4.61 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
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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"time"
"github.com/aqatl/mal/anilist"
"github.com/aqatl/mal/mal"
"github.com/fatih/color"
)
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 getDataDir() string {
// Check for old cache dir at $HOME/.mal
if usr, err := user.Current(); err == nil {
dir := filepath.Join(usr.HomeDir, ".mal")
if _, err := os.Stat(dir); err == nil {
return dir
} else {
if !os.IsNotExist(err) {
log.Printf("Error probing for %s: %v", dir, err)
}
}
} else {
log.Printf("Error getting current user: %v. ignoring", err)
}
// Old cache dir not present, use user config dir
dataDir, err := os.UserConfigDir()
if err != nil {
log.Printf("Error getting user config dir: %v", err)
return ""
}
return filepath.Join(dataDir, "mal")
}
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 int, score float32, scoreFormat anilist.ScoreFormat, lastUpdated time.Time) {
var scorePattern string
if scoreFormat == anilist.Point10Decimal {
scorePattern = "%.1f"
} else {
scorePattern = "%.f"
}
titleStr := color.HiYellowString("%s", title)
episodesStr := color.HiRedString("%d/%d", watchedEps, eps)
scoreStr := color.HiRedString(scorePattern, 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 int, score float32, scoreFormat anilist.ScoreFormat, lastUpdated time.Time) {
var scorePattern string
if scoreFormat == anilist.Point10Decimal {
scorePattern = "%.1f"
} else {
scorePattern = "%.f"
}
titleStr := color.HiYellowString("%s", title)
episodesBeforeStr := color.HiRedString("%d/%d", epsBefore, eps)
episodesAfterStr := color.HiRedString("%d/%d", epsNow, eps)
scoreStr := color.HiRedString(scorePattern, 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,
float32(int(entry.MyScore)),
anilist.Point10,
time.Unix(entry.LastUpdated, 0))
}
func malPrintEntryDetailsAfterUpdatedEpisodes(entry *mal.Anime, epsBefore int) {
printEntryDetailsAfterUpdatedEpisodes(
entry.Title,
entry.MyStatus.String(),
epsBefore,
entry.WatchedEpisodes,
entry.Episodes,
float32(int(entry.MyScore)),
anilist.Point10,
time.Unix(entry.LastUpdated, 0))
}
func alPrintEntryDetails(entry *anilist.MediaListEntry, scoreFormat anilist.ScoreFormat) {
printEntryDetails(entry.Title.UserPreferred,
entry.Status.String(),
entry.Progress,
entry.Episodes,
entry.Score,
scoreFormat,
time.Unix(int64(entry.UpdatedAt), 0))
}
func alPrintEntryDetailsAfterUpdatedEpisodes(entry *anilist.MediaListEntry, epsBefore int, scoreFormat anilist.ScoreFormat) {
printEntryDetailsAfterUpdatedEpisodes(
entry.Title.UserPreferred,
entry.Status.String(),
epsBefore,
entry.Progress,
entry.Episodes,
entry.Score,
scoreFormat,
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)
}
}
if os.IsNotExist(err) {
return false
}
panic(err)
}
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)
}