summaryrefslogtreecommitdiff
path: root/input/stdin.go
blob: 25840ef9e9d32dcbbc4f8f94f2223e13a068c889 (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
package input

import (
	"bufio"
	"fmt"
	"io"
	"os"

	"github.com/Shugyousha/stasher/work"
)

type StdinInput struct {
	retchan chan *work.Work
}

func NewStdin() *StdinInput {
	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
}