Skip to content

Commit

Permalink
feat(term): support setting terminal state (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas authored Jan 10, 2024
1 parent 1a0fe44 commit 2e1bea1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func GetState(fd int) (*State, error) {
return getState(fd)
}

// SetState sets the given state of the terminal.
func SetState(fd int, state *State) error {
return setState(fd, state)
}

// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func Restore(fd int, oldState *State) error {
Expand Down
4 changes: 4 additions & 0 deletions term/term_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func getSize(fd int) (width, height int, err error) {
return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

func setState(fd int, state *State) error {
return fmt.Errorf("terminal: SetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

func readPassword(fd int) ([]byte, error) {
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
8 changes: 8 additions & 0 deletions term/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func makeRaw(fd int) (*State, error) {
return &oldState, nil
}

func setState(fd int, state *State) error {
var termios *unix.Termios
if state != nil {
termios = &state.Termios
}
return unix.IoctlSetTermios(fd, ioctlWriteTermios, termios)
}

func getState(fd int) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions term/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func makeRaw(fd int) (*State, error) {
return &State{state{st}}, nil
}

func setState(fd int, state *State) error {
var mode uint32
if state != nil {
mode = state.Mode
}
return windows.SetConsoleMode(windows.Handle(fd), mode)
}

func getState(fd int) (*State, error) {
var st uint32
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
Expand Down

0 comments on commit 2e1bea1

Please sign in to comment.