Class: Pick::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pick/cli.rb

Class Method Summary collapse

Class Method Details

.prompt(input_io, options = {}) ⇒ String+

Read items from input_io and prompt the user to choose among them.

Parameters:

  • input_io (IO)

    The input IO from which to read the items

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prompt_input (IO)

    IO to use for prompt input

  • :prompt_output (IO)

    IO to use for prompt output

  • :tty_dev (String) — default: '/dev/tty'

    The device to use as the default for TTY i/o, used for :prompt_input and :prompt_output

  • :input_delimiter (String)

    The string that separates items on the input stream.

  • :multiple (Boolean)

    Whether to select a single item or multiple items

Returns:

  • (String, Array<String>)

    A single item or an array of items chosen by the user.



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, options={})
  prompt_opts = {}
  options = options.dup

  tty_dev = options[:tty_dev] || '/dev/tty' # TODO windows?

  options[:prompt_input] ||= File.open(tty_dev, 'r')
  options[:prompt_output] ||= File.open(tty_dev, 'a')

  options[:per_page] ||= TTY::Screen.height - 2
  if options[:per_page] && options[:per_page] >= 1
    prompt_opts[:per_page] = options.fetch(:per_page)
  end

  if input_io.tty? && options.fetch(:prompt_output).tty? \
     && !options[:quiet]
    options.fetch(:prompt_output).puts('Waiting for input items...')
  end

  # read the input io
  data = input_io.read

  # set default separator
  separator = options[: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: options.fetch(:prompt_input),
    output: options.fetch(:prompt_output)
  )

  if options[:multiple]
    prompt = options.fetch(:prompt, 'Select multiple items:')
    p.multi_select(prompt, choices, prompt_opts)
  else
    prompt = options.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.

Parameters:

  • input_io (IO)

    The input IO from which to read the items

  • options (Hash) (defaults to: {})

    See prompt

Options Hash (options):

  • :output_io (IO) — default: STDOUT

    The IO to print selected items to.

  • :output_delimiter (String) — default: "\n"

    The delimiter to print after each selected item.



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, options={})
  answer = prompt(input_io, options)

  return if answer.empty?

  output_io = options[:output_io] || STDOUT

  out_sep = options[:output_delimiter]
  out_sep ||= "\n"

  output_io.print(Array(answer).join(out_sep) + out_sep)

  answer
end