master
go 40 lines 571 Bytes
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package functions
4
5 import (
6 "bufio"
7 "os"
8 "sync"
9 )
10
11 type input interface {
12 lines() <-chan string
13 }
14
15 func newStdinInput() input {
16 return &stdinReader{}
17 }
18
19 type stdinReader struct {
20 once sync.Once
21 linesCh chan string
22 }
23
24 func (in *stdinReader) run() {
25 defer close(in.linesCh)
26 sc := bufio.NewScanner(bufio.NewReader(os.Stdin))
27
28 for sc.Scan() {
29 in.linesCh <- sc.Text()
30 }
31 }
32
33 func (in *stdinReader) lines() <-chan string {
34 in.once.Do(func() {
35 in.linesCh = make(chan string)
36 go in.run()
37 })
38
39 return in.linesCh
40 }