From 41781767b3b95e3298c59774e7959ad109e9d01b Mon Sep 17 00:00:00 2001 From: Dominic Davis-Foster Date: Sun, 10 Dec 2023 23:13:00 +0000 Subject: [PATCH] Add splitlines method to StringList for textwrap.indent --- domdf_python_tools/stringlist.py | 12 ++++++++++++ tests/test_stringlist.py | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/domdf_python_tools/stringlist.py b/domdf_python_tools/stringlist.py index 2f376273..af892096 100644 --- a/domdf_python_tools/stringlist.py +++ b/domdf_python_tools/stringlist.py @@ -463,6 +463,18 @@ def with_indent_type(self, indent_type: str = '\t'): finally: self.indent_type = original_indent_type + def splitlines(self, keepends: bool = False) -> List[str]: + """ + Analagous to :meth:`str.splitlines`. + + .. versionadded:: 3.8.0 + """ + + if keepends: + return [line + '\n' for line in self] + else: + return self + class DelimitedList(List[_S]): """ diff --git a/tests/test_stringlist.py b/tests/test_stringlist.py index 83af8e98..e3b09ad0 100644 --- a/tests/test_stringlist.py +++ b/tests/test_stringlist.py @@ -1,6 +1,8 @@ # stdlib import pickle +import textwrap from textwrap import dedent +from typing import no_type_check # 3rd party import pytest @@ -558,3 +560,11 @@ def test_splitlines(string, lines): @joinlines_splitlines_param def test_joinlines(string, lines): assert string == joinlines(lines) + + +@no_type_check +def test_textwrap_indent(): + sl = StringList(['', '', "hello", "world", '', '', '', "1234"]) + assert textwrap.indent(sl, " ") == "\n\n hello\n world\n\n\n\n 1234\n" + assert textwrap.indent(sl, '\t') == "\n\n\thello\n\tworld\n\n\n\n\t1234\n" + assert textwrap.indent(sl, ">>> ") == "\n\n>>> hello\n>>> world\n\n\n\n>>> 1234\n"