Skip to content

Commit 65bd387

Browse files
committed
docstrings and comments
1 parent bfb9be2 commit 65bd387

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

src/textual/command.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,10 @@ async def _gather_commands(self, search_value: str) -> None:
10001000
# list of commands that have been gathered so far.
10011001

10021002
def build_prompt() -> Iterable[Content]:
1003+
"""Generator for prompt content."""
10031004
assert hit is not None
10041005
yield Content.from_rich_text(hit.prompt)
1006+
# Optional help text
10051007
if hit.help:
10061008
help_style = VisualStyle.from_styles(
10071009
self.get_component_styles("command-palette--help-text")

src/textual/drivers/linux_driver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def on_terminal_resize(signum, stack) -> None:
269269
self.write("\x1b[>1u") # https://sw.kovidgoyal.net/kitty/keyboard-protocol/
270270
# Disambiguate escape codes https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
271271
self.write("\x1b[=1;u")
272+
272273
self.flush()
273274
self._key_thread = Thread(target=self._run_input_thread)
274275
send_size_event()
@@ -366,6 +367,7 @@ def stop_application_mode(self) -> None:
366367
# Disable the Kitty keyboard protocol. This must be done before leaving
367368
# the alt screen. https://sw.kovidgoyal.net/kitty/keyboard-protocol/
368369
self.write("\x1b[<u")
370+
369371
# Alt screen false, show cursor
370372
self.write("\x1b[?1049l")
371373
self.write("\x1b[?25h")

src/textual/visual.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def is_visual(obj: object) -> bool:
3636
return isinstance(obj, Visual) or hasattr(obj, "textualize")
3737

3838

39+
# Note: not runtime checkable currently, as I've found that to be slow
3940
class SupportsTextualize(Protocol):
4041
"""An object that supports the textualize protocol."""
4142

@@ -52,6 +53,9 @@ class VisualError(Exception):
5253
def visualize(widget: Widget, obj: object) -> Visual:
5354
"""Get a visual instance from an object.
5455
56+
If the object does not support the Visual protocol and is a Rich renderable, it
57+
will be wrapped in a [RichVisual][textual.visual.RichVisual].
58+
5559
Args:
5660
obj: An object.
5761
@@ -87,7 +91,7 @@ def visualize(widget: Widget, obj: object) -> Visual:
8791
@rich.repr.auto
8892
@dataclass(frozen=True)
8993
class Style:
90-
"""Represent a content style (color and other attributes)."""
94+
"""Represents a style in the Visual interface (color and other attributes)."""
9195

9296
background: Color = TRANSPARENT
9397
foreground: Color = TRANSPARENT
@@ -128,6 +132,14 @@ def __add__(self, other: object) -> Style:
128132

129133
@classmethod
130134
def from_rich_style(cls, rich_style: RichStyle) -> Style:
135+
"""Build a Style from a (Rich) Style.
136+
137+
Args:
138+
rich_style: A Rich Style object.
139+
140+
Returns:
141+
New Style.
142+
"""
131143
return Style(
132144
Color.from_rich_color(rich_style.bgcolor),
133145
Color.from_rich_color(rich_style.color),
@@ -140,6 +152,12 @@ def from_rich_style(cls, rich_style: RichStyle) -> Style:
140152

141153
@classmethod
142154
def from_styles(cls, styles: StylesBase) -> Style:
155+
"""Create a Visual Style from a Textual styles object.
156+
157+
Args:
158+
styles: A Styles object, such as `my_widget.styles`.
159+
160+
"""
143161
text_style = styles.text_style
144162
return Style(
145163
styles.background,
@@ -158,6 +176,11 @@ def from_styles(cls, styles: StylesBase) -> Style:
158176

159177
@cached_property
160178
def rich_style(self) -> RichStyle:
179+
"""Convert this Styles in to a Rich style.
180+
181+
Returns:
182+
A Rich style object.
183+
"""
161184
return RichStyle(
162185
color=(self.background + self.foreground).rich_color,
163186
bgcolor=self.background.rich_color,
@@ -209,18 +232,19 @@ def render_strips(
209232
Args:
210233
base_style: The base style.
211234
width: Width of desired render.
212-
height: Height of desired render.
235+
height: Height of desired render or `None` for any height.
236+
style: A Visual Style.
213237
214238
Returns:
215-
An iterable of Strips.
239+
An list of Strips.
216240
"""
217241

218242
@abstractmethod
219243
def get_optimal_width(self, container_width: int) -> int:
220244
"""Get ideal width of the renderable to display its content.
221245
222246
Args:
223-
tab_size: Size of tabs.
247+
container_size: The size of the container.
224248
225249
Returns:
226250
A width in cells.
@@ -253,13 +277,13 @@ def to_strips(
253277
widget: Widget that produced the visual.
254278
visual: A Visual instance.
255279
width: Desired width (in cells).
256-
height: Desired height (in lines).
280+
height: Desired height (in lines) or `None` for no limit.
257281
style: A (Visual) Style instance.
258-
pad: Pad to desired height?
282+
pad: Pad to desired width?
259283
align: Tuple of horizontal and vertical alignment.
260284
261285
Returns:
262-
_type_: _description_
286+
A list of Strips containing the render.
263287
"""
264288
strips = visual.render_strips(widget, width, height, style)
265289
if height is None:
@@ -285,7 +309,15 @@ def to_strips(
285309

286310
@rich.repr.auto
287311
class RichVisual(Visual):
312+
"""A Visual to wrap a Rich renderable."""
313+
288314
def __init__(self, widget: Widget, renderable: RenderableType) -> None:
315+
"""
316+
317+
Args:
318+
widget: The associated Widget.
319+
renderable: A Rich renderable.
320+
"""
289321
self._widget = widget
290322
self._renderable = renderable
291323
self._measurement: Measurement | None = None
@@ -295,13 +327,12 @@ def __rich_repr__(self) -> rich.repr.Result:
295327
yield self._renderable
296328

297329
def _measure(self, console: Console, options: ConsoleOptions) -> Measurement:
298-
return Measurement.get(
299-
console,
300-
options,
301-
self._widget.post_render(self._renderable, RichStyle.null()),
302-
)
303330
if self._measurement is None:
304-
self._measurement = Measurement.get(console, options, self._renderable)
331+
self._measurement = Measurement.get(
332+
console,
333+
options,
334+
self._widget.post_render(self._renderable, RichStyle.null()),
335+
)
305336
return self._measurement
306337

307338
def get_optimal_width(self, container_width: int) -> int:
@@ -363,7 +394,15 @@ def render_strips(
363394

364395
@rich.repr.auto
365396
class Padding(Visual):
397+
"""A Visual to pad another visual."""
398+
366399
def __init__(self, visual: Visual, spacing: Spacing):
400+
"""
401+
402+
Args:
403+
Visual: A Visual.
404+
spacing: A Spacing object containing desired padding dimensions.
405+
"""
367406
self._visual = visual
368407
self._spacing = spacing
369408

0 commit comments

Comments
 (0)