-
Notifications
You must be signed in to change notification settings - Fork 0
/
dig.go
57 lines (50 loc) · 1.38 KB
/
dig.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
package complete
import (
"golang.org/x/net/context"
"log"
"strings"
"github.com/c-bata/go-prompt"
"github.com/koooyooo/soi-go/pkg/cli/soiprompt/utils"
)
// digCmd はppコマンド系のSuggestを提示します
func (c *Completer) digCmd(d prompt.Document) []prompt.Suggest {
if utils.IsOptionWord(d) {
return browserOptSuggests
}
digPath := removeOption(strings.Split(d.TextBeforeCursor(), " ")[1])
if len(c.cache.DigPathCache) == 0 {
paths, err := c.service.ListPath(context.Background(), digPath, true)
if err != nil {
log.Fatal(err)
}
c.cache.DigPathCache = paths
}
var suggests []prompt.Suggest
for _, nextPath := range nextElmPath(c.cache.DigPathCache, digPath) {
suggests = append(suggests, prompt.Suggest{Text: nextPath})
}
return suggests
}
func nextElmPath(paths []string, part string) []string {
numElmsOfPart := strings.Count(part, "/") + 1
var result []string
for _, path := range paths {
if !strings.HasPrefix(path, part) {
continue
}
numElmsOfPath := len(strings.Split(strings.TrimSuffix(path, "/"), "/"))
if numElmsOfPath != numElmsOfPart {
continue
}
result = append(result, path)
}
return result
}
// toLeafDirPath はPathを末端ディレクトリのPathに変換します
func toLeafDirPath(path string) string {
lastSlashIdx := strings.LastIndex(path, "/")
if lastSlashIdx == -1 {
return ""
}
return path[0:lastSlashIdx]
}