consider trailing whitespace a part of the prompt, making copy/paste more straight forward #1645
Consider white space between prompt and command to be part of the prompt. this usually makes copy/paste more straight forward.
For example, if highlighting this shell session:
Before the patch, attempting to select the command with the mouse for copy & paste will include the space between
According to man (1) bash, the default PS1 is
An alternative implementation could make spaces an extra special token that cannot be selected. but I don't know the right class for that. Another variation could be to really consider only the first space as part of the prompt. Please let me know if you prefer any of that.
Regarding tests: I don't quite get why the first element in
The text was updated successfully, but these errors were encountered:
I'm not to deep into pygments code of course, so I hope I get you right. I've tried to create subclass lexer that makes the whitespace a
The whitespace is still selected, so at least this doesn't help, I'm still able to select the whitespace.
Ok, just to understand this properly: If you change it to be of part of the prompt, it doesn't get selected, but if it's whitespace it does? I'm not sure I'm following what exactly you're doing. If I run this through the Bash shell session, I get:
If I double-click on
Exactly. That's because the
Double click only only selects the word (command in this case) without any surrounding whitespace. But try a "triple click" (which is supposed to select the whole line) and you get the starting space:
The same thing happens e.g. when you have for example multiple lines of commands (and don't show output) and the user wants to select multiple lines. Take this Sphinx doc as an example:
New developers: just run these commands to set up your PC to get started: .. code-block:: console user@host:~$ apt update user@host:~$ apt install -y python3 python3-pip user@host:~$ mkdir -p ~/.config/pip/ user@host:~$ echo -e "[install]\nupdate-strategy = eager" > ~/.config/pip/pip.conf
From the rendered HTML I'd expect that users can copy/paste the entire code block, paste it in the terminal, and all commands run through (assuming all commands are non-interactive). This is properly assured by the
For comparison, here is the same selection with the patch applied (notice how the space after the
I too feel a bit uncomfortable about this, I agree. I looked into this in a bit more detail, and believe I found what's causing it. But I think the answer is... not pretty. Not sure you want to know ;-). In any case I believe my patch only shows a problem, and does not cause it.
But here's what's happening: In both cases (with/without patch), the first
I have modified the code a bit to get some text output to see what's happening. Here is the modified code snippet (note that I have also added a print in front of every other yield to make sure that this is really the first one, but I've trimmed that here for brevity):
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index daef1f39..3d97b2ad 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -204,8 +204,12 @@ class ShellSessionBaseLexer(Lexer): backslash_continuation = curcode.endswith('\\\n') else: if insertions: - toks = innerlexer.get_tokens_unprocessed(curcode) + print('insertions:', insertions) + print(' curcode:', repr(curcode)) + toks = list(innerlexer.get_tokens_unprocessed(curcode)) + print(' toks:', toks) for i, t, v in do_insertions(insertions, toks): + print(' yield:', 1, pos+i, t, repr(v)) # proof of what we yield yield pos+i, t, v yield match.start(), Generic.Output, line insertions = 
Here is what I get without my patch:
And here's what I get with my patch:
Bottom line seems to be that the class of the first yielded empty text token - before the prompt - is determined by the element that comes after the prompt.
I have also attached two more test cases to illustrate and dissect this even further. Note that the test