Skip to content

Commit

Permalink
Add splitlines method to StringList for textwrap.indent
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Dec 10, 2023
1 parent e3b2529 commit 4178176
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions domdf_python_tools/stringlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_stringlist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# stdlib
import pickle
import textwrap
from textwrap import dedent
from typing import no_type_check

# 3rd party
import pytest
Expand Down Expand Up @@ -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"

0 comments on commit 4178176

Please sign in to comment.