-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
198 lines (180 loc) · 3.82 KB
/
util.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strings"
"time"
)
func ensure(c bool) {
if !c {
log.Fatalln("assertion failed")
}
}
func checkDir(artist string, album string) bool {
dirs, err := os.ReadDir(filepath.Join(config.Library.Root, artist))
if err != nil {
return false
}
for _, dir := range dirs {
if strings.HasPrefix(dir.Name(), album) {
fmt.Println(filepath.Join(artist, dir.Name()))
return true
}
}
return false
}
func checkRelPaths(items []string) {
// stop checking as soon as one entry is valid. errors are only logged;
// no action is taken
for _, item := range items {
info, err := os.Stat(filepath.Join(config.Library.Root, item))
if err != nil {
log.Println("not exist:", item)
continue
}
if !info.IsDir() {
log.Println("not dir:", item)
continue
}
break
}
}
func timer(name string) func() {
// https://stackoverflow.com/a/45766707
start := time.Now() // at time of defer
return func() {
dur := time.Since(start) // at end of func scope
if dur >= time.Second {
log.Printf(
"%s took %v\n",
name,
dur,
)
}
}
}
func intRange(n int) []int {
ints := make([]int, n)
for i := range n {
ints[i] = i
}
return ints
}
// base should always be a valid absolute path
//
// returns basenames of immediate children
func descend(base string) ([]string, error) {
entries, err := os.ReadDir(base)
if err != nil {
// TODO: remove from queue
// log.Println("not a valid dir:", base)
return []string{}, err
}
// ch := []string{}
ch := make([]string, len(entries))
for i, e := range entries {
ch[i] = e.Name()
}
return ch, nil
}
// Pretty-print arbitrary http (json) response without needing to know its
// schema
//
// Warning: resp will be closed
// func debugResponse(resp *http.Response) {
// defer resp.Body.Close()
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// panic(err)
// }
// var data map[string]any
// if err := json.Unmarshal(body, &data); err != nil {
// panic(err)
// }
// x, _ := json.MarshalIndent(data, "", " ")
// fmt.Println(string(x))
// }
// "a", "b [c]"
// "a c", "b"
func movePerfsToArtist(artist string, album string) (string, string) {
// TODO:
// strings.LastIndex(album, " [")
x := strings.SplitN(album, " [", 2) // "b", "c]"
perfs := strings.TrimSuffix(x[1], "]")
return artist + " " + perfs, x[0]
}
// Sort a slice of albums by year suffix (" (YYYY)"). Sorting is performed
// inplace.
func sortByYear(albums []string) {
slices.SortFunc(albums, func(a string, b string) int {
if a[len(a)-1] != ')' {
return -1
}
if b[len(b)-1] != ')' {
return 1
}
ay := a[len(a)-5:]
by := b[len(b)-5:]
switch {
case ay < by:
return -1
case ay > by:
return 1
default:
return 0
}
})
}
// generics {{{
// If target is not found in the dereferenced slice, the slice is unchanged.
// This is not an in-place operation (due to language constraints?).
//
// Warning: for performance, order is not preserved!
func remove[T comparable](ptr *[]T, target T) *[]T {
s := *ptr
for i, item := range s {
if item == target {
// swap last item with target, to prevent costly
// shifting of items
// https://stackoverflow.com/a/37335777
s[i] = s[len(s)-1]
// re-assignment always reallocates, so a new ptr must
// be returned
// https://stackoverflow.com/a/56394697
s = s[:len(s)-1]
return &s
}
}
return ptr
}
func Map[T any](items []T, f func(T) T) []T {
var out []T
for _, i := range items {
out = append(out, f(i))
}
return out
}
func anyValue[T comparable](m map[T]bool) bool {
if len(m) == 0 {
return false
}
for _, v := range m {
if v {
return true
}
}
return false
}
func intersect[T comparable](a []T, b []T) []T {
var inter []T
for _, x := range a {
if slices.Contains(b, x) {
inter = append(inter, x)
}
}
return inter
}
// }}}