Closed michaelmacinnis closed 10 years ago
It's not possible in all cases. In particular, it is not possible when TERM=dumb
.
I believe that an interrupted prompt is a poor User Experience, so I'd be leery of adding an Abort method even to the platforms that can support it.
OK. In many CLIs it's possible for the user to signal that the current (possibly multi-line command) will not be completed and should not be executed. As a trivial example, when using most Unix shells it is possible to enter something like:
if true; then
and press return only to realize that you don't want to execute the (currently incomplete) command and press Ctrl-C to return to the prompt without completing or executing the incomplete command.
Is there a way to accomplish this when using liner?
...not exactly. In theory, it could work if Liner owned SIGINT so it could return a literal Ctrl-C as the command string. I'd rather not go there, but I'd at least consider a pull request. The main sticking point is the difficulty implementing it for TERM=dumb
.
If you're implementing bash, any syntax error (for example, a bare ;
) is enough to abort the block. If you're implementing your own language, I might suggest adding a token or keyword that aborts execution of any partial command. Or if you update to c0c66ddb5b2843eef8e7ae379194e59b118b796e, Ctrl-D on an empty line (returns io.EOF, even when TERM=dumb
) should be similar to what you're asking for.
I agree with not having liner intercept SIGINT. I like that it isn't as I need to intercept it for other purposes and it would complicate things if liner was also interested in that signal. I may need to do something very similar to what you suggest to abort the execution of a partial command but I would still be hoping to have this behavior triggered by the user pressing Ctrl-C and in this case I believe I would still need a way to notify liner to erase the current input buffer and redraw the prompt.
@michaelmacinnis Can you explain why the current Ctrl-D exit method is not enough? I like it, because in unix Ctrl-C usually closes the program, and various programs (like PostgreSQL psql) use the Ctrl-D to abort prompt when having an incomplete command.
@ElPincheTopo Ctrl-C can be used in most Unix shells to signal that the current (possibly multi-line command) will not be completed and should not be executed. For example, it is possible to enter something like:
if true; then
in a Bourne-like shell and press return only to realize that you don't want to execute the (currently incomplete) command and press Ctrl-C to return to the prompt without completing or executing the incomplete command.
I can provide the same behaviour on Unix with the changes I've made to liner but have not worked out (or spent any time, really, working out) how to make similar changes to liner on Windows or thought about what the behaviour should be on dumb terminals.
Ctrl-D is typically used to exit a shell or (as I've just discovered) erase the character under the cursor. These behaviours (abort and Ctrl-D) do not seem similar to me.
Would it be possible to add an Abort method so that a call to Prompt could be aborted? If that's possible, it would also be great if there was a way to know that Prompt was aborted when it returns.