Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix: Add SetAttr for manual attribute control #23

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions term_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ type Term struct {
orig syscall.Termios // original state of the terminal, see Open and Restore
}

// SetAttr returns an option function which will apply the provided modifier to
// a syscall.Termios before using that syscall.Termios to set the state of the
// Term. This allows a developer to manually set attributes for the terminal.
// Here's an example case to set a terminal into raw mode, but then re-enable
// the 'opost' attribute:
//
// func EnableOutputPostprocess(a *syscall.Termios) uintptr {
// a.Oflag |= syscall.OPOST
// return termios.TCSANOW
// }
//
// func init() {
// t, _ = term.Open("/dev/tty")
// t.SetRaw()
// t.SetOption(term.SetAttr(EnableOutputPostprocess))
// }
func SetAttr(modifier func(*syscall.Termios) uintptr) func(*Term) error {
return func(t *Term) error {
var a syscall.Termios
if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil {
return err
}
action := modifier((*syscall.Termios)(&a))
return termios.Tcsetattr(uintptr(t.fd), action, (*syscall.Termios)(&a))
}
}

// SetCbreak sets cbreak mode.
func (t *Term) SetCbreak() error {
return t.SetOption(CBreakMode)
Expand Down