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

Add interactive history search. Reject unprintable characters. #225

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ _testmain.go

# Glide
vendor/

# vim
*.swp
*.swo
12 changes: 10 additions & 2 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ type CompletionManager struct {
verticalScroll int
wordSeparator string
showAtStart bool
disabled bool
}

func (c *CompletionManager) Enable(on bool) {
c.disabled = !on
}

// GetSelectedSuggestion returns the selected item.
func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {
if c.selected == -1 {
if c.selected == -1 || c.disabled {
return Suggest{}, false
} else if c.selected < -1 {
debug.Assert(false, "must not reach here")
Expand All @@ -53,6 +58,9 @@ func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {

// GetSuggestions returns the list of suggestion.
func (c *CompletionManager) GetSuggestions() []Suggest {
if c.disabled {
return []Suggest{}
}
return c.tmp
}

Expand Down Expand Up @@ -88,7 +96,7 @@ func (c *CompletionManager) Next() {

// Completing returns whether the CompletionManager selects something one.
func (c *CompletionManager) Completing() bool {
return c.selected != -1
return c.selected != -1 && !c.disabled
}

func (c *CompletionManager) update() {
Expand Down
63 changes: 63 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package prompt

import (
"strings"
)

// History stores the texts that are entered.
type History struct {
histories []string
tmp []string
selected int
searchAt int
}

// Add to add text in history.
Expand All @@ -21,6 +26,63 @@ func (h *History) Clear() {
}
h.tmp = append(h.tmp, "")
h.selected = len(h.tmp) - 1
h.searchAt = -1
}

func (h *History) SearchReset(begin bool) {
hlen := len(h.histories)
if begin {
h.searchAt = 0
} else {
h.searchAt = hlen - 1
}
}

func (h *History) Search(pattern string, fwd bool, skipCur bool) string {
hlen := len(h.histories)
if skipCur {
if h.searchAt < 0 && fwd {
h.searchAt = 0
}
if h.searchAt >= hlen && !fwd {
h.searchAt = hlen -1
}
}
if h.searchAt < 0 || h.searchAt >= hlen {
return ""
}
if fwd {
for idx := h.searchAt; idx < hlen; idx++ {
hstr := h.histories[idx]
if strings.Contains(hstr, pattern) {
if skipCur {
h.searchAt = idx + 1
skipCur = false
} else {
return hstr
}
}
}
if skipCur {
h.searchAt = hlen
}
} else {
for idx := h.searchAt; idx >= 0; idx-- {
hstr := h.histories[idx]
if strings.Contains(hstr, pattern) {
if skipCur {
h.searchAt = idx - 1
skipCur = false
} else {
return hstr
}
}
}
if skipCur {
h.searchAt = -1
}
}
return ""
}

// Older saves a buffer of current line and get a buffer of previous line by up-arrow.
Expand Down Expand Up @@ -57,5 +119,6 @@ func NewHistory() *History {
histories: []string{},
tmp: []string{""},
selected: 0,
searchAt: -1,
}
}
Loading