Module: Readline
- Defined in:
- lib/vv/readline_methods.rb
Class Method Summary collapse
- .prompt(message, exit_on_exit: true) ⇒ Object
- .prompt_yn(message, default: nil) ⇒ Object
- .safe_readline(prompt) ⇒ Object
- .vv_enabled? ⇒ Boolean
Class Method Details
.prompt(message, exit_on_exit: true) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vv/readline_methods.rb', line 20 def prompt , exit_on_exit: true .is_a! String .concat " " input = Readline.safe_readline if input.nil? puts exit end input.chomp! exit if input == "exit" and exit_on_exit input end |
.prompt_yn(message, default: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vv/readline_methods.rb', line 39 def prompt_yn(, default: nil) default = true if default == "y" or default == "Y" default = false if default == "n" or default == "N" option = case default when nil "[y/n]" when false "[y/N]" when true "[Y/n]" end option.prepend " " += option answer = nil loop do input = prompt() answer = default if input == "" answer = true if input == "y" or input == "Y" answer = false if input == "n" or input == "N" break unless answer.nil? puts "Unrecognized input. Please enter response again." end answer end |
.safe_readline(prompt) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vv/readline_methods.rb', line 8 def safe_readline(prompt) stty_save = `stty -g`.chomp begin Readline.readline prompt rescue Interrupt puts system("stty", stty_save) exit end end |
.vv_enabled? ⇒ Boolean
3 4 5 |
# File 'lib/vv/readline_methods.rb', line 3 def vv_enabled? true end |