-
Notifications
You must be signed in to change notification settings - Fork 351
/
output_posix.go
67 lines (58 loc) · 1.51 KB
/
output_posix.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
61
62
63
64
65
66
67
// +build !windows
package prompt
import (
"syscall"
)
const flushMaxRetryCount = 3
// PosixWriter is a ConsoleWriter implementation for POSIX environment.
// To control terminal emulator, this outputs VT100 escape sequences.
type PosixWriter struct {
VT100Writer
fd int
}
// Flush to flush buffer
func (w *PosixWriter) Flush() error {
l := len(w.buffer)
offset := 0
retry := 0
for {
n, err := syscall.Write(w.fd, w.buffer[offset:])
if err != nil {
if retry < flushMaxRetryCount {
retry++
continue
}
return err
}
offset += n
if offset == l {
break
}
}
w.buffer = []byte{}
return nil
}
var _ ConsoleWriter = &PosixWriter{}
var (
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
// This generates VT100 escape sequences because almost terminal emulators
// in POSIX OS built on top of a VT100 specification.
// Deprecated: Please use NewStdoutWriter
NewStandardOutputWriter = NewStdoutWriter
)
// NewStdoutWriter returns ConsoleWriter object to write to stdout.
// This generates VT100 escape sequences because almost terminal emulators
// in POSIX OS built on top of a VT100 specification.
func NewStdoutWriter() ConsoleWriter {
return &PosixWriter{
fd: syscall.Stdout,
}
}
// NewStderrWriter returns ConsoleWriter object to write to stderr.
// This generates VT100 escape sequences because almost terminal emulators
// in POSIX OS built on top of a VT100 specification.
func NewStderrWriter() ConsoleWriter {
return &PosixWriter{
fd: syscall.Stderr,
}
}