Added Lines:UCL for processing UCL commands
This commit is contained in:
parent
855a0aa114
commit
c5f8d9e82e
3 changed files with 235 additions and 81 deletions
|
|
@ -215,6 +215,48 @@ var TextFilters = map[string]TextProcessor{
|
|||
},
|
||||
},
|
||||
|
||||
"ucl-each-line": {
|
||||
Label: "Lines: UCL…",
|
||||
Description: "Transform each line as the result of a UCL expression.",
|
||||
FilterWithArg: &FilterWithArg{
|
||||
Label: "UCL Expression",
|
||||
OnConfirm: func(ctx context.Context, prompt string) (filter TextFilter, err error) {
|
||||
spv := strPseudoVar{}
|
||||
|
||||
uclCtx := uclInstFromContext(ctx)
|
||||
uclCtx.SetPseudoVar(".", &spv)
|
||||
|
||||
return func(ctx context.Context, input string) (resp TextFilterResponse, err error) {
|
||||
var (
|
||||
dst bytes.Buffer
|
||||
)
|
||||
scnr := bufio.NewScanner(strings.NewReader(input))
|
||||
isFirst := true
|
||||
for scnr.Scan() {
|
||||
if isFirst {
|
||||
isFirst = false
|
||||
} else {
|
||||
dst.WriteString("\n")
|
||||
}
|
||||
|
||||
line := scnr.Text()
|
||||
spv.str = line
|
||||
|
||||
if res, err := uclCtx.EvalString(ctx, prompt); err == nil {
|
||||
dst.WriteString(fmt.Sprint(res))
|
||||
} else if errors.Is(err, ucl.ErrNotConvertable) {
|
||||
dst.WriteString(line)
|
||||
} else {
|
||||
return TextFilterResponse{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return TextFilterResponse{Output: dst.String()}, nil
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
"ucl-evaluate": {
|
||||
Label: "UCL: Evaluate",
|
||||
Description: "Evaluates the input as a UCL expression and displays the result in the status bar.",
|
||||
|
|
@ -247,3 +289,11 @@ var TextFilters = map[string]TextProcessor{
|
|||
},
|
||||
},
|
||||
}
|
||||
|
||||
type strPseudoVar struct {
|
||||
str string
|
||||
}
|
||||
|
||||
func (s strPseudoVar) Get(ctx context.Context) (any, error) {
|
||||
return s.str, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue