Inconsistently styled space after prompt

Issue #1379 new
Miro Hrončok
created an issue

I've recently find out that the space after prompt is styled inconsistently. See the examples.

$ pip install pygments
Collecting pygments
  Using cached Pygments-2.2.0-py2.py3-none-any.whl
Installing collected packages: pygments
Successfully installed pygments-2.2.0
Python 3.6.2 (default, Aug 11 2017, 11:59:59) 
[GCC 7.1.1 20170622 (Red Hat 7.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
>>> import pygments.lexers
>>> import pygments.formatters.html
>>> pygments_formatter = pygments.formatters.html.HtmlFormatter()
>>> def highlight(code, lang):
...     lexer = pygments.lexers.get_lexer_by_name(lang)
...     return pygments.highlight(code, lexer, pygments_formatter).strip()
>>> highlight('(venv) $ foo', 'console')
'<div class="highlight"><pre><span></span><span class="gp">(venv) $</span> foo\n</pre></div>'
>>> highlight('>>> import pygments', 'pycon')
'<div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pygments</span>\n</pre></div>'
>>> highlight('>>> with foo:\n...     bar()', 'pycon')
'<div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">foo</span><span class="p">:</span>\n<span class="gp">... </span>    <span class="n">bar</span><span class="p">()</span>\n</pre></div>'

Important parts:

The console lexer puts the space after the prompt span:

<span class="gp">(venv) $</span> foo

The pycon lexer puts the first space in the prompt span:

<span class="gp">&gt;&gt;&gt; </span>
<span class="gp">... </span>    

I find this behavior inconsistent an potentially problematic. In my opinion, the first space is part of the prompt and the pycon lexer is right, whereas the console lexer has a bug.

We had to incorporate a workaround in order to be able to style the prompt including the first space:

def style_space_after_prompt(html):
    return re.sub(r'<span class="gp">([^<]*[^<\s])</span>(\s)',
                  r'<span class="gp">\1\2</span>',

I wonder what do you think: is the first space supposed to be in the prompt span? Or does it deserve its own class?


Comments (2)

  1. encukou

    Note on motivation: the space usually looks the same whether it's in gp or not. But code examples arguably work nicer if the prompt is made non-selectable (using user-select: none in CSS). And that makes whitespace matter – especially for Python.

  2. Log in to comment