Module: Doing::PromptInput
- Included in:
- Prompt
- Defined in:
- lib/doing/prompt/input.rb
Overview
Methods for requesting user text input
Instance Method Summary collapse
-
#enter_text(prompt, default_response: '') ⇒ String
deprecated
Deprecated.
Use #read_line instead
-
#read_line(prompt: 'Enter text', completions: [], default_response: '') ⇒ String
Request single-line input using Readline.
-
#read_lines(prompt: 'Enter text', completions: [], default_response: '') ⇒ String
Request multi-line input using Readline.
-
#request_lines(prompt: 'Enter text', default_response: '') ⇒ Object
deprecated
Deprecated.
Use #read_lines instead
Instance Method Details
#enter_text(prompt, default_response: '') ⇒ String
Deprecated.
Use #read_line instead
Request single-line input
19 20 21 22 23 24 25 |
# File 'lib/doing/prompt/input.rb', line 19 def enter_text(prompt, default_response: '') $stdin.reopen('/dev/tty') return default_response if @default_answer print "#{yellow(prompt).sub(/:?$/, ':')} #{reset}" $stdin.gets.strip end |
#read_line(prompt: 'Enter text', completions: [], default_response: '') ⇒ String
Request single-line input using Readline. Allows for control sequences and tab completions
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/doing/prompt/input.rb', line 41 def read_line(prompt: 'Enter text', completions: [], default_response: '') $stdin.reopen('/dev/tty') return default_response if @default_answer unless completions.empty? completions.sort! comp = proc { |s| completions.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = ' ' Readline.completion_proc = comp end begin Readline.readline("#{yellow(prompt).sub(/:?$/, ':')} #{reset}", true).strip rescue Interrupt raise UserCancelled end end |
#read_lines(prompt: 'Enter text', completions: [], default_response: '') ⇒ String
Request multi-line input using Readline. Allows for control sequences and tab completion
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/doing/prompt/input.rb', line 73 def read_lines(prompt: 'Enter text', completions: [], default_response: '') $stdin.reopen('/dev/tty') return default_response if @default_answer completions.sort! comp = proc { |s| completions.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = ' ' Readline.completion_proc = comp puts format(['%<promptcolor>s%<prompt>s %<textcolor>sEnter a blank line', '(%<keycolor>sreturn twice%<textcolor>s)', 'to end editing and save,', '%<keycolor>sCTRL-C%<textcolor>s to cancel%<reset>s'].join(' '), { promptcolor: boldgreen, prompt: prompt.sub(/:?$/, ':'), textcolor: yellow, keycolor: boldwhite, reset: reset }) res = [] begin while (line = Readline.readline('> ', true)) break if line.strip.empty? res << line.chomp end rescue Interrupt return nil end res.join("\n").strip end |
#request_lines(prompt: 'Enter text', default_response: '') ⇒ Object
Deprecated.
Use #read_lines instead
Request multi-line input
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/doing/prompt/input.rb', line 113 def request_lines(prompt: 'Enter text', default_response: '') $stdin.reopen('/dev/tty') return default_response if @default_answer ask_note = [] reader = TTY::Reader.new(interrupt: -> { raise Errors::UserCancelled }, track_history: false) puts "#{boldgreen(prompt.sub(/:?$/, ':'))} #{yellow('Hit return for a new line, ')}#{boldwhite('enter a blank line (')}#{boldyellow('return twice')}#{boldwhite(') to end editing')}" loop do res = reader.read_line(green('> ')) break if res.strip.empty? ask_note.push(res) end ask_note.join("\n").strip end |