func (t *Term) Close() error {
	err := unix.Close(t.fd)
	t.fd = -1
	return err
}
Close closes the device and releases any associated resources.
package term
import (
	"time"
	"github.com/pkg/term/termios"
	"golang.org/x/sys/unix"
)
type Term struct {
	name string
	fd   int
	orig unix.Termios  
}
func SetAttr(modifier func(*unix.Termios) uintptr) func(*Term) error {
	return func(t *Term) error {
		a, err := termios.Tcgetattr(uintptr(t.fd))
		if err != nil {
			return err
		}
		action := modifier(a)
		return termios.Tcsetattr(uintptr(t.fd), action, a)
	}
}
func (t *Term) SetCbreak() error {
	return t.SetOption(CBreakMode)
}
func CBreakMode(t *Term) error {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return err
	}
	termios.Cfmakecbreak(a)
	return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, a)
}
func (t *Term) SetRaw() error {
	return t.SetOption(RawMode)
}
func RawMode(t *Term) error {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return err
	}
	termios.Cfmakeraw(a)
	return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, a)
}
func Speed(baud int) func(*Term) error {
	return func(t *Term) error {
		return t.setSpeed(baud)
	}
}
func (t *Term) SetSpeed(baud int) error {
	return t.SetOption(Speed(baud))
}
func (t *Term) setSpeed(baud int) error {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return err
	}
	err = (*attr)(a).setSpeed(baud)
	if err != nil {
		return err
	}
	return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, a)
}
func (t *Term) GetSpeed() (int, error) {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return 0, err
	}
	return (*attr)(a).getSpeed()
}
func clamp(v, lo, hi int64) int64 {
	if v < lo {
		return lo
	}
	if v > hi {
		return hi
	}
	return v
}
func timeoutVals(d time.Duration) (uint8, uint8) {
	if d > 0 {
		vtimeDeci := d.Nanoseconds() / 1e6 / 100
		vtime := uint8(clamp(vtimeDeci, 1, 0xff))
		return 0, vtime
	}
	return 1, 0
}
func ReadTimeout(d time.Duration) func(*Term) error {
	return func(t *Term) error {
		return t.setReadTimeout(d)
	}
}
func (t *Term) SetReadTimeout(d time.Duration) error {
	return t.SetOption(ReadTimeout(d))
}
func (t *Term) setReadTimeout(d time.Duration) error {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return err
	}
	a.Cc[unix.VMIN], a.Cc[unix.VTIME] = timeoutVals(d)
	return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, a)
}
func FlowControl(kind int) func(*Term) error {
	return func(t *Term) error {
		return t.setFlowControl(kind)
	}
}
func (t *Term) SetFlowControl(kind int) error {
	return t.SetOption(FlowControl(kind))
}
func (t *Term) setFlowControl(kind int) error {
	a, err := termios.Tcgetattr(uintptr(t.fd))
	if err != nil {
		return err
	}
	switch kind {
	case NONE:
		a.Iflag &^= termios.IXON | termios.IXOFF | termios.IXANY
		a.Cflag &^= termios.CRTSCTS
	case XONXOFF:
		a.Cflag &^= termios.CRTSCTS
		a.Iflag |= termios.IXON | termios.IXOFF | termios.IXANY
	case HARDWARE:
		a.Iflag &^= termios.IXON | termios.IXOFF | termios.IXANY
		a.Cflag |= termios.CRTSCTS
	}
	return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, a)
}
func (t *Term) Flush() error {
	return termios.Tcflush(uintptr(t.fd), termios.TCIOFLUSH)
}
func (t *Term) SendBreak() error {
	return termios.Tcsendbreak(uintptr(t.fd), 0)
}
func (t *Term) DCD() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_CD == unix.TIOCM_CD, err
}
func (t *Term) SetDTR(v bool) error {
	bits := unix.TIOCM_DTR
	if v {
		return termios.Tiocmbis(uintptr(t.fd), bits)
	} else {
		return termios.Tiocmbic(uintptr(t.fd), bits)
	}
}
func (t *Term) DTR() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_DTR == unix.TIOCM_DTR, err
}
func (t *Term) DSR() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_DSR == unix.TIOCM_DSR, err
}
func (t *Term) SetRTS(v bool) error {
	bits := unix.TIOCM_RTS
	if v {
		return termios.Tiocmbis(uintptr(t.fd), bits)
	} else {
		return termios.Tiocmbic(uintptr(t.fd), bits)
	}
}
func (t *Term) RTS() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_RTS == unix.TIOCM_RTS, err
}
func (t *Term) CTS() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_CTS == unix.TIOCM_CTS, err
}
func (t *Term) RI() (bool, error) {
	status, err := termios.Tiocmget(uintptr(t.fd))
	return status&unix.TIOCM_RI == unix.TIOCM_RI, err
}