lib/cli/ui/prompt.rb in cli-ui-1.4.0 vs lib/cli/ui/prompt.rb in cli-ui-1.5.0
- old
+ new
@@ -1,9 +1,24 @@
# coding: utf-8
require 'cli/ui'
require 'readline'
+module Readline
+ unless const_defined?(:FILENAME_COMPLETION_PROC)
+ FILENAME_COMPLETION_PROC = proc do |input|
+ directory = input[-1] == '/' ? input : File.dirname(input)
+ filename = input[-1] == '/' ? '' : File.basename(input)
+
+ (Dir.entries(directory).select do |fp|
+ fp.start_with?(filename)
+ end - (input[-1] == '.' ? [] : ['.', '..'])).map do |fp|
+ File.join(directory, fp).gsub(/\A\.\//, '')
+ end
+ end
+ end
+end
+
module CLI
module UI
module Prompt
autoload :InteractiveOptions, 'cli/ui/prompt/interactive_options'
autoload :OptionsHandler, 'cli/ui/prompt/options_handler'
@@ -189,16 +204,16 @@
handler.options
end
raise(ArgumentError, 'insufficient options') if options.nil? || options.empty?
navigate_text = if CLI::UI::OS.current.supports_arrow_keys?
- "Choose with ↑ ↓ ⏎"
+ 'Choose with ↑ ↓ ⏎'
else
"Navigate up with 'k' and down with 'j', press Enter to select"
end
- instructions = (multiple ? "Toggle options. " : "") + navigate_text
+ instructions = (multiple ? 'Toggle options. ' : '') + navigate_text
instructions += ", filter with 'f'" if filter_ui
instructions += ", enter option with 'e'" if select_ui && (options.size > 9)
puts_question("#{question} {{yellow:(#{instructions})}}")
resp = interactive_prompt(options, multiple: multiple, default: default)
@@ -210,13 +225,13 @@
# reset the question to include the answer
resp_text = resp
if multiple
resp_text = case resp.size
when 0
- "<nothing>"
+ '<nothing>'
when 1..2
- resp.join(" and ")
+ resp.join(' and ')
else
"#{resp.size} items"
end
end
puts_question("#{question} (You chose: {{italic:#{resp_text}}})")
@@ -249,13 +264,13 @@
end
def readline(is_file: false)
if is_file
Readline.completion_proc = Readline::FILENAME_COMPLETION_PROC
- Readline.completion_append_character = ""
+ Readline.completion_append_character = ''
else
Readline.completion_proc = proc { |*| nil }
- Readline.completion_append_character = " "
+ Readline.completion_append_character = ' '
end
# because Readline is a C library, CLI::UI's hooks into $stdout don't
# work. We could work around this by having CLI::UI use a pipe and a
# thread to manage output, but the current strategy feels like a