peterbrittain / asciimatics

A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Apache License 2.0
3.62k stars 237 forks source link

Escape symbol can break AnsiTerminalParser #321

Closed apockill closed 3 years ago

apockill commented 3 years ago

Describe the bug Feeding certain input into ColouredText raises an IndexError.

To Reproduce

from asciimatics.strings import ColouredText
from asciimatics.parsers import AnsiTerminalParser

bad_input = ""
ColouredText(bad_input, parser=AnsiTerminalParser(), colour=None)

The Escape symbol appears to bug out within the AnsiTerminalParser. The above code will raise this error:

Traceback (most recent call last):
  File "/mnt/drive/drive/Projects - In Progress/Project - 2021 - GrokLog/groklog/scrap/bug_report.py", line 6, in <module>
    ColouredText(bad_input, parser=AnsiTerminalParser(), colour=None)
  File "/home/alex/.cache/pypoetry/virtualenvs/groklog-d-X9QCJT-py3.8/lib/python3.8/site-packages/asciimatics/strings.py", line 41, in __init__
    self._parser.reset(self._raw_text, self._init_colour)
  File "/home/alex/.cache/pypoetry/virtualenvs/groklog-d-X9QCJT-py3.8/lib/python3.8/site-packages/asciimatics/parsers.py", line 336, in reset
    new_offset = _handle_escape(state)
  File "/home/alex/.cache/pypoetry/virtualenvs/groklog-d-X9QCJT-py3.8/lib/python3.8/site-packages/asciimatics/parsers.py", line 179, in _handle_escape
    elif st.text[1] == "M":
IndexError: string index out of range

I've also had this happen with other strings containing escape symbols. Here's a string that specifically caused this issue in real-world testing:

real_world_bad_input = "dashboard_1       |"

Expected behavior A ColouredText object that contains the escape symbol.

System details (please complete the following information):


I've been meaning to take a closer look at this bug, but I'm not quite sure I understand how the parser code is supposed to work. Anyways, if it's a simple issue that's great! If not, I can take a closer look at it in a couple days.

peterbrittain commented 3 years ago

Yeah - I hit similar problems when creating the AnsiArt player... The code in master should fix this. Can you try that to check there's not anything else left to fix?

apockill commented 3 years ago

I just tested it on the head of master, it works fine. I tested it with a big log of input I have that used to cause trouble. Thanks!