From 7986ef2432ff8adf9359cb72df36e091ca7554ac Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 30 Oct 2023 19:45:13 -0400 Subject: [PATCH] render files that have a byte-order-marker properly --- babi/buf.py | 2 +- tests/features/save_test.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/babi/buf.py b/babi/buf.py index 9ddb78a..347bc39 100644 --- a/babi/buf.py +++ b/babi/buf.py @@ -298,7 +298,7 @@ def tab_string(self) -> str: def rendered_line(self, idx: int, dim: Dim) -> str: x = self._cursor_x if idx == self.y else 0 - expanded = self._lines[idx].expandtabs(self.tab_size) + expanded = self._lines[idx].expandtabs(self.tab_size).lstrip('\ufeff') return scrolled_line(expanded, x, dim.width) # movement diff --git a/tests/features/save_test.py b/tests/features/save_test.py index a960bda..1cea4b1 100644 --- a/tests/features/save_test.py +++ b/tests/features/save_test.py @@ -15,6 +15,17 @@ def test_mixed_newlines(run, tmpdir): h.await_text(r"mixed newlines will be converted to '\n'") +def test_byte_order_marker(run, tmpdir): + src = b'\xef\xbb\xbfhello\n' + f = tmpdir.join('f') + f.write_binary(src) + with run(str(f)) as h, and_exit(h): + # renders ok without an extra space for BOM + h.await_text('\nhello\n') + h.press('^S') + assert f.read_binary() == src + + def test_modify_file_with_windows_newlines(run, tmpdir): f = tmpdir.join('f') f.write_binary(b'foo\r\nbar\r\n')