-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
79 lines (70 loc) · 1.85 KB
/
term.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
68
69
70
71
72
73
74
75
76
77
78
79
// Package term manages POSIX terminals. As POSIX terminals are connected to,
// or emulate, a UART, this package also provides control over the various
// UART and serial line parameters.
package term
import (
"errors"
"io"
"os"
"syscall"
"github.com/pkg/term/termios"
)
const (
NONE = iota // flow control off
XONXOFF // software flow control
HARDWARE // hardware flow control
)
var errNotSupported = errors.New("not supported")
// Read reads up to len(b) bytes from the terminal. It returns the number of
// bytes read and an error, if any. EOF is signaled by a zero count with
// err set to io.EOF.
func (t *Term) Read(b []byte) (int, error) {
n, e := syscall.Read(t.fd, b)
if n < 0 {
n = 0
}
if n == 0 && len(b) > 0 && e == nil {
return 0, io.EOF
}
if e != nil {
return n, &os.PathError{"read", t.name, e}
}
return n, nil
}
// SetOption takes one or more option function and applies them in order to Term.
func (t *Term) SetOption(options ...func(*Term) error) error {
for _, opt := range options {
if err := opt(t); err != nil {
return err
}
}
return nil
}
// Write writes len(b) bytes to the terminal. It returns the number of bytes
// written and an error, if any. Write returns a non-nil error when n !=
// len(b).
func (t *Term) Write(b []byte) (int, error) {
n, e := syscall.Write(t.fd, b)
if n < 0 {
n = 0
}
if n != len(b) {
return n, io.ErrShortWrite
}
if e != nil {
return n, &os.PathError{"write", t.name, e}
}
return n, nil
}
// Available returns how many bytes are unused in the buffer.
func (t *Term) Available() (int, error) {
var n int
err := termios.Tiocinq(uintptr(t.fd), &n)
return n, err
}
// Buffered returns the number of bytes that have been written into the current buffer.
func (t *Term) Buffered() (int, error) {
var n int
err := termios.Tiocoutq(uintptr(t.fd), &n)
return n, err
}