piotrmurach / tty-prompt

A beautiful and powerful interactive command line prompt
https://ttytoolkit.org
MIT License
1.47k stars 136 forks source link

prompt.ask with nil question #103

Closed roscom closed 5 years ago

roscom commented 5 years ago

I am using TTY::Prompt in a REPL.

TTY::Prompt is initialised with a prefix, as in, TTY::Prompt.new(prefix: '?')

Often times, I don't need to put a question, just the prompt, however, prompt.ask requires at least one non-blank string parameter (being the question), otherwise prompt.ask will summarily exit.

It would be very helpful if prompt.ask would accept 0.. parameters as legal.

Secondly, it would also be very useful if the prompt.ask method accepted an empty response as legal instead of summarily exiting.

piotrmurach commented 5 years ago

Hi Ross,

Thanks for using tty-prompt!

I'm not sure I understand the point of ask prompt that doesn't ask anything and doesn't accept any value. It kind of defeats the point of tty-prompt, e.i. tool for gathering user input. Are you sure you're using the right tool for the job? I mean tty-reader sounds more like what you need, e.i. something that responds to key events. I've even created an example of shell/REPL that you may want to check out.

roscom commented 5 years ago

Hi Piotr

Thank you for responding.

In the REPL I have (which uses 'rb-readline'), I use the empty return response to output a help message appropriate for that response. The help msg is a set of commands that are applicable for that response at that point in the REPL. Works well.

I am looking to replace that gem - it's a bit buggy, although has some nice features.

TTY::Prompt.new(prefix: '?') presents sufficient information for the user to enter the next command.

Very minimalist I know. But it works well for the users.

Secondly, I currently manage the history in a redis cache, outside of rb-readline which is then available to all users. On session start up, the REPL currently populates rb-readline history from the redis cache. That would be a handy feature to be able to do that in your tty-* modules.

Another feature for tty-reader & tty-prompt would be to allow text to pre-populate the input line for editing which is sourced from other than history - another keyword parameter perhaps on prompt.ask

Hopefully, this explains what I'm looking for relative to your libraries.

Regards Ross

piotrmurach commented 5 years ago

Ross, thanks for explaining this. 🙇

In that case, consider me persuaded to allow ask prompt without a question.

Regarding the option to prepopulate an ask prompt with an actual line content sounds reasonable to me. I would probably opt for :line or :source option as the :input is already taken by stdin.

When it comes to loading history, it would need to be done by tty-reader. Would you mind posting an issue over there?

piotrmurach commented 5 years ago

The ask prompt now accepts empty question and allows to set the line input for editing. I added an issue about reading history https://github.com/piotrmurach/tty-reader/issues/14