diff options
Diffstat (limited to 'console')
-rw-r--r-- | console/console.go | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/console/console.go b/console/console.go index 4fe9680..577e021 100644 --- a/console/console.go +++ b/console/console.go @@ -1,7 +1,6 @@ package console import ( - "context" "fmt" "io" "os" @@ -11,36 +10,38 @@ type Console struct { r io.Reader w io.Writer ich, och chan uint16 + done chan struct{} } -func New(ctx context.Context) *Console { +func New() *Console { c := &Console{ - r: os.Stdin, - w: os.Stdout, - ich: make(chan uint16, 1), - och: make(chan uint16, 1), + r: os.Stdin, + w: os.Stdout, + ich: make(chan uint16, 1), + och: make(chan uint16, 1), + done: make(chan struct{}), } - go c.read(ctx) - go c.write(ctx) + go c.read() + go c.write() return c } -func (c *Console) read(ctx context.Context) { +func (c *Console) read() { var v uint16 for { fmt.Fscanf(c.r, "%c", &v) select { - case <-ctx.Done(): + case <-c.done: return case c.ich <- v: } } } -func (c *Console) write(ctx context.Context) { +func (c *Console) write() { for { select { - case <-ctx.Done(): + case <-c.done: return case v := <-c.och: fmt.Fprintf(c.w, "%c", v) @@ -51,3 +52,4 @@ func (c *Console) write(ctx context.Context) { func (c *Console) Read() uint16 { return <-c.ich } func (c *Console) Write(v uint16) { c.och <- v } func (c *Console) Len() uint16 { return uint16(len(c.ich)) } +func (c *Console) Stop() { close(c.done) } |