Skip to content

Commit b11650f

Browse files
authored
🔀 Merge pull request #17 from davep/resize-via-clear
Allow resizing the canvas when clearing it
2 parents 59bf752 + f0b0395 commit b11650f

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# textual-canvas ChangeLog
22

3+
## Unreleased
4+
5+
**Released: WiP**
6+
7+
- Added `width` and `height` parameters to `Canvas.clear`.
8+
([#17](https://github.com/davep/textual-canvas/pull/17))
9+
310
## v0.3.0
411

512
**Released: 2025-04-15**

src/textual_canvas/canvas.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ def __init__(
8282
"""The background colour of the canvas itself."""
8383
self._pen_colour = pen_color
8484
"""The default pen colour, used when drawing pixels."""
85-
self._canvas: list[list[Color | None]] = self._blank_canvas
85+
self._canvas: list[list[Color | None]] = []
8686
"""The canvas itself."""
8787
self._refreshing = True
88-
"""Are we refreshing by default?"""
89-
self.virtual_size = Size(width, ceil(height / 2))
88+
"""The current default refresh state."""
89+
self.clear()
9090

9191
@property
9292
def _blank_canvas(self) -> list[list[Color | None]]:
@@ -169,24 +169,36 @@ def _pixel_check(self, x: int, y: int) -> None:
169169
f"x={x}, x={y} is not within 0, 0, {self._width}, {self._height}"
170170
)
171171

172-
def clear(self, color: Color | None = None) -> Self:
172+
def clear(
173+
self,
174+
color: Color | None = None,
175+
width: int | None = None,
176+
height: int | None = None,
177+
) -> Self:
173178
"""Clear the canvas.
174179
175180
Args:
176181
color: Optional default colour for the canvas.
182+
width: Optional width for the canvas.
183+
height: Optional height for the canvas.
177184
178185
Returns:
179186
The canvas.
180187
181-
Note:
182-
If the color isn't provided, then the color used when first
183-
making the canvas is used, this in turn becomes the new default
184-
color (and will then be used for subsequent clears, unless
185-
another color is provided).
188+
If the color isn't provided, then the color used when first making
189+
the canvas is used, this in turn becomes the new default color (and
190+
will then be used for subsequent clears, unless another color is
191+
provided).
192+
193+
Explicitly setting the colour to [`None`][None] will set the canvas
194+
colour to whatever the widget's `background` colour is.
186195
187-
Explicitly setting the colour to [`None`][None] will set the
188-
canvas colour to whatever the widget's `background` colour is.
196+
If `width` or `height` are omitted then the current value for those
197+
dimensions will be used.
189198
"""
199+
self._width = self._width if width is None else width
200+
self._height = self._height if height is None else height
201+
self.virtual_size = Size(self._width, ceil(self._height / 2))
190202
self._canvas_colour = color or self._canvas_colour
191203
self._canvas = self._blank_canvas
192204
return self.refresh()

0 commit comments

Comments
 (0)