User friendly command-line input with ruby readline
While getting user input for various fields, you’d like some values to appear so the user can choose them via arrow keys, or Control-R. Readline makes this very simple.
Readline::HISTORY.push(*values) unless values.empty?
Readline::HISTORY is like an array. You can
push values to it, print it using “to_a”.
To get user input, instead of “gets” we can use:
str = Readline::readline('>', boolean)
If boolean is true, entries will be automatically pushed into HISTORY.
When a user enters multiple records, its easy to save entries in a file based on column name, and add them to HISTORY before prompting him. So the user gets column-specific history. I am doing this in bugzyrb (a command-line bug tracker). Its on github in a file named cmdapp.rb history_read and history_save.
I might mention that Highline, also allows a readline flag in ask and question. However, from what I tried, it does not push valid values into HISTORY. It defines a completion_proc so you can tab complete the given options.