module Quickpress::CLI
Basic input/output functions for the console.
They’re not how we handle the quickpress commands - for that check ‘bin/qp`.
These are ways to interact with the user - get password input, ask yes-or-no questions and such.
Public Instance Methods
Asks ‘prompt` and returns a true/false answer.
# File lib/quickpress/cli.rb, line 51 def ask prompt print "#{prompt} (Y/n) " ans = $stdin.gets.lstrip.strip return true if ans.empty? ['y', 'Y'].include? ans end
Erases from the cursor to the beginning of line.
# File lib/quickpress/cli.rb, line 61 def clear_line print "\r\e[0K" end
Shows ‘prompt` and gets a string from the user.
This ensures that the string is not empty and pre/post spaces are removed.
If ‘allow_empty` is true, allows the user to type an empty input.
# File lib/quickpress/cli.rb, line 25 def get(prompt, allow_empty=false) print "#{prompt} " ret = "" if allow_empty ret = $stdin.gets.lstrip.strip else ret = $stdin.gets.lstrip.strip while ret.empty? end ret end
Shows ‘prompt` and gets a secret string from the user.
Hides things the user is typing.
# File lib/quickpress/cli.rb, line 41 def get_secret prompt ret = "" $stdin.noecho { ret = get(prompt) } puts ret end
Shows ‘prompt` and reads a line from commandline, doing tab-completion according to `completions` Array.
‘separator` is the character to use when separating words.
# File lib/quickpress/cli.rb, line 78 def tab_complete(prompt, completions, separator=" ") abbrevs = Abbrev::abbrev completions word = "" line = "" $stdin.raw do while (char = $stdin.getch) != "\r" if char == "\t" if abbrevs.include?(word) word = abbrevs[word] end elsif (char == "\b" || char.ord == 127) # strange... if word.empty? line.chop! else word.chop! end else word += char if char == separator line += word word.clear end end clear_line print (line + word) end line += word end puts line end
Runs a block of code withing a quick status ‘prompt`.
# File lib/quickpress/cli.rb, line 66 def with_status(prompt) print prompt yield clear_line end