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

Enhancement to support bash like multiple commands execution in one go #173

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 51 additions & 30 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,36 +68,43 @@ func (p *Prompt) Run() {
for {
select {
case b := <-bufCh:
if shouldExit, e := p.feed(b); shouldExit {
p.renderer.BreakLine(p.buf)
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}
return
} else if e != nil {
// Stop goroutine to run readBuffer function
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}

// Unset raw mode
// Reset to Blocking mode because returned EAGAIN when still set non-blocking mode.
debug.AssertNoError(p.in.TearDown())
p.executor(e.input)

p.completion.Update(*p.buf.Document())

p.renderer.Render(p.buf, p.completion)

if p.exitChecker != nil && p.exitChecker(e.input, true) {
p.skipTearDown = true
for {
shouldExit, e, bufLeft := p.feed(b)
if shouldExit {
p.renderer.BreakLine(p.buf)
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}
return
} else if e != nil {
// Stop goroutine to run readBuffer function
stopReadBufCh <- struct{}{}
stopHandleSignalCh <- struct{}{}

// Unset raw mode
// Reset to Blocking mode because returned EAGAIN when still set non-blocking mode.
debug.AssertNoError(p.in.TearDown())
p.executor(e.input)

p.completion.Update(*p.buf.Document())

p.renderer.Render(p.buf, p.completion)

if p.exitChecker != nil && p.exitChecker(e.input, true) {
p.skipTearDown = true
return
}
// Set raw mode
debug.AssertNoError(p.in.Setup())
go p.readBuffer(bufCh, stopReadBufCh)
go p.handleSignals(exitCh, winSizeCh, stopHandleSignalCh)
} else {
p.completion.Update(*p.buf.Document())
p.renderer.Render(p.buf, p.completion)
}
// Set raw mode
debug.AssertNoError(p.in.Setup())
go p.readBuffer(bufCh, stopReadBufCh)
go p.handleSignals(exitCh, winSizeCh, stopHandleSignalCh)
} else {
p.completion.Update(*p.buf.Document())
p.renderer.Render(p.buf, p.completion)
if len(bufLeft) == 0 {
break
}
b = bufLeft
}
case w := <-winSizeCh:
p.renderer.UpdateWinSize(w)
Expand All @@ -112,7 +119,21 @@ func (p *Prompt) Run() {
}
}

func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
func (p *Prompt) feed(buf []byte) (shouldExit bool, exec *Exec, bufLeft []byte) {
idx := bytes.IndexAny(buf, string([]byte{0xa, 0xd}))
var b []byte
if idx == 0 {
b = []byte{buf[0]}
if len(buf) > 1 {
bufLeft = buf[1:]
}
} else if idx != -1 {
b = buf[0:idx]
bufLeft = buf[idx:]
} else {
b = buf
}

key := GetKey(b)
p.buf.lastKeyStroke = key
// completion
Expand Down Expand Up @@ -248,7 +269,7 @@ func (p *Prompt) Input() string {
for {
select {
case b := <-bufCh:
if shouldExit, e := p.feed(b); shouldExit {
if shouldExit, e, _ := p.feed(b); shouldExit {
p.renderer.BreakLine(p.buf)
stopReadBufCh <- struct{}{}
return ""
Expand Down