-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
68 lines (57 loc) · 2.05 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
package itunes2subsonic
import (
"math/rand"
"os"
"path/filepath"
"strings"
pb "github.com/schollz/progressbar/v3"
)
// PbWithOptions applies options to a progressbar. I like the default, but the
// saucer is something that doesn't render well in my terminal.
func PbWithOptions(p *pb.ProgressBar) *pb.ProgressBar {
pb.OptionSetTheme(pb.Theme{Saucer: "=", SaucerPadding: " ", BarStart: "[", BarEnd: "]"})(p)
return p
}
// longestLibraryPrefix returns the longest prefix that would match the given 2
// paths assuming they were the same file. If nothing matches, returns the input
// strings.
func longestLibraryPrefix(a, b string) (string, string) {
var aDir, bDir string
for aDir, bDir = a, b; aDir != "" && bDir != ""; {
ad, af := filepath.Split(strings.TrimSuffix(aDir, string(os.PathSeparator)))
bd, bf := filepath.Split(strings.TrimSuffix(bDir, string(os.PathSeparator)))
if af != bf {
break
}
aDir, bDir = ad, bd
}
return aDir, bDir
}
// LibraryPrefix finds the most likely library root for the given src and dst
// assuming that both libraries contain mostly the same music.
//
// Note: All paths normalized to lower case.
func LibraryPrefix(src, dst []SongInfo) (string, string) {
// Pick an obviously wrong suffix as an empty string indicates matching libraries.
wrong := "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
srcPrefix, dstPrefix := wrong, wrong
// Sample at random as the beginning or end of the libraries may not line up. Pick a limit because this is an expensive scan.
for i := 0; i < 500; i++ {
// User lower case for systems like Windows where case can change without triggering a library update.
srcP := strings.ToLower(src[rand.Intn(len(src))].Path())
for _, d := range dst {
dstP := strings.ToLower(d.Path())
if srcP == "" || dstP == "" {
continue
}
sp, dp := longestLibraryPrefix(srcP, dstP)
if len(sp) < len(srcPrefix) {
srcPrefix = sp
}
if len(dp) < len(dstPrefix) {
dstPrefix = dp
}
}
}
return srcPrefix, dstPrefix
}