Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce prompt.OptionCloseOnControlC() #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ func OptionShowCompletionAtStart() Option {
}
}

// OptionCloseOnControlC causes prompts to close on ControlC
func OptionCloseOnControlC() Option {
return func(p *Prompt) error {
p.renderer.closeOnControlC = true
return nil
}
}

// OptionBreakLineCallback to run a callback at every break line
func OptionBreakLineCallback(fn func(*Document)) Option {
return func(p *Prompt) error {
Expand Down
11 changes: 8 additions & 3 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
p.history.Add(exec.input)
}
case ControlC:
p.renderer.BreakLine(p.buf)
p.buf = NewBuffer()
p.history.Clear()
if p.renderer.closeOnControlC && p.buf.Text() == "" {
shouldExit = true
return
} else {
p.renderer.BreakLine(p.buf)
p.buf = NewBuffer()
p.history.Clear()
}
case Up, ControlP:
if !completing { // Don't use p.completion.Completing() because it takes double operation when switch to selected=-1.
if newBuf, changed := p.history.Older(p.buf); changed {
Expand Down
1 change: 1 addition & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Render struct {
prefix string
livePrefixCallback func() (prefix string, useLivePrefix bool)
breakLineCallback func(*Document)
closeOnControlC bool
title string
row uint16
col uint16
Expand Down