-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.go
140 lines (121 loc) · 2.9 KB
/
transformer.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
package shrinker
import (
"math"
"os"
"path/filepath"
"strings"
"unicode"
)
type Mode int
const (
ModeAmbiguous Mode = iota + 1
ModeShort
)
type Config struct {
PreserveLast bool
Shorten bool
ReplaceTilde bool
Mode Mode
}
type Transformer interface {
Transform(input []string) ([]string, error)
}
type ReplaceTildeTransformer struct {
HomeDir string
}
func (tt *ReplaceTildeTransformer) Transform(input []string) ([]string, error) {
path := strings.Join(input, string(os.PathSeparator))
path = strings.Replace(path, tt.HomeDir, "~", -1)
return strings.Split(path, string(os.PathSeparator)), nil
}
type ReadDirFunc func(dirname string) ([]os.DirEntry, error)
type AmbiguousTransformer struct {
StartDir string
ReadDirFunc ReadDirFunc
}
func (at *AmbiguousTransformer) getAmbiguousName(parent, target string) (string, error) {
if target == "" {
return "", nil
}
result := ""
files, err := at.ReadDirFunc(parent)
if err != nil {
return "", err
}
for _, f := range files {
if !f.IsDir() {
continue
}
if f.Name() == target {
continue
}
nameRunes := []rune(f.Name())
targetRunes := []rune(target)
maxLength := int(math.Max(float64(len(nameRunes)), float64(len(targetRunes))))
minLength := int(math.Min(float64(len(nameRunes)), float64(len(targetRunes))))
a := make([]rune, 0, maxLength)
previousMatched := true
var i int
for i = 0; i < minLength; i++ {
// Compare character case insensitively
if previousMatched && unicode.ToLower(nameRunes[i]) == unicode.ToLower(targetRunes[i]) {
a = append(a, targetRunes[i])
} else {
// TODO: maybe a bug
previousMatched = false //nolint:ineffassign
break
}
}
if i < len(target)-1 {
// Append additional 1 rune to distinguish name
a = append(a, targetRunes[i])
}
if len(a) > len(result) {
result = string(a)
}
}
if len(result) == 0 {
result = string([]rune(target)[0])
}
return result, nil
}
func (at *AmbiguousTransformer) Transform(input []string) ([]string, error) {
parent := at.StartDir
result := make([]string, 0, len(input))
for _, dir := range input {
if dir == "" {
continue
}
// TODO: goroutine
name, err := at.getAmbiguousName(parent, dir)
if err != nil {
return nil, err
}
result = append(result, name)
parent = filepath.Join(parent, dir)
}
return result, nil
}
type ShortenTransformer struct{}
func (st *ShortenTransformer) Transform(input []string) ([]string, error) {
length := len(input)
result := make([]string, 0, length)
for _, v := range input {
runes := []rune(v)
if len(runes) == 0 {
continue
}
result = append(result, string(runes[0]))
}
return result, nil
}
type PreserveLastTransformer struct {
Last string
}
func (plt *PreserveLastTransformer) Transform(input []string) ([]string, error) {
result := make([]string, len(input))
copy(result, input)
length := len(input)
result[length-1] = plt.Last
return result, nil
}