module RedmineCLI::Helpers::Input

Helpers for input

Public Instance Methods

ask(text, params = {}) click to toggle source

Asks user for something.

Params:

  • `:default`

  • `:limited_to` - can be `Array<String>` or `Regexp`

  • `:required` - force user input until it's not empty

# File lib/redmine_cli/helpers/input.rb, line 75
def ask(text, params = {})
  default_set = params.key?(:default)
  if params[:required]
    fail('required + default options') if default_set
    fail('required + array with limits') if params[:limited_to].is_a? Array
  end

  print_prompt_message(text, params)
  input = read(params)

  return params[:default] if default_set && input.empty?
  input
end
ask_for_object(object_list) click to toggle source

prints names as enumerable list and asks user for element

# File lib/redmine_cli/helpers/input.rb, line 39
def ask_for_object(object_list)
  fail EmptyList if object_list.size.zero?

  # From 1
  i = 0
  object_list = object_list.map { |obj| [(i += 1), obj] }.to_h

  print_object_list(object_list)
  puts
  input = ask m(:select_item_from_list),
              default: '1',
              limited_to: ->(str) { (1..i).cover? str.to_i }

  object_list[input.to_i]
end
ask_for_user(message, params = {}) click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 14
def ask_for_user(message, params = {})
  params[:limited_to] = ->(input) { @cached_user = RedmineRest::Models::User.find_by_id(input) }

  ask(message, params)
  @cached_user
end
ask_from_text_editor(welcome_message = '') click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 21
def ask_from_text_editor(welcome_message = '')
  file = Tempfile.open('redmine_cli') do |f|
    f.write welcome_message
    f
  end

  editor = Config['editor'] || ENV['EDITOR'] || 'nano'

  system(editor + ' ' + file.path)
  result = File.read(file)
  file.unlink

  result
end
ask_url(text, params = {}) click to toggle source

ask with :limited_to set to url regexp. TODO: make good regexp

# File lib/redmine_cli/helpers/input.rb, line 59
def ask_url(text, params = {})
  params[:limited_to] = /\S*/
  url = ask(text, params).strip
  url = "http://#{url}" unless url =~ %r{\Ahttps?://}

  url
end

Private Instance Methods

fit_in_limit?(input, limit) click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 121
def fit_in_limit?(input, limit)
  case limit
  when Array then limit.include?(input)
  when Proc then limit.call(input)
  when Regexp then limit =~ input

  else fail('limit should be Array or Regexp or Proc')
  end
end
read(params) click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 91
def read(params)
  return read_with_limitations(params) if params[:limited_to]
  return read_until_not_empty if params[:required]

  read_line
end
read_line() click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 131
def read_line
  $stdin.gets.chomp
end
read_until_not_empty() click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 98
def read_until_not_empty
  loop do
    input = read_line
    return input unless input.empty?

    puts m(:error_input_required)
  end
end
read_with_limitations(params) click to toggle source
# File lib/redmine_cli/helpers/input.rb, line 107
def read_with_limitations(params)
  input = nil

  loop do
    input = read_line
    break if fit_in_limit?(input, params[:limited_to])
    return params[:default] if input.empty? && params.key?(:default)

    puts m(:error_try_again)
  end

  input
end