-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
79 lines (59 loc) · 1.32 KB
/
entry.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
package retrosort
import (
"regexp"
"strings"
"path/filepath"
)
func getPrefix(fn string, prefixSize int) string {
fn = strings.ToLower(filepath.Base(fn))
if len(fn) < prefixSize {
return fn
}
return fn[:prefixSize]
}
func getCategory(fn string) string {
c := strings.ToLower(filepath.Base(fn))[0]
if c >= 'a' && c <= 'z' {
return string(c)
}
return "#"
}
// entry represents a single entry in the output tree
// name is the file name for a single source
// or the group name for multiple sources
// sources is a list of paths to copy from
type entry struct {
name string
sortName string
sources []string
}
var sortNameRe = regexp.MustCompile(`[^a-z0-9]+`)
func newEntry(name string, sources []string) entry {
sortName := strings.ToLower(name)
sortName = sortNameRe.ReplaceAllString(sortName, "_")
return entry{
name: name,
sortName: sortName,
sources: sources,
}
}
func (e entry) prefix(size int) string {
if size == 1 {
return getCategory(e.sortName[:size])
}
if size >= len(e.sortName) {
return e.sortName
}
return e.sortName[:size]
}
func (e entry) fileMap() map[string]string {
out := make(map[string]string)
for _, source := range e.sources {
path := filepath.Base(source)
if len(e.sources) > 1 {
path = filepath.Join(e.name, path)
}
out[source] = path
}
return out
}