Class: Pick::CLI
- Inherits:
-
Object
- Object
- Pick::CLI
- Defined in:
- lib/pick/cli.rb
Class Method Summary collapse
-
.prompt(input_io, options = {}) ⇒ String+
Read items from input_io and prompt the user to choose among them.
-
.run(input_io, options = {}) ⇒ Object
Read items from input_io and prompt the user to choose among them.
Class Method Details
.prompt(input_io, options = {}) ⇒ String+
Read items from input_io and prompt the user to choose among them.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pick/cli.rb', line 48 def self.prompt(input_io, ={}) prompt_opts = {} = .dup tty_dev = [:tty_dev] || '/dev/tty' # TODO windows? [:prompt_input] ||= File.open(tty_dev, 'r') [:prompt_output] ||= File.open(tty_dev, 'a') [:per_page] ||= TTY::Screen.height - 2 if [:per_page] && [:per_page] >= 1 prompt_opts[:per_page] = .fetch(:per_page) end if input_io.tty? && .fetch(:prompt_output).tty? \ && ![:quiet] .fetch(:prompt_output).puts('Waiting for input items...') end # read the input io data = input_io.read # set default separator separator = [:input_delimiter] if separator.nil? data.gsub!("\r\n", "\n") separator = "\n" end # split input into choices choices = data.split(separator) p = TTY::Prompt.new( input: .fetch(:prompt_input), output: .fetch(:prompt_output) ) if [:multiple] prompt = .fetch(:prompt, 'Select multiple items:') p.multi_select(prompt, choices, prompt_opts) else prompt = .fetch(:prompt, 'Select an item:') p.select(prompt, choices, prompt_opts) end end |
.run(input_io, options = {}) ⇒ Object
Read items from input_io and prompt the user to choose among them. Then print the output to options or stdout.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pick/cli.rb', line 17 def self.run(input_io, ={}) answer = prompt(input_io, ) return if answer.empty? output_io = [:output_io] || STDOUT out_sep = [:output_delimiter] out_sep ||= "\n" output_io.print(Array(answer).join(out_sep) + out_sep) answer end |