Skip to content

Commit dc1f29d

Browse files
committed
Fix draw frame, update console __str__ and add tests.
Tests using print(console) will now replace the space character instead of leaving blank spaces which will mess up doc tests.
1 parent 0bee024 commit dc1f29d

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

tcod/console.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ class Console:
7979
... dtype=tcod.console.Console.DTYPE,
8080
... order="F",
8181
... )
82-
>>> buffer["ch"] = ord(' ')
82+
>>> buffer["ch"] = ord('_')
8383
>>> buffer["ch"][:, 1] = ord('x')
8484
>>> c = tcod.console.Console(20, 3, order="F", buffer=buffer)
8585
>>> print(c)
86-
< |
87-
|xxxxxxxxxxxxxxxxxxxx|
88-
| >
86+
<____________________
87+
xxxxxxxxxxxxxxxxxxxx
88+
____________________>
8989

9090
.. versionadded:: 8.5
9191

@@ -920,7 +920,7 @@ def __repr__(self) -> str:
920920

921921
def __str__(self) -> str:
922922
"""Return a simplified representation of this consoles contents."""
923-
return "<%s>" % "|\n|".join("".join(chr(c) for c in line) for line in self._tiles["ch"])
923+
return "<%s>" % "\n ".join("".join(chr(c) for c in line) for line in self._tiles["ch"])
924924

925925
def _pythonic_index(self, x: int, y: int) -> Tuple[int, int]:
926926
if __debug__ and (x < 0 or y < 0):
@@ -1087,6 +1087,26 @@ def draw_frame(
10871087

10881088
.. versionchanged:: 12.6
10891089
Added `decoration` parameter.
1090+
1091+
Example::
1092+
1093+
>>> console = tcod.Console(12, 6)
1094+
>>> console.draw_frame(x=0, y=0, width=3, height=3)
1095+
>>> console.draw_frame(x=3, y=0, width=3, height=3, decoration="╔═╗║ ║╚═╝")
1096+
>>> console.draw_frame(x=6, y=0, width=3, height=3, decoration="123456789")
1097+
>>> console.draw_frame(x=9, y=0, width=3, height=3, decoration="/-\\| |\\-/")
1098+
>>> console.draw_frame(x=0, y=3, width=12, height=3)
1099+
>>> console.print_box(x=0, y=3, width=12, height=1, string=" Title ", alignment=tcod.CENTER)
1100+
1
1101+
>>> console.print_box(x=0, y=5, width=12, height=1, string="┤Lower├", alignment=tcod.CENTER)
1102+
1
1103+
>>> print(console)
1104+
<┌─┐╔═╗123/-\\
1105+
│ │║ ║456| |
1106+
└─┘╚═╝789\\-/
1107+
┌─ Title ──┐
1108+
│ │
1109+
└─┤Lower├──┘>
10901110
"""
10911111
x, y = self._pythonic_index(x, y)
10921112
if title and decoration != "┌─┐│ │└─┘":
@@ -1121,16 +1141,19 @@ def draw_frame(
11211141
decoration_ = decoration
11221142
if len(decoration_) != 9:
11231143
raise TypeError(f"Decoration must have a length of 9 (len(decoration) is {len(decoration_)}.)")
1124-
lib.TCOD_console_draw_frame_rgb(
1125-
x,
1126-
y,
1127-
width,
1128-
height,
1129-
decoration_,
1130-
(fg,) if fg is not None else ffi.NULL,
1131-
(bg,) if bg is not None else ffi.NULL,
1132-
bg_blend,
1133-
clear,
1144+
_check(
1145+
lib.TCOD_console_draw_frame_rgb(
1146+
self.console_c,
1147+
x,
1148+
y,
1149+
width,
1150+
height,
1151+
decoration_,
1152+
(fg,) if fg is not None else ffi.NULL,
1153+
(bg,) if bg is not None else ffi.NULL,
1154+
bg_blend,
1155+
clear,
1156+
)
11341157
)
11351158

11361159
def draw_rect(

tests/test_console.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ def test_console_repr() -> None:
102102
@pytest.mark.filterwarnings("ignore")
103103
def test_console_str() -> None:
104104
console = tcod.console.Console(10, 2)
105+
console.ch[:] = ord(".")
105106
console.print_(0, 0, "Test")
106-
assert str(console) == ("<Test |\n" "| >")
107+
assert str(console) == ("<Test......\n" " ..........>")
107108

108109

109110
def test_console_fortran_buffer() -> None:
@@ -142,3 +143,15 @@ def test_rexpaint(tmp_path: Path) -> None:
142143
assert consoles[1].rgb.shape == loaded[1].rgb.shape
143144
with pytest.raises(FileNotFoundError):
144145
tcod.console.load_xp(tmp_path / "non_existant")
146+
147+
148+
def test_draw_frame() -> None:
149+
console = tcod.Console(3, 3, order="C")
150+
with pytest.raises(TypeError):
151+
console.draw_frame(0, 0, 3, 3, title="test", decoration="123456789")
152+
with pytest.raises(TypeError):
153+
console.draw_frame(0, 0, 3, 3, decoration="0123456789")
154+
155+
console.draw_frame(0, 0, 3, 3, decoration=(49, 50, 51, 52, 53, 54, 55, 56, 57))
156+
assert console.ch.tolist() == [[49, 50, 51], [52, 53, 54], [55, 56, 57]]
157+
console.draw_frame(0, 0, 3, 3, title="T")

0 commit comments

Comments
 (0)