Skip to content

Commit

Permalink
feat: go runtime abort on Ctrl+\
Browse files Browse the repository at this point in the history
  • Loading branch information
knz authored and muesli committed Oct 22, 2022
1 parent ad7a7ee commit 36f5073
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,13 @@ func WithoutJobControl() ProgramOption {
p.disableSuspendOnCtrlZ = true
}
}

// WithoutGoStandardAbort disables support for sending SIGQUIT
// to the process (and generating a goroutine dump) when
// Ctrl+\ is pressed. By default, Ctrl+\ causes SIGQUIT to be
// emitted, this is a standard Go feature.
func WithoutGoStandardAbort() ProgramOption {
return func(p *Program) {
p.disableGoStandardAbort = true
}
}
17 changes: 16 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ type Program struct {
// (suspend process).
disableSuspendOnCtrlZ bool

// disableGoStandardAbort removes direct bubbletea support for Ctrl+\
// (SIGQUIT / goroutine dump).
disableGoStandardAbort bool

// Stores the original reference to stdin for cases where input is not a
// TTY on windows and we've automatically opened CONIN$ to receive input.
// When the program exits this will be restored.
Expand Down Expand Up @@ -314,10 +318,21 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
}()

case KeyMsg:
if !p.disableSuspendOnCtrlZ && msg.Type == KeyCtrlZ && !msg.Alt {
if !p.disableSuspendOnCtrlZ && !msg.Alt && msg.Type == KeyCtrlZ {
cmds <- Suspend()
continue
}
if !p.disableGoStandardAbort && !msg.Alt && msg.Type == KeyCtrlBackslash {
cmds <- Exec(fnAsCommand(func() {
pr, err := os.FindProcess(os.Getpid())
if err != nil {
// No-op.
return
}
_ = pr.Signal(syscall.SIGQUIT)
}), nil)
continue
}

case suspendMsg:
if canSuspendProcess {
Expand Down

0 comments on commit 36f5073

Please sign in to comment.