Skip to content

Commit

Permalink
Merge pull request #299 from av-guy/bugfix-colortable-290
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Apr 28, 2024
2 parents 2702d6b + 7d2cf38 commit 0edf8e9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,10 @@ def _stringify_title(self, title, options):
)
bits.append(endpoint)
title = " " * lpad + title + " " * rpad
bits.append(self._justify(title, len(self._hrule) - 2, "c"))
lpad, rpad = self._get_padding_widths(options)
sum_widths = sum([n + lpad + rpad + 1 for n in self._widths])

bits.append(self._justify(title, sum_widths - 1, "c"))
bits.append(endpoint)
lines.append("".join(bits))
return "\n".join(lines)
Expand All @@ -1899,9 +1902,11 @@ def _stringify_header(self, options):
if options["hrules"] in (ALL, FRAME):
bits.append(self._stringify_hrule(options, "top_"))
if options["title"] and options["vrules"] in (ALL, FRAME):
left_j_len = len(self.left_junction_char)
right_j_len = len(self.right_junction_char)
bits[-1] = (
self.left_junction_char
+ bits[-1][1:-1]
+ bits[-1][left_j_len:-right_j_len]
+ self.right_junction_char
)
bits.append("\n")
Expand Down
63 changes: 62 additions & 1 deletion tests/test_colortable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from prettytable import PrettyTable
from prettytable.colortable import RESET_CODE, ColorTable, Theme
from prettytable.colortable import RESET_CODE, ColorTable, Theme, Themes


@pytest.fixture
Expand Down Expand Up @@ -94,3 +94,64 @@ def test_stripped(self) -> None:
def test_multiple(self) -> None:
assert Theme.format_code("30;42") == "\x1b[30;42m"
assert Theme.format_code("\x1b[30;42m") == "\x1b[30;42m"


class TestColorTableRendering:
"""Tests for the rendering of the color table
Methods
-------
test_color_table_rendering
Tests the color table rendering using the default alignment (`'c'`)
"""

def test_color_table_rendering(self) -> None:
"""Tests the color table rendering using the default alignment (`'c'`)"""
chars = {
"+": "\x1b[36m+\x1b[0m\x1b[96m",
"-": "\x1b[34m-\x1b[0m\x1b[96m",
"|": "\x1b[34m|\x1b[0m\x1b[96m",
" ": " ",
}

plus = chars.get("+")
minus = chars.get("-")
pipe = chars.get("|")
space = chars.get(" ")

# +-----------------------+
# | Efforts |
# +---+---+---+---+---+---+
# | A | B | C | D | E | f |
# +---+---+---+---+---+---+
# | 1 | 2 | 3 | 4 | 5 | 6 |
# +---+---+---+---+---+---+

header = (
plus + minus * 23 + plus,
pipe + space * 8 + "Efforts" + space * 8 + pipe,
(plus + minus * 3) * 6 + plus,
)

body = (
"".join(pipe + space + char + space for char in "ABCDEF") + pipe,
(plus + minus * 3) * 6 + plus,
"".join(pipe + space + char + space for char in "123456") + pipe,
(plus + minus * 3) * 6 + plus,
)

header_str = str("\n".join(header))
body_str = str("\n".join(body))

table = ColorTable(
("A", "B", "C", "D", "E", "F"),
theme=Themes.OCEAN,
)

table.title = "Efforts"
table.add_row([1, 2, 3, 4, 5, 6])

expected = header_str + "\n" + body_str + "\x1b[0m"
result = str(table)

assert expected == result

0 comments on commit 0edf8e9

Please sign in to comment.