/
handler.go
142 lines (129 loc) · 3.27 KB
/
handler.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
package commands
import (
"fmt"
pl "github.com/Mrs4s/power-liner"
"github.com/Mrs4s/six-cli/models"
"github.com/Mrs4s/six-cli/models/fs"
"github.com/Mrs4s/six-cli/shell"
"github.com/Mrs4s/six-cli/six_cloud"
"strings"
)
var (
alias = make(map[string][]string)
explains = make(map[string]string)
)
type CommandHandler struct {
}
func (CommandHandler) Alias() map[string][]string {
return alias
}
func (CommandHandler) Explains() map[string]string {
return explains
}
func refreshPrompt() {
if fs.GetParentPath(shell.CurrentPath) != "/" {
shell.App.SetPrompt(shell.CurrentUser.Username + "@six-pan:" + models.ShortPath(fs.GetFileName(shell.CurrentPath), 30) + "$ ")
return
}
shell.App.SetPrompt(shell.CurrentUser.Username + "@six-pan:" + models.ShortPath(shell.CurrentPath, 30) + "$ ")
}
func printUserList() {
for i, user := range shell.SavedUsers {
if user.Identity == shell.CurrentUser.Identity {
fmt.Println(i+1, "->", user.Username)
continue
}
fmt.Println(user.Username)
}
}
func PathCompleter(c *pl.Context, f bool) []string {
if len(c.Nokeys) > 1 {
return []string{}
}
if len(strings.Split(c.Nokeys[0], "/")) <= 1 {
fun := func(s string) string {
if strings.Contains(s, " ") {
return "\"" + s + "\""
}
if strings.Contains(s, ".") {
return s
}
return s + "/"
}
if f {
return models.SelectStrings(append(filterCurrentDirs(), filterCurrentFiles()...), fun)
}
return models.SelectStrings(filterCurrentDirs(), fun)
}
newPath := shell.CurrentPath + "/" + c.Nokeys[0]
if shell.CurrentPath == "/" {
newPath = "/" + c.Nokeys[0]
}
if strings.HasPrefix(c.Nokeys[0], "/") {
newPath = c.Nokeys[0]
}
files, err := shell.CurrentUser.GetFilesByPath(fs.GetParentPath(newPath))
if err != nil {
return []string{}
}
filter := func(s string) bool {
if strings.HasSuffix(newPath, "/") {
return true
}
return strings.HasPrefix(s, fs.GetFileName(newPath))
}
selector := func(s string) string {
com := models.CombinePaths(fs.GetParentPath(newPath), s, "/")
if strings.Contains(com, " ") {
return "\"" + com[1:] + "\""
}
if strings.Contains(com[1:], ".") {
return com[1:]
}
return com[1:] + "/"
}
//fmt.Println(f)f
if f {
return models.SelectStrings(models.FilterStrings(append(filterDirs(files), filterFiles(files)...), filter), selector)
}
return models.SelectStrings(models.FilterStrings(filterDirs(files), filter), selector)
}
func filterCurrentDirs() []string {
files, err := shell.CurrentUser.GetFilesByPath(shell.CurrentPath)
if err != nil {
return []string{}
}
return filterDirs(files)
}
func filterDirs(files []*six_cloud.SixFile) (res []string) {
for _, file := range files {
if file.IsDir {
res = append(res, file.Name)
}
}
return
}
func filterCurrentFiles() []string {
files, err := shell.CurrentUser.GetFilesByPath(shell.CurrentPath)
if err != nil {
return []string{}
}
return filterFiles(files)
}
func filterFiles(files []*six_cloud.SixFile) (res []string) {
for _, file := range files {
if !file.IsDir {
res = append(res, file.Name)
}
}
return
}
func filterFileArray(arr []*six_cloud.SixFile, filter func(*six_cloud.SixFile) bool) []*six_cloud.SixFile {
var res []*six_cloud.SixFile
for _, file := range arr {
if filter(file) {
res = append(res, file)
}
}
return res
}