~yerinalexey/rex

d5ca6c5d454e2eec2ee0ff133527b142749ace77 — Alexey Yerin 4 months ago de5a6f7
Use -p to print matched lines

Printing the regular expression used for matching is not such a good
use-case. Printing matched lines on the other hand, will allow using rex
as an interactive regex-based text filter.
2 files changed, 34 insertions(+), 9 deletions(-)

M README.md
M main.go
M README.md => README.md +2 -1
@@ 52,7 52,8 @@ field supports readline-like bindings: [reference].

The following options are supported:
- `--input REGEX` (`-i REGEX`) - default value for the input field (default: `.`)
- `--print` (`-p`) - print the input after exit (default: `false`)
- `--print` (`-p`) - print matching lines after exit (default: `false`). This
  allows using `rex` as an interactive filter.
- `--version` (`-V`) - show version number
- `--help` (`-h`) - show the help message


M main.go => main.go +32 -8
@@ 45,7 45,7 @@ type Options struct {
	// Default value for regex input
	input string

	// Print the input field contents after exit
	// Print matched lines on exit
	printOnExit bool
}



@@ 54,6 54,21 @@ func fatalf(format string, v ...interface{}) {
	os.Exit(1)
}

// splitLines splits lines by line feeds, removing \r
func splitLines(lines string) []string {
	newLines := []string{}
	
	for _, line := range strings.Split(lines, "\n") {
		// Here we strip \r that might come from files with CRLF,
		// which causes issues with matching $
		line = strings.TrimSuffix(line, "\r")

		newLines = append(newLines, line)
	}

	return newLines
}

// regexWorker matches data from `in` to each line in `tests` and writes
// colored result to `out`
func regexWorker(in chan string, tests string, out *tview.TextView) {


@@ 75,11 90,8 @@ func regexWorker(in chan string, tests string, out *tview.TextView) {

			out.Clear()

			for _, line := range strings.Split(tests, "\n") {
				// Here we strip \r that might come from files with CRLF,
				// which causes issues with matching $
				line = strings.TrimSuffix(line, "\r")

			lines := splitLines(tests)
			for _, line := range lines {
				matches := rg.MatchString(line)

				color := "red"


@@ 129,7 141,7 @@ func main() {

	// TODO: More flags
	pflag.StringVarP(&opts.input, "input", "i", ".", "Default value for the input field")
	pflag.BoolVarP(&opts.printOnExit, "print", "p", false, "Print the input after exit")
	pflag.BoolVarP(&opts.printOnExit, "print", "p", false, "Print matching lines exit")
	pflag.BoolVarP(&opts.version, "version", "V", false, "Show version number")
	pflag.BoolVarP(&opts.help, "help", "h", false, "Show this message")
	pflag.Parse()


@@ 207,7 219,19 @@ func main() {
			app.Stop()

			if opts.printOnExit {
				fmt.Println(regexInput.GetText())
				rg, err := regexp.Compile(regexInput.GetText())
				if err != nil {
					break
				}

				lines := splitLines(data)
				for _, line := range lines {
					matches := rg.MatchString(line)

					if matches {
						fmt.Println(line)
					}
				}
			}
		case tcell.KeyTab:
			move = 1