Escape symbol can break AnsiTerminalParser
apockill opened this issue · comments
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 = "�[35mdashboard_1 |�"
Expected behavior
A ColouredText object that contains the escape symbol.
System details (please complete the following information):
- OS and version: Ubuntu 20.04
- Python version: Python 3.8.10
- Python distribution: CPython
- Asciimatics version: 1.13.0
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.
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?
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!