Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DigitDisplay widget #2995

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added the ability to run async methods as thread workers https://github.com/Textualize/textual/pull/2938
- Added `App.stop_animation` https://github.com/Textualize/textual/issues/2786
- Added `Widget.stop_animation` https://github.com/Textualize/textual/issues/2786
- Added DigitDisplay widget https://github.com/Textualize/textual/pull/2995

### Changed

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unit-test:

.PHONY: test-snapshot-update
test-snapshot-update:
$(run) pytest --cov-report term-missing --cov=textual tests/ -vv --snapshot-update
$(run) pytest --cov-report term-missing --cov=textual tests/ -vv --snapshot-update $(PYTEST_ARGS)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: I took the liberty to add this snippet, which let you do things like: make PYTEST_ARGS="-k digit_display" test-snapshot-update while developing.
Let me know if you prefer that I remove this.


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


class MyApp(App):
BINDINGS = []

def compose(self) -> ComposeResult:
yield Label("Digits: 0123456789")
yield DigitDisplay("0123456789")

punctuation = " .+,XYZ^*/-="
yield Label("Punctuation: " + punctuation)
yield DigitDisplay(punctuation)

equation = "x = y^2 + 3.14159*y + 10"
yield Label("Equation: " + equation)
yield DigitDisplay(equation)


if __name__ == "__main__":
app = MyApp()
app.run()
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
71 changes: 71 additions & 0 deletions docs/widgets/digit_display.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# DigitDisplay

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

- [ ] Focusable
- [ ] Container

## Examples
rodrigogiraoserrao marked this conversation as resolved.
Show resolved Hide resolved


### Static display example

=== "Screenshot"

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

=== "digit_display.py"

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


### Reacting to an input

=== "Screenshot"

```{.textual path="docs/examples/widgets/digit_display_reacting.py" press="1,2,3"}
```

=== "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
2 changes: 2 additions & 0 deletions src/textual/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
from ._tooltip import Tooltip
from ._tree import Tree
from ._welcome import Welcome
from ._digit_display import DigitDisplay


__all__ = [
"Button",
"Checkbox",
"ContentSwitcher",
"DataTable",
"DigitDisplay",
"DirectoryTree",
"Footer",
"Header",
Expand Down
Loading