Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 10, 2024
1 parent 1a0fe44 commit a824a7e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
34 changes: 34 additions & 0 deletions term/term.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package term

// Flag represents a terminal state flag.
type Flag interface {
flag()
}

// These represents the state flag types.
type (
I uint64 // Input flags
O uint64 // Output flags
C uint64 // Control flags, not supported on Windows
L uint64 // Local flags, not supported on Windows
)

func (I) flag() {}
func (O) flag() {}
func (C) flag() {}
func (L) flag() {}

// State contains platform-specific state of a terminal.
type State struct {
state
Expand Down Expand Up @@ -42,3 +60,19 @@ func GetSize(fd int) (width, height int, err error) {
func ReadPassword(fd int) ([]byte, error) {
return readPassword(fd)
}

// EnableMode enables terminal modes.
func EnableMode(fd int, flags ...Flag) (*State, error) {
if len(flags) == 0 {
return GetState(fd)
}
return enableMode(fd, flags...)
}

// DisableMode disables terminal modes.
func DisableMode(fd int, flags ...Flag) (*State, error) {
if len(flags) == 0 {
return GetState(fd)
}
return disableMode(fd, flags...)
}
8 changes: 8 additions & 0 deletions term/term_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ func getSize(fd int) (width, height int, err error) {
func readPassword(fd int) ([]byte, error) {
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

func enableMode(fd int, flags ...Flag) (*State, error) {
return nil, fmt.Errorf("terminal: EnableMode not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

func disableMode(fd int, flags ...Flag) (*State, error) {
return nil, fmt.Errorf("terminal: DisableMode not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
48 changes: 48 additions & 0 deletions term/term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,51 @@ func readPassword(fd int) ([]byte, error) {

return readPasswordLine(passwordReader(fd))
}

func change(fd int, enable bool, flags ...Flag) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil {
return nil, err
}

var iflags, oflags, lflags, cflags uint64
for _, f := range flags {
switch f := f.(type) {
case I:
iflags |= uint64(f)
case O:
oflags |= uint64(f)
case L:
lflags |= uint64(f)
case C:
cflags |= uint64(f)
}
}

// TODO: support termios.Cc and termios.[I|o]speed
if enable {
termios.Iflag |= iflags
termios.Oflag |= oflags
termios.Lflag |= lflags
termios.Cflag |= cflags
} else {
termios.Iflag &^= iflags
termios.Oflag &^= oflags
termios.Lflag &^= lflags
termios.Cflag &^= cflags
}

if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil {
return nil, err
}

return &State{state{Termios: *termios}}, nil
}

func enableMode(fd int, flags ...Flag) (*State, error) {
return change(fd, true, flags...)
}

func disableMode(fd int, flags ...Flag) (*State, error) {
return change(fd, false, flags...)
}

0 comments on commit a824a7e

Please sign in to comment.