Skip to content

Commit

Permalink
Add enable_ansi_escape attribute to writer classes: #30
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jul 26, 2020
1 parent 93a85b7 commit 0c4a4b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pytablewriter/style/_styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_font_size(self, style: Style) -> Optional[str]:

class TextStyler(AbstractStyler):
def apply_terminal_style(self, value: str, style: Style) -> str:
if not self._writer.colorize_terminal:
if not self._writer.colorize_terminal or not self._writer.enable_ansi_escape:
return value

ansi_styles = []
Expand Down
13 changes: 13 additions & 0 deletions pytablewriter/writer/_table_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def __init__(self) -> None:
self._styler = self._create_styler(self)
self.style_filter_kwargs = {} # type: Dict[str, Any]
self.colorize_terminal = True
self.__enable_ansi_escape = True

self.max_workers = 1

Expand Down Expand Up @@ -486,6 +487,18 @@ def column_styles(self, value: Sequence[Optional[Style]]) -> None:

self.__clear_preprocess()

@property
def enable_ansi_escape(self) -> bool:
return self.__enable_ansi_escape

@enable_ansi_escape.setter
def enable_ansi_escape(self, value: bool) -> None:
if self.__enable_ansi_escape == value:
return

self.__enable_ansi_escape = value
self.__clear_preprocess()

def add_style_filter(self, style_filter: StyleFilterFunc) -> None:
"""Add a style filter function.
Expand Down
17 changes: 17 additions & 0 deletions test/writer/text/test_markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,23 @@ def test_normal_ansi_style(self):
assert regexp_ansi_escape.search(out)
assert regexp_ansi_escape.sub("", out) == expected

def test_normal_enable_ansi_escape(self):
writer = table_writer_class()
writer.column_styles = [
Style(font_weight="bold"),
Style(decoration_line="line-through"),
]
writer.headers = ["w/ bold", "w/ line through"]
writer.value_matrix = [["hoge", "foo"]]

writer.enable_ansi_escape = True
out = writer.dumps()
assert regexp_ansi_escape.search(out)

writer.enable_ansi_escape = False
out = writer.dumps()
assert regexp_ansi_escape.search(out) is None

def test_normal_margin_1(self, capsys):
writer = table_writer_class()
writer.from_tabledata(TableData("", headers, value_matrix))
Expand Down

0 comments on commit 0c4a4b7

Please sign in to comment.