-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_string.go
More file actions
185 lines (158 loc) · 3.99 KB
/
value_string.go
File metadata and controls
185 lines (158 loc) · 3.99 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cmdline
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type StringHandlerFactory interface {
Set(ptr *string) ValueHandler
Call(func(value string)) ValueHandler
}
type StringParser interface {
Parse(text string) (string, error)
Complete(text string, observer CompletionObserver)
TypeName() string
}
type SimpleStringParser struct {
StringHandlerFactory
}
func (p *SimpleStringParser) Parse(text string) (string, error) {
return text, nil
}
func (p *SimpleStringParser) Complete(text string, observer CompletionObserver) {
}
func (p *SimpleStringParser) TypeName() string {
return "string"
}
func (p *SimpleStringParser) Set(ptr *string) ValueHandler {
return &StringHandler{Parser: p, Ptr: ptr}
}
func (p *SimpleStringParser) Call(callback func(value string)) ValueHandler {
return &StringHandler{Parser: p, Callback: callback}
}
var String StringHandlerFactory = &SimpleStringParser{}
type StringHandler struct {
Parser StringParser
Callback func(value string)
Ptr *string
AffectsParsing bool
}
func (h *StringHandler) Notify(text string, log Logger) bool {
value, err := h.Parser.Parse(text)
if err != nil {
log.Error(err.Error())
return !h.AffectsParsing
} else if h.Callback != nil {
h.Callback(value)
return true
} else if h.Ptr != nil {
*h.Ptr = value
return true
} else {
log.Error("missing consumer")
return !h.AffectsParsing
}
}
func (h *StringHandler) Complete(text string, observer CompletionObserver) {
h.Parser.Complete(text, observer)
}
func (h *StringHandler) TypeName() string {
return h.Parser.TypeName()
}
type Enum struct {
Possible []string
}
func (p *Enum) Parse(text string) (string, error) {
for _, possible := range p.Possible {
if text == possible {
return text, nil
}
}
return "", &parseError{message: fmt.Sprintf("%#v is not in %s", text, p.TypeName())}
}
func (p *Enum) Complete(text string, observer CompletionObserver) {
for _, possible := range p.Possible {
if strings.HasPrefix(possible, text) {
observer.FinalCompletion(possible)
}
}
}
func (p *Enum) TypeName() string {
return fmt.Sprintf("{%s}", strings.Join(p.Possible, ","))
}
func (p *Enum) Set(ptr *string) ValueHandler {
return &StringHandler{Parser: p, Ptr: ptr}
}
func (p *Enum) Call(callback func(value string)) ValueHandler {
return &StringHandler{Parser: p, Callback: callback}
}
type FilePath struct {
Root string
MustExist bool
FileFilter func(os.FileInfo) bool
}
func (p *FilePath) effectivePath(file string) string {
if p.Root != "" {
return filepath.Join(p.Root, file)
} else if file != "" {
return file
} else {
return "."
}
}
func (p *FilePath) Parse(text string) (string, error) {
fullpath := p.effectivePath(text)
_, err := os.Stat(fullpath)
if err != nil {
if p.MustExist && os.IsNotExist(err) {
return "", &parseError{message: fullpath + ": no such file or directory"}
}
}
return text, nil
}
func (p *FilePath) Complete(text string, observer CompletionObserver) {
dir, prefix := filepath.Split(text)
files, err := ioutil.ReadDir(p.effectivePath(dir))
// If this path isn't rooted in a real directory, don't offer any completions.
if err != nil {
return
}
for _, file := range files {
name := file.Name()
// Is it a valid completion?
if !strings.HasPrefix(name, prefix) {
continue
}
// Does the application accept it as a completion?
if p.FileFilter != nil && !p.FileFilter(file) {
continue
}
// Generate the completion.
full := filepath.Join(dir, name)
if file.IsDir() {
observer.PartialCompletion(full + "/")
} else {
observer.FinalCompletion(full)
}
}
}
func (p *FilePath) TypeName() string {
name := ""
if p.MustExist {
name = "existing file"
} else {
name = "file path"
}
if p.Root != "" {
name += " in " + p.Root
}
return name
}
func (p *FilePath) Set(ptr *string) ValueHandler {
return &StringHandler{Parser: p, Ptr: ptr}
}
func (p *FilePath) Call(callback func(value string)) ValueHandler {
return &StringHandler{Parser: p, Callback: callback}
}