Skip to content

Commit

Permalink
configurable os.Signal catching
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Dec 22, 2021
1 parent 6336041 commit 90c02fa
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions v3/termutil/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
var echoLocked bool
var echoLockMutex sync.Mutex
var errLocked = errors.New("terminal locked")
var autoTerminate = true

// AutoTerminate enables or disables automatic terminate signal catching.
// It's needed to restore the terminal state after the pool was used.
// By default, it's enabled.
func AutoTerminate(enable bool) {
echoLockMutex.Lock()
defer echoLockMutex.Unlock()
autoTerminate = enable
}

// RawModeOn switches terminal to raw mode
func RawModeOn() (quit chan struct{}, err error) {
Expand Down Expand Up @@ -45,8 +55,10 @@ func RawModeOff() (err error) {
// listen exit signals and restore terminal state
func catchTerminate(quit chan struct{}) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, unlockSignals...)
defer signal.Stop(sig)
if autoTerminate {
signal.Notify(sig, unlockSignals...)
defer signal.Stop(sig)
}
select {
case <-quit:
RawModeOff()
Expand Down

0 comments on commit 90c02fa

Please sign in to comment.