summaryrefslogtreecommitdiff
path: root/input/stdin/stdin.go
blob: e2910ec3e43d76ea3f6fc76cf9a483b657fa8189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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["stdin"] = 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
}