Skip to content

Commit

Permalink
Fix not to disable ANSI escape sequences other than colorizing when c…
Browse files Browse the repository at this point in the history
…olorize_terminal is False
  • Loading branch information
thombashi committed Jul 26, 2020
1 parent 5ac77af commit db4e62d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions 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 or not self._writer.enable_ansi_escape:
if not self._writer.enable_ansi_escape:
return value

ansi_styles = []
Expand All @@ -85,7 +85,10 @@ def apply_terminal_style(self, value: str, style: Style) -> str:
if style.font_weight == FontWeight.BOLD:
ansi_styles.append("bold")

return tcolor(value, color=style.color, bg_color=style.bg_color, styles=ansi_styles)
if self._writer.colorize_terminal:
return tcolor(value, color=style.color, bg_color=style.bg_color, styles=ansi_styles)

return tcolor(value, styles=ansi_styles)

def __get_align_format(self, style: Style) -> str:
align_char = get_align_char(style.align)
Expand Down
2 changes: 1 addition & 1 deletion pytablewriter/writer/text/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self) -> None:
self._quoting_flags = copy.deepcopy(dataproperty.NOT_QUOTING_FLAGS)
self._table_tag = None # type: Any

self.colorize_terminal = False
self.enable_ansi_escape = False

def write_table(self, **kwargs) -> None:
"""
Expand Down
8 changes: 7 additions & 1 deletion test/writer/text/test_markdown_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ def test_normal_enable_ansi_escape(self):
]
writer.headers = ["w/ bold", "w/ line through"]
writer.value_matrix = [["hoge", "foo"]]
writer.colorize_terminal = True

writer.enable_ansi_escape = True
out = writer.dumps()
Expand All @@ -834,6 +835,11 @@ def test_normal_enable_ansi_escape(self):
out = writer.dumps()
assert regexp_ansi_escape.search(out) is None

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

def test_normal_margin_1(self, capsys):
writer = table_writer_class()
writer.from_tabledata(TableData("", headers, value_matrix))
Expand Down Expand Up @@ -898,7 +904,7 @@ def test_normal_register_trans_func(self):

def test_normal_flavor(self):
writer = table_writer_class()
writer.colorize_terminal = False
writer.enable_ansi_escape = False
writer.column_styles = [
None,
Style(decoration_line="strike"),
Expand Down

0 comments on commit db4e62d

Please sign in to comment.