-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
invokedAction.go
174 lines (154 loc) · 4.99 KB
/
invokedAction.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
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
package carapace
import (
"net/url"
"strings"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/export"
_shell "github.com/carapace-sh/carapace/internal/shell"
"github.com/carapace-sh/carapace/pkg/match"
)
// InvokedAction is a logical alias for an Action whose (nested) callback was invoked.
type InvokedAction struct {
action Action
}
func (ia InvokedAction) export() export.Export {
return export.Export{Meta: ia.action.meta, Values: ia.action.rawValues}
}
// Filter filters given values.
//
// a := carapace.ActionValues("A", "B", "C").Invoke(c)
// b := a.Filter([]string{"B"}) // ["A", "C"]
func (ia InvokedAction) Filter(values ...string) InvokedAction {
ia.action.rawValues = ia.action.rawValues.Filter(values...)
return ia
}
// Merge merges InvokedActions (existing values are overwritten)
//
// a := carapace.ActionValues("A", "B").Invoke(c)
// b := carapace.ActionValues("B", "C").Invoke(c)
// c := a.Merge(b) // ["A", "B", "C"]
func (ia InvokedAction) Merge(others ...InvokedAction) InvokedAction {
for _, other := range append([]InvokedAction{ia}, others...) {
ia.action.rawValues = append(ia.action.rawValues, other.action.rawValues...)
ia.action.meta.Merge(other.action.meta)
}
ia.action.rawValues = ia.action.rawValues.Unique()
return ia
}
// Prefix adds a prefix to values (only the ones inserted, not the display values)
//
// carapace.ActionValues("melon", "drop", "fall").Invoke(c).Prefix("water")
func (ia InvokedAction) Prefix(prefix string) InvokedAction {
for index, val := range ia.action.rawValues {
ia.action.rawValues[index].Value = prefix + val.Value
}
return ia
}
// Retain retains given values.
//
// a := carapace.ActionValues("A", "B", "C").Invoke(c)
// b := a.Retain([]string{"A", "C"}) // ["A", "C"]
func (ia InvokedAction) Retain(values ...string) InvokedAction {
ia.action.rawValues = ia.action.rawValues.Retain(values...)
return ia
}
// Suffix adds a suffx to values (only the ones inserted, not the display values)
//
// carapace.ActionValues("apple", "melon", "orange").Invoke(c).Suffix("juice")
func (ia InvokedAction) Suffix(suffix string) InvokedAction {
for index, val := range ia.action.rawValues {
ia.action.rawValues[index].Value = val.Value + suffix
}
return ia
}
// UidF TODO experimental
func (ia InvokedAction) UidF(f func(s string) (*url.URL, error)) InvokedAction {
for index, v := range ia.action.rawValues {
url, err := f(v.Value)
if err != nil {
return ActionMessage(err.Error()).Invoke(Context{})
}
ia.action.rawValues[index].Uid = url.String()
}
return ia
}
// ToA casts an InvokedAction to Action.
func (ia InvokedAction) ToA() Action {
return ia.action
}
func tokenize(s string, dividers ...string) []string {
if len(dividers) == 0 {
return []string{s}
}
result := make([]string, 0)
for _, word := range strings.SplitAfter(s, dividers[0]) {
tokens := tokenize(strings.TrimSuffix(word, dividers[0]), dividers[1:]...)
if len(tokens) > 0 && strings.HasSuffix(word, dividers[0]) {
tokens[len(tokens)-1] = tokens[len(tokens)-1] + dividers[0]
}
result = append(result, tokens...)
}
return result
}
// ToMultiPartsA create an ActionMultiParts from values with given dividers
//
// a := carapace.ActionValues("A/B/C", "A/C", "B/C", "C").Invoke(c)
// b := a.ToMultiPartsA("/") // completes segments separately (first one is ["A/", "B/", "C"])
func (ia InvokedAction) ToMultiPartsA(dividers ...string) Action {
return ActionCallback(func(c Context) Action {
splittedCV := tokenize(c.Value, dividers...)
uniqueVals := make(map[string]common.RawValue)
for _, val := range ia.action.rawValues {
if match.HasPrefix(val.Value, c.Value) {
if splitted := tokenize(val.Value, dividers...); len(splitted) >= len(splittedCV) {
v := strings.Join(splitted[:len(splittedCV)], "")
d := splitted[len(splittedCV)-1]
if len(splitted) == len(splittedCV) {
uniqueVals[v] = common.RawValue{
Value: v,
Display: d,
Description: val.Description,
Style: val.Style,
Tag: val.Tag,
Uid: val.Uid,
}
} else {
uniqueVals[v] = common.RawValue{
Value: v,
Display: d,
Description: "",
Style: "",
Tag: val.Tag,
Uid: val.Uid,
}
}
}
}
}
vals := make([]common.RawValue, 0)
for _, val := range uniqueVals {
vals = append(vals, val)
}
a := Action{rawValues: vals}
for _, divider := range dividers {
if runes := []rune(divider); len(runes) == 0 {
a.meta.Nospace.Add('*')
break
} else {
a.meta.Nospace.Add(runes[len(runes)-1])
}
}
return a
})
}
func (ia InvokedAction) value(shell string, value string) string {
return _shell.Value(shell, value, ia.action.meta, ia.action.rawValues)
}
func init() {
common.FromInvokedAction = func(i interface{}) (common.Meta, common.RawValues) {
if invoked, ok := i.(InvokedAction); ok {
return invoked.action.meta, invoked.action.rawValues
}
return common.Meta{}, nil
}
}