Skip to content

consider exposing Readline.completion_proc via HighLine::Question#selection #232

@doriantaylor

Description

@doriantaylor

Happy Monday,

Playing around with HighLine, finding myself in a situation where I need finer-grained access to tab completion than an array. Looking at terminal.rb it seems as if it would be pretty easy to re-expose Readline.completion_proc via HighLine::Question.

Something like this would probably be all that was necessary:

def readline_read(question)
  # prep auto-completion
  sel  = question.selection
  proc = sel.is_a?(Proc) ? sel : sel.empty? ? nil : lambda do |str|
      sel.grep(/\A#{Regexp.escape(str)}/)
  end
  Readline.completion_proc = proc

  # work-around ugly readline() warnings
  old_verbose = $VERBOSE
  $VERBOSE    = nil

  raw_answer  = run_preserving_stty do
    Readline.readline("", true)
  end

  $VERBOSE = old_verbose

  raw_answer
end

Then in Highline::Question, something like:

def selection
  if completion.is_a?(Array) or completion.is_a?(Proc)
    completion
  elsif [File, Pathname].include?(completion)
    Dir[File.join(directory.to_s, glob)].map do |file|
      File.basename(file)
    end
  else
    []
  end
end

This is just me eyeballing the issue for now; I can fork and PR if you're amenable.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions