Skip to content

Commit

Permalink
Fix c-bata#266 correctly restore terminal parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
joeycumines committed Aug 10, 2023
1 parent 6343189 commit 5473460
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions input_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const maxReadBytes = 1024

// PosixParser is a ConsoleParser implementation for POSIX environment.
type PosixParser struct {
fd int
origTermios syscall.Termios
fd int
}

// Setup should be called before starting input
Expand Down
16 changes: 13 additions & 3 deletions internal/term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
saveTermios *unix.Termios
saveTermios unix.Termios
saveTermiosErr error
saveTermiosFD int
saveTermiosOnce sync.Once
Expand All @@ -20,9 +20,19 @@ var (
func getOriginalTermios(fd int) (*unix.Termios, error) {
saveTermiosOnce.Do(func() {
saveTermiosFD = fd
saveTermios, saveTermiosErr = termios.Tcgetattr(uintptr(fd))
var v *unix.Termios
v, saveTermiosErr = termios.Tcgetattr(uintptr(fd))
if saveTermiosErr == nil {
// save a copy
saveTermios = *v
}
})
return saveTermios, saveTermiosErr
if saveTermiosErr != nil {
return nil, saveTermiosErr
}
// return a copy
v := saveTermios
return &v, nil
}

// Restore terminal's mode.
Expand Down

0 comments on commit 5473460

Please sign in to comment.