1
1
mirror of https://github.com/wader/fq.git synced 2025-01-01 10:02:15 +03:00

Merge pull request #616 from wader/interp-subrepl-complete-regression

interp: Fix input completion regression in sub-REPLs
This commit is contained in:
Mattias Wadman 2023-03-10 14:06:46 +01:00 committed by GitHub
commit 52bdb5e65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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