Skip to content

Commit

Permalink
DigitDisplay: more followup on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdorneles committed Jul 31, 2023
1 parent a519225 commit 982d244
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/textual/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"Checkbox",
"ContentSwitcher",
"DataTable",
"DigitDisplay",
"DirectoryTree",
"Footer",
"Header",
Expand Down Expand Up @@ -77,7 +78,6 @@
"Tooltip",
"Tree",
"Welcome",
"DigitDisplay",
]

_WIDGETS_LAZY_LOADING_CACHE: dict[str, type[Widget]] = {}
Expand Down
102 changes: 84 additions & 18 deletions src/textual/widgets/_digit_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from textual.reactive import reactive
from textual.widget import Widget
from textual.widgets import Static
from ..geometry import Size


_character_map: dict[str, str] = {}

# in the mappings below, we use underscores to make spaces more visible,
# we will strip them out later
_VIRTUAL_SPACE = "·"

# in the mappings below, we use a dot instead of spaces to make them more
# visible, we will strip them out later
_character_map[
"0"
] = """
Expand Down Expand Up @@ -158,9 +161,9 @@
_character_map[
"="
] = """
···
╺━·
╺━·
··
╺━
╺━
"""

_character_map[
Expand Down Expand Up @@ -189,29 +192,59 @@
"""


_VIRTUAL_SPACE = "·"

# here we strip spaces and replace virtual spaces with spaces
_character_map = {
k: v.strip().replace(_VIRTUAL_SPACE, " ") for k, v in _character_map.items()
}


class _SingleDigitDisplay(Static):
"""
A widget to display a single digit or basic arithmetic symbol using Unicode blocks.
"""

digit = reactive(" ", layout=True)
"""The digit to display."""

DEFAULT_CSS = """
_SingleDigitDisplay {
height: 3;
min-width: 2;
max-width: 3;
}
_SingleDigitDisplay {
height: 3;
min-width: 2;
max-width: 3;
}
"""

def __init__(self, initial_value=" ", **kwargs):
super().__init__(**kwargs)
def __init__(
self,
initial_value: str = " ",
expand: bool = False,
shrink: bool = False,
markup: bool = True,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
"""
Create a single digit display widget.
Example:
```py
class Example(App):
def compose(self) -> ComposeResult:
return _SingleDigitDisplay("1")
"""
super().__init__(
expand=expand,
shrink=shrink,
markup=markup,
name=name,
id=id,
classes=classes,
disabled=disabled,
)
self.digit = initial_value
self._content_width = 3

def validate_digit(self, digit: str) -> str:
"""Sanitize and validate the digit input."""
Expand All @@ -224,11 +257,21 @@ def validate_digit(self, digit: str) -> str:

def _watch_digit(self, digit: str) -> None:
"""Called when the digit attribute changes and passes validation."""
self.update(_character_map[digit.upper()])
content = _character_map[digit.upper()]
self._content_width = len(content.splitlines()[0])
self.update(content)

def get_content_width(self, container: Size, viewport: Size) -> int:
return self._content_width

def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return 3


class DigitDisplay(Widget):
"""A widget to display digits and basic arithmetic operators using Unicode blocks."""
"""
A widget to display digits and basic arithmetic symbols using Unicode blocks.
"""

digits = reactive("", layout=True)
"""The digits to display."""
Expand All @@ -243,8 +286,31 @@ class DigitDisplay(Widget):
}
"""

def __init__(self, initial_value="", **kwargs):
super().__init__(**kwargs)
def __init__(
self,
initial_value: str = "",
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
"""
Create a Digit Display widget.
Example:
```py
class Example(App):
def compose(self) -> ComposeResult:
return DigitDisplay("123+456")
Args:
initial_value (str, optional): The initial value to display. Defaults to "".
name: The name of the widget.
id: The ID of the widget in the DOM.
classes: The CSS classes for the widget.
disabled: Whether the widget is disabled or not.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self._displays = [_SingleDigitDisplay(d) for d in initial_value]
self.digits = initial_value

Expand Down
Loading

0 comments on commit 982d244

Please sign in to comment.