Skip to content

Commit

Permalink
readline: remove direct access to (*Instance).Config
Browse files Browse the repository at this point in the history
  • Loading branch information
slingamn committed Mar 9, 2023
1 parent 29bb15a commit ffc66db
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (a autoCompleterFn) Do(line []rune, pos int) (newLine [][]rune, length int)

type stdOS struct {
rl *readline.Instance
historyFile string
closeChan chan struct{}
interruptChan chan struct{}
}
Expand Down Expand Up @@ -169,25 +170,26 @@ func (o *stdOS) Readline(opts interp.ReadlineOpts) (string, error) {
historyFile = filepath.Join(cacheDir, "fq/history")
_ = os.MkdirAll(filepath.Dir(historyFile), 0700)

o.rl, err = readline.NewEx(&readline.Config{
cfg := &readline.Config{
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:]))
}

return runeNames, shared
})
}
o.rl, err = readline.NewEx(cfg)
if err != nil {
return "", err
}
}

if opts.CompleteFn != nil {
o.rl.Config.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:]))
}

return runeNames, shared
})
o.historyFile = historyFile
}

o.rl.SetPrompt(opts.Prompt)
Expand All @@ -205,7 +207,7 @@ func (o *stdOS) Readline(opts interp.ReadlineOpts) (string, error) {

func (o *stdOS) History() ([]string, error) {
// TODO: refactor history handling to use internal fs?
r, err := os.Open(o.rl.Config.HistoryFile)
r, err := os.Open(o.historyFile)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ffc66db

Please sign in to comment.