From aa680eea696e542ee92611276da00b775da743e1 Mon Sep 17 00:00:00 2001 From: "exe.dev user" Date: Tue, 14 Apr 2026 23:26:59 +0000 Subject: [PATCH] Add 'Remove Blank Lines' and 'Keep Substring Matching' text processors - 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 --- textfilters.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/textfilters.go b/textfilters.go index 4c02c83..d0de1bd 100644 --- a/textfilters.go +++ b/textfilters.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "regexp" "strconv" "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": { Label: "UCL: Replace", Description: "Evaluates the input as a UCL expression and replaces it with the result.",