Added d:analyze and upgraded UCL
This commit is contained in:
parent
9751be125e
commit
dea9393d4d
5 changed files with 225 additions and 45 deletions
40
plugins.go
40
plugins.go
|
|
@ -36,6 +36,7 @@ func (p *PluginFilters) Module() ucl.Module {
|
|||
Name: "d",
|
||||
Builtins: map[string]ucl.BuiltinHandler{
|
||||
"filter": p.filterBuiltin,
|
||||
"analyze": p.filterAnalyze,
|
||||
"map-lines": p.mapLinesBuiltin,
|
||||
},
|
||||
}
|
||||
|
|
@ -80,6 +81,45 @@ func (p *PluginFilters) filterBuiltin(ctx context.Context, args ucl.CallArgs) (a
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
// filterBuiltin implements `d:analyze LABEL PROC`. PROC receives the filter input as its only
|
||||
// argument and its result is displayed as the result in the status bar.
|
||||
func (p *PluginFilters) filterAnalyze(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var (
|
||||
label string
|
||||
proc ucl.Invokable
|
||||
)
|
||||
|
||||
if err := args.Bind(&label, &proc); err != nil {
|
||||
return nil, fmt.Errorf("d:analyze: expected LABEL and PROC: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(label) == "" {
|
||||
return nil, errors.New("d:analyze: label must not be empty")
|
||||
}
|
||||
if proc.IsNil() {
|
||||
return nil, errors.New("d:analyze: PROC must not be nil")
|
||||
}
|
||||
|
||||
p.filters[pluginKeyPrefix+slugify(label)] = TextProcessor{
|
||||
Label: label,
|
||||
Analyze: func(ctx context.Context, input string) (string, error) {
|
||||
res, err := proc.Invoke(ctx, input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
switch v := res.(type) {
|
||||
case nil:
|
||||
return "", errors.New("filter returned no value")
|
||||
case string:
|
||||
return v, nil
|
||||
default:
|
||||
return fmt.Sprint(v), nil
|
||||
}
|
||||
},
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// mapLinesBuiltin implements `d:map-lines INPUT PROC` which applies a transform for each line of the input.
|
||||
func (p *PluginFilters) mapLinesBuiltin(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue