Skip to content

Commit

Permalink
Fix colorize_terminal to clear preprocess data when the value changed
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jul 26, 2020
1 parent 4abb745 commit f245e1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pytablewriter/writer/_table_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def __init__(self) -> None:
self._style_filters = [] # type: List[StyleFilterFunc]
self._styler = self._create_styler(self)
self.style_filter_kwargs = {} # type: Dict[str, Any]
self.colorize_terminal = True
self.__colorize_terminal = True
self.__enable_ansi_escape = True

self.max_workers = 1
Expand Down Expand Up @@ -489,6 +489,18 @@ def column_styles(self, value: Sequence[Optional[Style]]) -> None:

self.__clear_preprocess()

@property
def colorize_terminal(self) -> bool:
return self.__colorize_terminal

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

self.__colorize_terminal = value
self.__clear_preprocess()

@property
def enable_ansi_escape(self) -> bool:
return self.__enable_ansi_escape
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_colorize_terminal(self):
writer = table_writer_class()
writer.column_styles = [
Style(color="red"),
Style(bg_color="white"),
]
writer.headers = ["fg color", "bg color"]
writer.value_matrix = [["hoge", "foo"]]

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

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

def test_normal_enable_ansi_escape(self):
writer = table_writer_class()
writer.column_styles = [
Expand Down

0 comments on commit f245e1a

Please sign in to comment.