Skip to content

Commit

Permalink
add documentation and examples DigitDisplay widget
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdorneles committed Jul 23, 2023
1 parent 0cb6a15 commit d62daec
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Added App.begin_capture_print, App.end_capture_print, Widget.begin_capture_print, Widget.end_capture_print https://github.com/Textualize/textual/issues/2952

### Added

- Added the ability to run async methods as thread workers https://github.com/Textualize/textual/pull/2938
- Added DigitDisplay widget https://github.com/Textualize/textual/pull/2995

### Changed

Expand Down
20 changes: 20 additions & 0 deletions docs/examples/widgets/digit_display_reacting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from textual.app import App, ComposeResult
from textual.widgets import Input
from textual.widgets import DigitDisplay


class MyApp(App):
BINDINGS = []

def compose(self) -> ComposeResult:
yield Input(placeholder="Type something:")
yield DigitDisplay("")

def on_input_changed(self, event: Input.Changed) -> None:
display: DigitDisplay = self.query_one(DigitDisplay)
display.digits = "".join(d for d in event.value if d in DigitDisplay.supported_digits)


if __name__ == "__main__":
app = MyApp()
app.run()
9 changes: 9 additions & 0 deletions docs/widget_gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ A powerful data table, with configurable cursors.
```{.textual path="docs/examples/widgets/data_table.py"}
```

## DigitDisplay

A widget to display digits and basic arithmetic operators using Unicode blocks.

[DigitDisplay reference](./widgets/digit_display.md){ .md-button .md-button--primary }

```{.textual path="docs/examples/widgets/digit_display.py"}
```

## DirectoryTree

A tree view of files and folders.
Expand Down
65 changes: 65 additions & 0 deletions docs/widgets/digit_display.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# DigitDisplay

A widget to display digits and basic arithmetic operators using Unicode blocks.

- [ ] Focusable
- [ ] Container

## Examples

=== "Static example"

```{.textual path="docs/examples/widgets/digit_display.py"}
```

=== "digit_display.py"

```python
--8<-- "docs/examples/widgets/digit_display.py"
```

=== "Reacting to an input"

```{.textual path="docs/examples/widgets/digit_display_reacting.py"}
```

=== "digit_display_reacting.py"

```python
--8<-- "docs/examples/widgets/digit_display_reacting.py"
```

## Reactive attributes

| Name | Type | Default | Description |
| ------ | ------ | ------- | ---------------------------------------------- |
| `digits` | `str` | `""` | Use this to update the digits to be displayed. |


## Read-only attributes

| Name | Type | Description |
| ------ | ------ | ----------------------------------------- |
| `supported_digits` | `frozenset[str]` | Contains the list of supported digits/characters.

## Messages

This widget sends no messages.


## Bindings

This widget defines no bindings.


## Component classes

This widget provides no component classes.


---


::: textual.widgets.DigitDisplay
options:
heading_level: 2
10 changes: 6 additions & 4 deletions src/textual/widgets/_digit_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@
}


class SingleDigitDisplay(Static):
class _SingleDigitDisplay(Static):
digit = reactive(" ", layout=True)

DEFAULT_CSS = """
SingleDigitDisplay {
_SingleDigitDisplay {
height: 3;
min-width: 2;
max-width: 3;
Expand All @@ -224,6 +224,8 @@ class DigitDisplay(Widget):

digits = reactive("", layout=True)

supported_digits = frozenset(_character_map.keys())

DEFAULT_CSS = """
DigitDisplay {
layout: horizontal;
Expand All @@ -233,15 +235,15 @@ class DigitDisplay(Widget):

def __init__(self, initial_value="", **kwargs):
super().__init__(**kwargs)
self._displays = [SingleDigitDisplay(d) for d in initial_value]
self._displays = [_SingleDigitDisplay(d) for d in initial_value]
self.digits = initial_value

def compose(self) -> ComposeResult:
for widget in self._displays:
yield widget

def _add_digit_widget(self, digit: str) -> None:
new_widget = SingleDigitDisplay(digit)
new_widget = _SingleDigitDisplay(digit)
self._displays.append(new_widget)
self.mount(new_widget)

Expand Down

0 comments on commit d62daec

Please sign in to comment.