-
Notifications
You must be signed in to change notification settings - Fork 31
/
processor.go
188 lines (163 loc) · 3.53 KB
/
processor.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package processors
import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/list"
)
var List = []list.Item{
ASCII85Decoding{},
ASCII85Encoding{},
Base32Decode{},
Base32Encoding{},
Base64Decode{},
Base64Encode{},
Base64URLDecode{},
Base64URLEncode{},
Bcrypt{},
Camel{},
CountCharacters{},
CountLines{},
CountWords{},
ExtractEmails{},
ExtractIPs{},
FormatJSON{},
HexDecode{},
HexEncode{},
HexToRGB{},
HTMLDecode{},
HTMLEncode{},
JSONEscape{},
JSONToMSGPACK{},
JSONToYAML{},
JSONUnescape{},
Kebab{},
Lower{},
Markdown{},
MD5{},
MSGPACKToJSON{},
RemoveNewLines{},
RemoveSpaces{},
Reverse{},
ReverseLines{},
ReverseLines{},
ROT13Encode{},
SHA1{},
SHA256{},
SHA512{},
ShuffleLines{},
Slug{},
Snake{},
SortLines{},
Title{},
UniqueLines{},
Upper{},
URLDecode{},
URLEncode{},
YAMLToJSON{},
Zeropad{},
}
type Processor interface {
// Name is the name of a processor used as the CLI command, must be one lowercase word,
// hyphens are allowed
Name() string
// Alias is an optional array of alias names for the processor
Alias() []string
// Transform is the text transformation function, implemented by the processor
Transform(data []byte, opts ...Flag) (string, error)
// Flags are flags that could be used to transform the text
Flags() []Flag
}
type FlagType string
func (f FlagType) String() string {
return string(f)
}
func (f FlagType) IsString() bool {
return f == FlagString
}
const (
FlagInt = FlagType("Int")
FlagUint = FlagType("Uint")
FlagBool = FlagType("Bool")
FlagString = FlagType("String")
)
type Flag struct {
// Name - required (long version) of the flag, lowercase (with hyphens)
Name string
// Short - required (single character, lowercase) of the flag
Short string
// Desc - required, a short description of the flag
Desc string
// Type - required the type of the flag
Type FlagType
// Value - optional default value of the flag
Value any
}
// Zeropad is an Example processor to show how to add text processors,
// it checks if the data is a number and pads it with zeros
// Example implements 'Item' and 'DefaultItem' from package 'github.com/charmbracelet/bubbles/list'
// to work with the ui, and `Processor` from this package to do the text transformation and generation
// of the cli commands
// After implementing add the struct to List.
type Zeropad struct{}
func (p Zeropad) Name() string {
return "zeropad"
}
func (p Zeropad) Alias() []string {
return nil
}
func (p Zeropad) Transform(data []byte, f ...Flag) (string, error) {
strIn := strings.TrimSpace(string(data))
neg := ""
i, err := strconv.ParseFloat(strIn, 64)
if err != nil {
return "", fmt.Errorf("number expected: '%s'", data)
}
if i < 0 {
neg = "-"
data = data[1:]
}
var n int
pre := ""
for _, flag := range f {
if flag.Short == "n" {
x, ok := flag.Value.(uint)
if ok {
n = int(x)
}
} else if flag.Short == "p" {
x, ok := flag.Value.(string)
if ok {
pre = x
}
}
}
return fmt.Sprintf("%s%s%s%s", pre, neg, strings.Repeat("0", n), data), nil
}
func (p Zeropad) Flags() []Flag {
return []Flag{
{
Name: "number-of-zeros",
Short: "n",
Desc: "Number of zeros to be padded",
Value: 5,
Type: FlagUint,
},
{
Name: "prefix",
Short: "p",
Desc: "The number get prefixed with this",
Value: "",
Type: FlagString,
},
}
}
func (p Zeropad) Title() string {
return strings.Title(p.Name())
}
func (p Zeropad) Description() string {
return "Pad a number with zeros"
}
func (p Zeropad) FilterValue() string {
return p.Title()
}