Add 'Remove Blank Lines' and 'Keep Substring Matching' text processors
All checks were successful
Build / build (push) Successful in 2m59s

- Remove Blank Lines: filters out any lines that are empty or contain
  only whitespace
- Keep Substring Matching: prompts for a regex via FilterWithArg, then
  replaces each line with its first matching substring, dropping
  non-matching lines

Co-authored-by: Shelley <shelley@exe.dev>
This commit is contained in:
exe.dev user 2026-04-14 23:26:59 +00:00
parent c5f8d9e82e
commit aa680eea69

View file

@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -273,6 +274,47 @@ var TextFilters = map[string]TextProcessor{
}, },
}, },
"remove-blank-lines": {
Label: "Lines: Remove Blank Lines",
Filter: func(ctx context.Context, input string) (resp TextFilterResponse, err error) {
var dst []string
scnr := bufio.NewScanner(strings.NewReader(input))
for scnr.Scan() {
line := scnr.Text()
if strings.TrimSpace(line) != "" {
dst = append(dst, line)
}
}
return TextFilterResponse{Output: strings.Join(dst, "\n")}, nil
},
},
"keep-substring-matching": {
Label: "Lines: Keep Substring Matching…",
Description: "Prompts for a regular expression, then replaces each line with the first matching substring, removing non-matching lines.",
FilterWithArg: &FilterWithArg{
Label: "Regular Expression",
OnConfirm: func(ctx context.Context, prompt string) (filter TextFilter, err error) {
re, err := regexp.Compile(prompt)
if err != nil {
return nil, err
}
return func(ctx context.Context, input string) (resp TextFilterResponse, err error) {
var dst []string
scnr := bufio.NewScanner(strings.NewReader(input))
for scnr.Scan() {
line := scnr.Text()
if match := re.FindString(line); match != "" {
dst = append(dst, match)
}
}
return TextFilterResponse{Output: strings.Join(dst, "\n")}, nil
}, nil
},
},
},
"ucl-replace": { "ucl-replace": {
Label: "UCL: Replace", Label: "UCL: Replace",
Description: "Evaluates the input as a UCL expression and replaces it with the result.", Description: "Evaluates the input as a UCL expression and replaces it with the result.",