Module: RedmineCLI::Helpers::Input

Overview

Helpers for input

Instance Method Summary collapse

Methods included from Output

#erb, #message, #print_object_list, #print_prompt_message

Instance Method Details

#ask(text, params = {}) ⇒ Object

Asks user for something.

Params:

  • β€˜:default`

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

  • β€˜:required` - force user input until it’s not empty



75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object

prints names as enumerable list and asks user for element



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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 = {}) ⇒ Object



14
15
16
17
18
19
# 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 = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 = {}) ⇒ Object

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



59
60
61
62
63
64
65
# 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