Skip to content

Commit

Permalink
pkg/term: Add ways to change the state of a terminal device
Browse files Browse the repository at this point in the history
A subsequent commit will use this to ensure that the user can still
interact with the image download prompt while 'skopeo inspect' fetches
the image size from the remote registry.  To do this, at some point, the
terminal device will be put into non-canonical mode input and the
echoing of input characters will be disabled to retain full control of
the cursor position.

#752
#1263
  • Loading branch information
debarshiray committed Dec 13, 2023
1 parent 34d46db commit 99963a8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/pkg/term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ import (
"golang.org/x/sys/unix"
)

type Option func(*unix.Termios)

func ChangeState(oldState *unix.Termios, options ...Option) *unix.Termios {
newState := *oldState
for _, option := range options {
option(&newState)
}

return &newState
}

func GetState(file *os.File) (*unix.Termios, error) {
fileFD := file.Fd()
fileFDInt := int(fileFD)
Expand All @@ -36,3 +47,41 @@ func IsTerminal(file *os.File) bool {

return true
}

func Restore(file *os.File, oldState *unix.Termios) error {
fileFD := file.Fd()
fileFDInt := int(fileFD)
err := unix.IoctlSetTermios(fileFDInt, unix.TCSETS, oldState)
return err
}

func SetState(file *os.File, state *unix.Termios) error {
fileFD := file.Fd()
fileFDInt := int(fileFD)
err := unix.IoctlSetTermios(fileFDInt, unix.TCSETS, state)
return err
}

func WithVMIN(vmin uint8) Option {
return func(state *unix.Termios) {
state.Cc[unix.VMIN] = vmin
}
}

func WithVTIME(vtime uint8) Option {
return func(state *unix.Termios) {
state.Cc[unix.VTIME] = vtime
}
}

func WithoutECHO() Option {
return func(state *unix.Termios) {
state.Lflag &^= unix.ECHO
}
}

func WithoutICANON() Option {
return func(state *unix.Termios) {
state.Lflag &^= unix.ICANON
}
}

0 comments on commit 99963a8

Please sign in to comment.