1
1
mirror of https://github.com/wader/fq.git synced 2024-11-22 15:45:45 +03:00

interp: Fix input completion regression in sub-REPLs

readline Config was used to pass completer function per readline call,
was changed in #612 and caused regression. Now use our own member in
stdOS to pass it instead.

Add a test but test script completer is implemented differently.
This commit is contained in:
Mattias Wadman 2023-03-10 13:51:05 +01:00
parent fcacd7e3a3
commit 3dd2c61d3c
3 changed files with 24 additions and 11 deletions

View File

@ -27,6 +27,7 @@ func maybeLogFile() {
}
}
// function implementing readline.AutoComplete interface
type autoCompleterFn func(line []rune, pos int) (newLine [][]rune, length int)
func (a autoCompleterFn) Do(line []rune, pos int) (newLine [][]rune, length int) {
@ -38,6 +39,7 @@ type stdOS struct {
historyFile string
closeChan chan struct{}
interruptChan chan struct{}
completerFn interp.CompleteFn
}
func newStandardOS() *stdOS {
@ -174,17 +176,19 @@ func (o *stdOS) Readline(opts interp.ReadlineOpts) (string, error) {
HistoryFile: historyFile,
HistorySearchFold: true,
}
if opts.CompleteFn != nil {
cfg.AutoComplete = autoCompleterFn(func(line []rune, pos int) (newLine [][]rune, length int) {
names, shared := opts.CompleteFn(string(line), pos)
var runeNames [][]rune
for _, name := range names {
runeNames = append(runeNames, []rune(name[shared:]))
}
cfg.AutoComplete = autoCompleterFn(func(line []rune, pos int) (newLine [][]rune, length int) {
if o.completerFn != nil {
return nil, 0
}
return runeNames, shared
})
}
names, shared := o.completerFn(string(line), pos)
var runeNames [][]rune
for _, name := range names {
runeNames = append(runeNames, []rune(name[shared:]))
}
return runeNames, shared
})
o.rl, err = readline.NewEx(cfg)
if err != nil {
return "", err
@ -192,6 +196,9 @@ func (o *stdOS) Readline(opts interp.ReadlineOpts) (string, error) {
o.historyFile = historyFile
}
// inject completer to autocompleter
o.completerFn = opts.CompleteFn
o.rl.SetPrompt(opts.Prompt)
line, err := o.rl.Readline()
if errors.Is(err, readline.ErrInterrupt) {

View File

@ -150,9 +150,11 @@ type Platform struct {
Arch string
}
type CompleteFn func(line string, pos int) (newLine []string, shared int)
type ReadlineOpts struct {
Prompt string
CompleteFn func(line string, pos int) (newLine []string, shared int)
CompleteFn CompleteFn
}
type OS interface {

View File

@ -68,4 +68,8 @@ color
colors
compact
completion_timeout
mp3> .frames[0] | repl
> .frames[0] mp3_frame> .he\t
header
> .frames[0] mp3_frame> ^D
mp3> ^D