Copy/pasting: multiline in a single line prompt
See original GitHub issueCould 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:
- Created 6 years ago
- Comments:11 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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).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 abstractionsprompt_toolkit.shortcuts.prompt
andprompt_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 usingcreate_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
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).