-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio.go
47 lines (38 loc) · 1.21 KB
/
stdio.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
package termite
import (
"bufio"
"io"
"os"
)
var (
// StdoutWriter to be used as standard out
StdoutWriter *AutoFlushingWriter
// StderrWriter to be used as standard err
StderrWriter *AutoFlushingWriter
// StdinReader to be used as standard in
StdinReader io.Reader
)
func init() {
StdoutWriter = NewAutoFlushingWriter(os.Stdout)
StderrWriter = NewAutoFlushingWriter(os.Stderr)
StdinReader = os.Stdin
}
// AutoFlushingWriter an implementation of an io.Writer and io.StringWriter with auto-flush semantics.
type AutoFlushingWriter struct {
Writer *bufio.Writer
}
// NewAutoFlushingWriter creates a new io.Writer that uses a buffer internally and flushes after every write.
// This writer should be used on top of Stdout and Stderr for components that require frequent screen updates.
func NewAutoFlushingWriter(w io.Writer) *AutoFlushingWriter {
return &AutoFlushingWriter{
Writer: bufio.NewWriter(w),
}
}
func (sw *AutoFlushingWriter) Write(b []byte) (int, error) {
defer sw.Writer.Flush()
return sw.Writer.Write(b)
}
// WriteString uses io.WriteString to write the specified string to the underlying writer.
func (sw *AutoFlushingWriter) WriteString(s string) (int, error) {
return sw.Write([]byte(s))
}