mirror of https://github.com/Hilbis/Hilbish
feat: add history search handler
parent
b65acca903
commit
bbf5a93ca0
|
@ -64,14 +64,8 @@ func (g *CompletionGroup) init(rl *Instance) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateTabFind - When searching through all completion groups (whether it be command history or not),
|
func (g *CompletionGroup) filterSuggestions(rl *Instance) []string {
|
||||||
// we ask each of them to filter its own items and return the results to the shell for aggregating them.
|
|
||||||
// The rx parameter is passed, as the shell already checked that the search pattern is valid.
|
|
||||||
func (g *CompletionGroup) updateTabFind(rl *Instance) {
|
|
||||||
|
|
||||||
suggs := make([]string, 0)
|
suggs := make([]string, 0)
|
||||||
|
|
||||||
// We perform filter right here, so we create a new completion group, and populate it with our results.
|
|
||||||
for i := range g.Suggestions {
|
for i := range g.Suggestions {
|
||||||
if rl.regexSearch == nil { continue }
|
if rl.regexSearch == nil { continue }
|
||||||
if rl.regexSearch.MatchString(g.Suggestions[i]) {
|
if rl.regexSearch.MatchString(g.Suggestions[i]) {
|
||||||
|
@ -82,8 +76,20 @@ func (g *CompletionGroup) updateTabFind(rl *Instance) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We overwrite the group's items, (will be refreshed as soon as something is typed in the search)
|
return suggs
|
||||||
g.Suggestions = suggs
|
}
|
||||||
|
|
||||||
|
// updateTabFind - When searching through all completion groups (whether it be command history or not),
|
||||||
|
// we ask each of them to filter its own items and return the results to the shell for aggregating them.
|
||||||
|
// The rx parameter is passed, as the shell already checked that the search pattern is valid.
|
||||||
|
func (g *CompletionGroup) updateTabFind(rl *Instance) {
|
||||||
|
// We perform filter right here, so we create a new completion group, and populate it with our results.
|
||||||
|
// Then overwrite the group's items, (will be refreshed as soon as something is typed in the search)
|
||||||
|
if rl.searchMode != HistoryFind {
|
||||||
|
g.Suggestions = g.filterSuggestions(rl)
|
||||||
|
} else {
|
||||||
|
g.Suggestions = rl.HistorySearcher(string(rl.tfLine))
|
||||||
|
}
|
||||||
|
|
||||||
// Finally, the group computes its new printing settings
|
// Finally, the group computes its new printing settings
|
||||||
g.init(rl)
|
g.init(rl)
|
||||||
|
|
|
@ -200,6 +200,8 @@ type Instance struct {
|
||||||
ViActionCallback func(ViAction, []string)
|
ViActionCallback func(ViAction, []string)
|
||||||
|
|
||||||
RawInputCallback func([]rune) // called on all input
|
RawInputCallback func([]rune) // called on all input
|
||||||
|
|
||||||
|
HistorySearcher func(string) []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInstance is used to create a readline instance and initialise it with sane defaults.
|
// NewInstance is used to create a readline instance and initialise it with sane defaults.
|
||||||
|
@ -229,6 +231,10 @@ func NewInstance() *Instance {
|
||||||
rl.evtKeyPress = make(map[string]func(string, []rune, int) *EventReturn)
|
rl.evtKeyPress = make(map[string]func(string, []rune, int) *EventReturn)
|
||||||
rl.TempDirectory = os.TempDir()
|
rl.TempDirectory = os.TempDir()
|
||||||
|
|
||||||
|
rl.HistorySearcher = func(filter string) []string {
|
||||||
|
grps := rl.completeHistory()
|
||||||
|
return grps[0].filterSuggestions(rl)
|
||||||
|
}
|
||||||
// Registers
|
// Registers
|
||||||
rl.initRegisters()
|
rl.initRegisters()
|
||||||
|
|
||||||
|
|
19
rl.go
19
rl.go
|
@ -27,6 +27,25 @@ func newLineReader(prompt string, noHist bool) *lineReader {
|
||||||
rl.SetHistoryCtrlR("History", fileHist)
|
rl.SetHistoryCtrlR("History", fileHist)
|
||||||
rl.HistoryAutoWrite = false
|
rl.HistoryAutoWrite = false
|
||||||
}
|
}
|
||||||
|
oldSearcher := rl.HistorySearcher
|
||||||
|
rl.HistorySearcher = func(filter string) []string {
|
||||||
|
searcherHandler := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("searcher"))
|
||||||
|
if searcherHandler == rt.NilValue {
|
||||||
|
return oldSearcher(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err := rt.Call1(l.MainThread(), searcherHandler, rt.StringValue(filter))
|
||||||
|
entries := []string{}
|
||||||
|
if err == nil && ret.Type() == rt.TableType {
|
||||||
|
util.ForEach(ret.AsTable(), func(k rt.Value, v rt.Value) {
|
||||||
|
if k.Type() == rt.IntType && v.Type() == rt.StringType {
|
||||||
|
entries = append(entries, v.AsString())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries
|
||||||
|
}
|
||||||
rl.ShowVimMode = false
|
rl.ShowVimMode = false
|
||||||
rl.ViModeCallback = func(mode readline.ViMode) {
|
rl.ViModeCallback = func(mode readline.ViMode) {
|
||||||
modeStr := ""
|
modeStr := ""
|
||||||
|
|
Loading…
Reference in New Issue