prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.2k stars 714 forks source link

repl shortcut function #528

Open asmeurer opened 7 years ago

asmeurer commented 7 years ago

An idea from https://github.com/jonathanslenders/python-prompt-toolkit/issues/519#issuecomment-318978114 and https://github.com/jonathanslenders/python-prompt-toolkit/issues/519#issuecomment-320065017.

It could be useful to have one higher level of abstraction in shortcuts.py, for the full "main loop" of a prompt REPL. Something like

def repl(execute_callback, message='', **kwargs):
    while True:
        try:
            res = prompt(message, **kwargs)
        except KeyboardInterrupt:
            print("KeyboardInterrupt", file=sys.stderr)
            continue
        except (EOFError, SystemExit):
            break
        execute_callback(res) # Perhaps the callback should also be passed the cli instance

It could also support other features like keeping track of prompt numbers, or an input "queue" (see https://github.com/jonathanslenders/python-prompt-toolkit/issues/519#issuecomment-318978114).

jonathanslenders commented 7 years ago

Let me think about this. I'm not sure how much value it adds, and whether this would actually be used.

asmeurer commented 7 years ago

Well it's just an idea. I would look to new users to see if they would find it useful. Like I said, I personally am already using my own implementations of most of the shortcuts functions, so I likely wouldn't use it myself.