Sha256: e3ae3c106c000110fc9b807bd35255c15b9ff19b8d14f931b6ea158cf3ba82bb
Contents?: true
Size: 1.6 KB
Versions: 6
Compression:
Stored size: 1.6 KB
Contents
module Lolcommits module Plugin module ConfigurationHelper # handle for bools, strings, ints and blanks from user input 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 # 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. 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 > 0 completed_input = gets_autocomplete(prompt, words) items.find { |item| item[name] == completed_input }[value] end private 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 end end end
Version data entries
6 entries across 6 versions & 1 rubygems