module Lolcommits::Plugin::ConfigurationHelper

Public Instance Methods

parse_user_input(str) click to toggle source

handle for bools, strings, ints and blanks from user input

# File lib/lolcommits/plugin/configuration_helper.rb, line 7
def parse_user_input(str)
  if 'true'.casecmp(str).zero?
    true
  elsif 'false'.casecmp(str).zero?
    false
  elsif str =~ /^[0-9]+$/
    str.to_i
  elsif str.strip.empty?
    nil
  else
    str
  end
end
prompt_autocomplete_hash(prompt, items, name: 'name', value: 'value', suggest_words: 5) click to toggle source

user input with autocomplete (via tab) through array of named values

e.g. prompt_autocomplete_hash(“Organization: ”, orgs)

where orgs are an array of hashes like so (with string keys): [

{ 'name' => 'some human readable name', 'value' => 1234 },

] User will be asked for Organization, can tab to autocomplete, and chosen value is returned.

# File lib/lolcommits/plugin/configuration_helper.rb, line 32
def prompt_autocomplete_hash(prompt, items, name: 'name', value: 'value', suggest_words: 5)
  words = items.map { |item| item[name] }.sort
  puts "e.g. #{words.take(suggest_words).join(', ')}" if suggest_words.positive?
  completed_input = gets_autocomplete(prompt, words)
  items.find { |item| item[name] == completed_input }[value]
end

Private Instance Methods

gets_autocomplete(prompt, words) click to toggle source
# File lib/lolcommits/plugin/configuration_helper.rb, line 41
def gets_autocomplete(prompt, words)
  completion_handler = proc { |s| words.grep(/^#{Regexp.escape(s)}/) }
  Readline.completion_append_character = ''
  Readline.completion_proc = completion_handler

  while (line = Readline.readline(prompt, true).strip)
    return line if words.include?(line)

    puts "'#{line}' not found"
  end
end