-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.go
60 lines (51 loc) · 1.03 KB
/
console.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"bufio"
"fmt"
"os"
)
type console interface {
readline() (string, bool)
readChar() (uint8, bool)
write(string)
writef(string, ...interface{})
close()
}
type consoleSimple struct {
in *bufio.Scanner
env *environment
}
func newConsoleSimple(env *environment) *consoleSimple {
var c consoleSimple
c.in = bufio.NewScanner(os.Stdin)
c.env = env
return &c
}
func (c *consoleSimple) readline() (string, bool) {
if !c.in.Scan() {
return "", true
}
line := c.in.Text()
c.env.writeSpool(line)
c.env.writeSpool("\n")
return line, false
}
func (c *consoleSimple) readChar() (uint8, bool) {
// TODO: capture keystrokes. We will just get the first char of the line
// and ignore the rest.
s, stop := c.readline()
if s == "" {
return ' ', stop
} else {
return s[0], stop
}
}
func (c *consoleSimple) write(s string) {
fmt.Print(s)
c.env.writeSpool(s)
}
func (c *consoleSimple) writef(format string, a ...interface{}) {
s := fmt.Sprintf(format, a...)
c.write(s)
}
func (c *consoleSimple) close() {}