question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Copy/pasting: multiline in a single line prompt

See original GitHub issue

Could we have an option so that prompt only accepts a single line, even when pasting some text ?

Currently, even when multiline is set to False, you can paste multi-line text in it.

Code: Here’s the code i am using:

PROMPT_TOOLKIT_HISTORY = prompt_toolkit.history.FileHistory(conf.histfile)
# ScapyCompleter is a quite basic completer, returning all matching content as a shell would do
_completer = ScapyCompleter()
from prompt_toolkit.token import Token
prompts_style = prompt_toolkit.styles.style_from_dict({
    Token.Prompt: "#2E64FE",
})
def readLineScapy(prompt):
    result = ""
    end = False
    while not end :
        if not end and result != "":
            line = prompt_toolkit.prompt(u"... ", completer=_completer, history=PROMPT_TOOLKIT_HISTORY)
        else:
            line = prompt_toolkit.prompt(u">>> ", completer=_completer, style=prompts_style, history=PROMPT_TOOLKIT_HISTORY)
        if line.strip()[-1:] in [":", "(", ",", "\\"]:
            end = False
        elif result == "":
            end = True
        if line.strip() == "":
            end = True
        result = result + "\n" + line
    result = result.replace("\\\n", "")
    return str(result)
# Run console with python_toolkit
code.interact(banner="hello, this is a great shell", readfunc=readLineScapy)

The goal of this code is to emulate a shell. In that way, we need to print “…” or “>>>” on each line. readlinescapy is using prompt_toolkit.prompt to get the same input as in a shell…

When one copy multi-line, the prompt function takes all the lines, so ... is not printed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
asmeurercommented, Jul 2, 2017

This is the function I used to detect if a block of Python code is “multiline” or not, using the tokenize module. Feel free to reuse that code if it helps you (there are other useful functions in that file too). This also goes for ptpython (this is a more robust method than what is used there).

0reactions
asmeurercommented, Aug 3, 2017

My main issue on the prompt-toolkit side is with the Input classes. If it’s possible, it would be nice if they didn’t require a fileno, but it isn’t completely clear to me from the code if it is possible. Also it seems they should just be file-like objects. I can open an issue about that.

For the rest, it only seems like something that could go in prompt-toolkit if you ever want to add some abstractions for REPL loops (an abstraction around the common while True: prompt() loop). For me personally, I already stopped using the prompt-toolkit provided abstractions prompt_toolkit.shortcuts.prompt and prompt_toolkit.shortcuts.create_application a while ago, because they didn’t provide enough customization for what I wanted. I create Buffer (actually a Buffer subclass), Application, and CommandLineInterface objects manually (I’m still using create_prompt_layout, but that may change change too, as I more and more customize things to my own tastes).

But for users who are less picky about customizing and are fine with prompt-toolkit defaults, these shortcut functions are nice. And they’re great for starting because you can get up and running very quickly with a basic prompt application, and then build on top of them once you get the hang of the library.

So maybe a shortcut function 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

in shortcuts.py could be useful. And then you could build on that to do the queueing behavior, and possibly other behaviors too (e.g., I keep IPython-style prompt numbers in my loop).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Copy multiple lines from Windows Command prompt without ...
Select long command line using the left mouse button · Press down shift · Right click into the selected area (to copy) ·...
Read more >
How to copy/paste multi-line command line ... - Stack Overflow
I think you need to powershellafy it like this: $retrain = @{ bottleneck_dir = “/tf_files” how_many_training_steps = “500" model_dir= ...
Read more >
Copy & Paste Multiple Lines Into Terminal #shorts - YouTube
Really short tidbit to save some time in terminal.#php #bash #terminal #coding #programming.
Read more >
Multiple copy paste line issue - Microsoft Community
Whenever I copy text with a new line character or multiple lines at a time, windows will paste multiple blanks lines.
Read more >
Pasting text with newline at shell prompt does not execute ...
Under Preferences -> General -> Selection I have "Copied text includes trailing newline" turned on. Under Edit -> Paste Special I have "Warn ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found