~yerinalexey/public-inbox

rex: Add support for STDIN v1 NEEDS REVISION

Garrit Franke: 1
 Add support for STDIN

 1 files changed, 35 insertions(+), 9 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~yerinalexey/public-inbox/patches/19498/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH rex] Add support for STDIN Export this patch

This adds the ability to read from stdin, as opposed to reading a file.
A simple example would be the following:

printf "foo\nbar\n" | rex

If text is piped from stdin *and* from a file, the file will be
prioritized:

printf "foo\nbar\n" | rex test.txt
---
 main.go | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/main.go b/main.go
index b880d8e..ec2974b 100644
--- a/main.go
+++ b/main.go
@@ -17,6 +17,7 @@
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
@@ -74,6 +75,31 @@ func regexWorker(in chan string, tests string, out *tview.TextView) {
	}
}

func fromStdin() string {
	var data string
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		data += scanner.Text()
		data += "\n"
	}

	return data
}

func fromFile(fname string) string {
	// TODO: This will block before file loads, should be in a separate
	// goroutine
	var data string

	if raw, err := ioutil.ReadFile(fname); err == nil {
		data = string(raw)
	} else {
		fatalf("Failed to read %s: %v\n", fname, err)
	}

	return data
}

func main() {
	flag.Usage = func() {
		fmt.Fprintln(os.Stderr, "Usage: rex [flags] <file>")
@@ -95,19 +121,19 @@ func main() {
	}

	file := flag.Arg(0)
	if file == "" {
		flag.Usage()
		return
	}

	// TODO: This will block before file loads, should be in a separate
	// goroutine
	var data string

	if raw, err := ioutil.ReadFile(file); err == nil {
		data = string(raw)
	// Determine type of input
	if file == "" {
		data = fromStdin()
	} else {
		fatalf("Failed to read %s: %v\n", file, err)
		data = fromFile(file)
	}

	if data == "" {
		flag.Usage()
		return
	}

	exprChan := make(chan string)
-- 
2.24.3 (Apple Git-128)
Looks good! Can you also update README and usage information?