Skip to content

Commit

Permalink
Fix/KFS-1682 completion on down (#42)
Browse files Browse the repository at this point in the history
* fix: small bug with completion on down and history

* fix: lint
  • Loading branch information
gustavodemorais authored Feb 20, 2024
1 parent 513af2d commit b9f20c2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions _example/multiline-echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
in := prompt.Input(">>> ", completer,
prompt.OptionTitle("sql-prompt"),
prompt.OptionHistory([]string{"SELECT * FROM users;"}),
prompt.OptionCompletionOnDown(),
prompt.OptionPrefixTextColor(prompt.Yellow),
prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
Expand Down
4 changes: 4 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
return new, true
}

func (h *History) HasNewer() bool {
return h.selected < len(h.tmp)-1
}

// Newer saves a buffer of current line and get a buffer of next line by up-arrow.
// The changes of line buffers are stored until new history is created.
func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
Expand Down
4 changes: 3 additions & 1 deletion prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
// move the cursor up by one line
p.buf.CursorDown(1)
} else if newBuf, changed := p.history.Newer(p.buf); changed {

p.prevText = p.buf.Text()
p.buf = newBuf
}
Expand All @@ -354,9 +355,10 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
}

func (p *Prompt) handleCompletionKeyBinding(key Key, completing bool) {

switch key {
case Down:
if completing || p.completionOnDown {
if completing || (p.completionOnDown && !p.history.HasNewer()) {
p.completion.Next()
}
case Tab, ControlI:
Expand Down

0 comments on commit b9f20c2

Please sign in to comment.