summaryrefslogtreecommitdiff
path: root/input/stdin
diff options
context:
space:
mode:
Diffstat (limited to 'input/stdin')
-rw-r--r--input/stdin/stdin.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/input/stdin/stdin.go b/input/stdin/stdin.go
new file mode 100644
index 0000000..fa4d21f
--- /dev/null
+++ b/input/stdin/stdin.go
@@ -0,0 +1,46 @@
+package stdin
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "os"
+
+ "github.com/Shugyousha/stasher/input"
+ "github.com/Shugyousha/stasher/registry"
+ "github.com/Shugyousha/stasher/work"
+)
+
+func init() {
+ registry.Inputregistry["input"] = New
+}
+
+type StdinInput struct {
+ retchan chan *work.Work
+}
+
+func New(map[string]string) input.Input {
+ return &StdinInput{}
+}
+
+func (i *StdinInput) Start() chan *work.Work {
+ i.retchan = make(chan *work.Work, 100)
+ r := bufio.NewReader(os.Stdin)
+
+ go func() {
+ for {
+ bs, err := r.ReadBytes(byte('\n'))
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ fmt.Printf("Error when reading input from Stdin: %q", err)
+ os.Exit(1)
+ }
+ i.retchan <- &work.Work{Data: bs}
+ }
+ close(i.retchan)
+ }()
+
+ return i.retchan
+}