Merge branch 'main' of github.com:Textualize/textual into more-testing

This commit is contained in:
Darren Burns
2022-10-24 11:16:56 +01:00
124 changed files with 1989 additions and 3808 deletions

3
.github/FUNDING.yml vendored
View File

@@ -1,4 +1 @@
# These are supported funding model platforms
github: willmcgugan
ko_fi: willmcgugan

View File

@@ -8,15 +8,13 @@ jobs:
permissions:
issues: write
steps:
- name: Did I solve your problem?
- name: Did we solve your problem?
uses: peter-evans/create-or-update-comment@a35cf36e5301d70b76f316e867e7788a55a31dae
with:
issue-number: ${{ github.event.issue.number }}
body: |
Did I solve your problem?
Did we solve your problem?
Consider [sponsoring my work](https://github.com/sponsors/willmcgugan) on Textual with a monthly donation.
Consider buying the Textualize developers a [coffee](https://ko-fi.com/textualize) to say thanks.
Or buy me a [coffee](https://ko-fi.com/willmcgugan) to say thanks.
– [Will McGugan](https://twitter.com/willmcgugan)
– [Textualize](https://twitter.com/textualizeio)

View File

@@ -5,12 +5,35 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - Unreleased
## [0.2.1] - 2022-10-23
### Changed
- Updated meta data for PyPI
## [0.2.0] - 2022-10-23
### Added
- CSS support
- Too numerous to mention
## [0.1.18] - 2022-04-30
### Changed
- Bump typing extensions
## [0.1.17] - 2022-03-10
### Changed
- Bumped Rich dependency
## [0.1.16] - 2022-03-10
### Fixed
- Fixed escape key hanging on Windows
## [0.1.15] - 2022-01-31
@@ -104,3 +127,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Scrollview now shows scrollbars automatically
- New handler system for messages that doesn't require inheritance
- Improved traceback handling
[0.2.1]: https://github.com/Textualize/textual/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/Textualize/textual/compare/v0.1.18...v0.2.0
[0.1.18]: https://github.com/Textualize/textual/compare/v0.1.17...v0.1.18
[0.1.17]: https://github.com/Textualize/textual/compare/v0.1.16...v0.1.17
[0.1.16]: https://github.com/Textualize/textual/compare/v0.1.15...v0.1.16
[0.1.15]: https://github.com/Textualize/textual/compare/v0.1.14...v0.1.15
[0.1.14]: https://github.com/Textualize/textual/compare/v0.1.13...v0.1.14
[0.1.13]: https://github.com/Textualize/textual/compare/v0.1.12...v0.1.13
[0.1.12]: https://github.com/Textualize/textual/compare/v0.1.11...v0.1.12
[0.1.11]: https://github.com/Textualize/textual/compare/v0.1.10...v0.1.11
[0.1.10]: https://github.com/Textualize/textual/compare/v0.1.9...v0.1.10
[0.1.9]: https://github.com/Textualize/textual/compare/v0.1.8...v0.1.9
[0.1.8]: https://github.com/Textualize/textual/compare/v0.1.7...v0.1.8
[0.1.7]: https://github.com/Textualize/textual/releases/tag/v0.1.7

128
CODE_OF_CONDUCT.md Normal file
View File

@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
[will@textualize.io](mailto:will@textualize.io).
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.

111
README.md
View File

@@ -1,21 +1,25 @@
# Textual
![Textual splash image](./imgs/textual.png)
![Textual splash image](https://raw.githubusercontent.com/Textualize/textual/main/imgs/textual.png)
Textual is a Python framework for creating interactive applications that run in your terminal.
<details>
<summary> 🎬 Code browser </summary>
<summary> 🎬 Demonstration </summary>
<hr>
This is the [code_browser.py](./examples/code_browser.py) example which clocks in at 61 lines (*including* docstrings and blank lines).
A quick run through of some Textual features.
https://user-images.githubusercontent.com/554369/197355913-65d3c125-493d-4c05-a590-5311f16c40ff.mov
https://user-images.githubusercontent.com/554369/196156524-5edea78c-1226-4103-91f3-e82d6a52bd2b.mov
</details>
## About
Textual adds interactivity to [Rich](https://github.com/Textualize/rich) with a Python API inspired by modern web development.
@@ -31,10 +35,65 @@ Textual runs on Linux, macOS, and Windows. Textual requires Python 3.7 or above.
Install Textual via pip:
```
pip install textual[dev]
pip install "textual[dev]"
```
The addition of `[dev]` installs Textual development tools.
The addition of `[dev]` installs Textual development tools. See the [docs](https://textual.textualize.io/getting_started/) if you need help getting started.
## Demo
Run the following command to see a little of what Textual can do:
```
python -m textual
```
![Textual demo](https://raw.githubusercontent.com/Textualize/textual/main/imgs/demo.png)
## Documentation
Head over to the [Textual documentation](http://textual.textualize.io/) to start building!
## Examples
The Textual repository comes with a number of examples you can experiment with or use as a template for your own projects.
<details>
<summary> 🎬 Code browser </summary>
<hr>
This is the [code_browser.py](https://github.com/Textualize/textual/blob/main/examples/code_browser.py) example which clocks in at 61 lines (*including* docstrings and blank lines).
https://user-images.githubusercontent.com/554369/197188237-88d3f7e4-4e5f-40b5-b996-c47b19ee2f49.mov
</details>
<details>
<summary> 📷 Calculator </summary>
<hr>
This is [calculator.py](https://github.com/Textualize/textual/blob/main/examples/calculator.py) which demonstrates Textual grid layouts.
![calculator screenshot](https://raw.githubusercontent.com/Textualize/textual/main/imgs/calculator.png)
</details>
<details>
<summary> 🎬 Stopwatch </summary>
<hr>
This is the Stopwatch example from the [tutorial](https://textual.textualize.io/tutorial/).
https://user-images.githubusercontent.com/554369/197360718-0c834ef5-6285-4d37-85cf-23eed4aa56c5.mov
</details>
## Reference commands
@@ -71,45 +130,25 @@ textual borders
https://user-images.githubusercontent.com/554369/196158235-4b45fb78-053d-4fd5-b285-e09b4f1c67a8.mov
</details>
## Examples
The Textual repository comes with a number of examples you can experiment with or use as a template for your own projects.
<details>
<summary> 📷 Calculator </summary>
<hr>
This is [calculator.py](./examples/calculator.py) which demonstrates Textual grid layouts.
![calculator screenshot](./imgs/calculator.svg)
</details>
<details>
<summary> 📷 Code browser </summary>
<hr>
This is [code_browser.py](./examples/code_browser.py) which demonstrates the directory tree widget.
![code browser screenshot](./imgs/codebrowser.svg)
</details>
<details>
<summary> 📷 Stopwatch </summary>
<summary> 🎬 Colors reference </summary>
<hr>
This is the Stopwatch example from the tutorial.
This is a reference for Textual's color design system.
### Light theme
```bash
textual colors
```
https://user-images.githubusercontent.com/554369/197357417-2d407aac-8969-44d3-8250-eea45df79d57.mov
![stopwatch light screenshot](./imgs/stopwatch_light.svg)
### Dark theme
![stopwatch dark screenshot](./imgs/stopwatch_dark.svg)
</details>

View File

@@ -2,4 +2,7 @@
{% block extrahead %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/fira_code.min.css" integrity="sha512-MbysAYimH1hH2xYzkkMHB6MqxBqfP0megxsCLknbYqHVwXTCg9IqHbk+ZP/vnhO8UEW6PaXAkKe2vQ+SWACxxA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Fathom - beautiful, simple website analytics -->
<script src="https://cdn.usefathom.com/script.js" data-site="TAUKXRLQ" defer></script>
<!-- / Fathom -->
{% endblock %}

View File

@@ -25,18 +25,59 @@ You can install Textual via PyPI.
If you plan on developing Textual apps, then you should install `textual[dev]`. The `[dev]` part installs a few extra dependencies for development.
```
pip install "textual[dev]==0.2.0b7"
pip install "textual[dev]"
```
If you only plan on _running_ Textual apps, then you can drop the `[dev]` part:
```
pip install textual==0.2.0b7
pip install textual
```
!!! important
## Demo
Once you have Textual installed, run the following to get an impression of what it can do:
```bash
python -m textual
```
If Textual is installed you should see the following:
```{.textual path="src/textual/demo.py" columns="127" lines="53" press="enter,_,_,_,_,_,_,tab,_,w,i,l,l"}
```
## Examples
The Textual repository comes with a number of example apps. To try out the examples, first clone the Textual repository:
=== "HTTPS"
```bash
git clone https://github.com/Textualize/textual.git
```
=== "SSH"
```bash
git clone git@github.com:Textualize/textual.git
```
=== "GitHub CLI"
```bash
gh repo clone Textualize/textual
```
With the repository cloned, navigate to the `/examples/` directory where you fill find a number of Python files you can run from the command line:
```bash
cd textual/examples/
python code_browser.py ../
```
There may be a more recent beta version since the time of writing. Check the [release history](https://pypi.org/project/textual/#history) for a more recent version.
## Textual CLI

View File

@@ -1 +0,0 @@
# Animation

View File

@@ -1,3 +0,0 @@
# How to ...
For those who want more focused information on Textual features.

View File

@@ -1 +0,0 @@
# Mouse and Keyboard

View File

@@ -1 +0,0 @@
# Scroll

View File

@@ -8,13 +8,21 @@ We have many new features in the pipeline. This page will keep track of that wor
High-level features we plan on implementing.
- [ ] Accessibility
* [ ] Integration with screen readers
* [x] Monochrome mode
* [ ] High contrast theme
* [ ] Color blind themes
- [ ] Command interface
* [ ] Command menu
* [ ] Fuzzy search
- [ ] Configuration (.toml based extensible configuration format)
- [x] Devtools
* [ ] Browser-inspired devtools interface with integrated DOM view, log, and REPL
- [ ] Reactive state
- [x] Console
- [ ] Devtools
* [ ] Integrated log
* [ ] DOM tree view
* [ ] REPL
- [ ] Reactive state abstraction
- [x] Themes
* [ ] Customize via config
* [ ] Builtin theme editor
@@ -40,25 +48,26 @@ Widgets are key to making user friendly interfaces. The builtin widgets should c
* [ ] Export to `attrs` objects
* [ ] Export to `PyDantic` objects
- [ ] Image support
- [ ] Half block
- [ ] Braile
- [ ] Sixels, and other image extensions
* [ ] Half block
* [ ] Braille
* [ ] Sixels, and other image extensions
- [x] Input
* [ ] Validation
* [ ] Error / warning states
* [ ] Template types: IP address, physical units (weight, volume), currency, credit card etc
- [ ] Markdown viewer (more dynamic than Rich markdown, with scrollable code areas / collapseable sections)
- [ ] Markdown viewer (more dynamic than Rich markdown, with scrollable code areas / collapsible sections)
- [ ] Plots
- [ ] bar chart
- [ ] line chart
- [ ] Candlestick chars
* [ ] bar chart
* [ ] line chart
* [ ] Candlestick chars
- [ ] Progress bars
* [ ] Style variants (solid, thin etc)
- [ ] Radio boxes
- [ ] Sparklines
- [ ] Spark-lines
- [ ] Tabs
- [ ] TextArea (multi-line input)
* [ ] Basic controls
* [ ] Syntax highlighting
* [ ] Indentation guides
* [ ] Smart features for various languages
* [ ] Syntax highlighting

View File

@@ -35,13 +35,13 @@ If you want to try the finished Stopwatch app and follow along with the code, fi
=== "HTTPS"
```bash
git clone -b css https://github.com/Textualize/textual.git
git clone https://github.com/Textualize/textual.git
```
=== "SSH"
```bash
git clone -b css git@github.com:Textualize/textual.git
git clone git@github.com:Textualize/textual.git
```
=== "GitHub CLI"
@@ -222,7 +222,7 @@ If we run the app now, it will look *very* different.
```{.textual path="docs/examples/tutorial/stopwatch03.py" title="stopwatch03.py"}
```
This app looks much more like our sketch. Let's look at how the Textual uses `stopwatch03.css` to apply styles.
This app looks much more like our sketch. Let's look at how Textual uses `stopwatch03.css` to apply styles.
### CSS basics
@@ -381,14 +381,15 @@ We've seen how we can update widgets with a timer, but we still need to wire up
We need to be able to start, stop, and reset each stopwatch independently. We can do this by adding a few more methods to the `TimeDisplay` class.
```python title="stopwatch06.py" hl_lines="14 18 30-44 50-61"
```python title="stopwatch06.py" hl_lines="14 18 22 30-44 50-61"
--8<-- "docs/examples/tutorial/stopwatch06.py"
```
Here's a summary of the changes made to `TimeDisplay`.
- We've added a `total` reactive attribute to store the total time elapsed between clicking that start and stop buttons.
- We've added a `total` reactive attribute to store the total time elapsed between clicking the start and stop buttons.
- The call to `set_interval` has grown a `pause=True` argument which starts the timer in pause mode (when a timer is paused it won't run until [resume()][textual.timer.Timer.resume] is called). This is because we don't want the time to update until the user hits the start button.
- The `update_time` method now adds `total` to the current time to account for the time between any previous clicks of the start and stop buttons.
- We've stored the result of `set_interval` which returns a Timer object. We will use this later to _resume_ the timer when we start the Stopwatch.
- We've added `start()`, `stop()`, and `reset()` methods.

View File

@@ -3,6 +3,7 @@ from decimal import Decimal
from textual.app import App, ComposeResult
from textual import events
from textual.containers import Container
from textual.css.query import NoMatches
from textual.reactive import var
from textual.widgets import Button, Static
@@ -19,15 +20,15 @@ class CalculatorApp(App):
value = var("")
operator = var("plus")
KEY_MAP = {
"+": "plus",
"-": "minus",
".": "point",
"*": "multiply",
"/": "divide",
"_": "plus-minus",
"%": "percent",
"=": "equals",
NAME_MAP = {
"asterisk": "multiply",
"slash": "divide",
"underscore": "plus-minus",
"full_stop": "point",
"plus_minus_sign": "plus-minus",
"percent_sign": "percent",
"equals_sign": "equals",
"enter": "equals",
}
def watch_numbers(self, value: str) -> None:
@@ -75,7 +76,10 @@ class CalculatorApp(App):
"""Called when the user presses a key."""
def press(button_id: str) -> None:
self.query_one(f"#{button_id}", Button).press()
try:
self.query_one(f"#{button_id}", Button).press()
except NoMatches:
pass
self.set_focus(None)
key = event.key
@@ -84,8 +88,8 @@ class CalculatorApp(App):
elif key == "c":
press("c")
press("ac")
elif key in self.KEY_MAP:
press(self.KEY_MAP[key])
else:
press(self.NAME_MAP.get(key, key))
def on_button_pressed(self, event: Button.Pressed) -> None:
"""Called when a button is pressed."""

88
examples/five_by_five.css Normal file
View File

@@ -0,0 +1,88 @@
$animation-type: linear;
$animatin-speed: 175ms;
Game {
align: center middle;
layers: gameplay messages;
}
GameGrid {
layout: grid;
grid-size: 5 5;
layer: gameplay;
}
GameHeader {
background: $primary-background;
color: $text;
height: 1;
dock: top;
layer: gameplay;
}
GameHeader #app-title {
width: 60%;
}
GameHeader #moves {
width: 20%;
}
GameHeader #progress {
width: 20%;
}
Footer {
height: 1;
dock: bottom;
layer: gameplay;
}
GameCell {
width: 100%;
height: 100%;
background: $surface;
border: round $surface-darken-1;
transition: background $animatin-speed $animation-type, color $animatin-speed $animation-type;
}
GameCell:hover {
background: $panel-lighten-1;
border: round $panel;
}
GameCell.filled {
background: $secondary;
border: round $secondary-darken-1;
}
GameCell.filled:hover {
background: $secondary-lighten-1;
border: round $secondary;
}
WinnerMessage {
width: 50%;
height: 25%;
layer: messages;
visibility: hidden;
content-align: center middle;
text-align: center;
background: $success;
color: $text;
border: round;
padding: 2;
}
.visible {
visibility: visible;
}
Help {
background: $primary;
color: $text;
border: round $primary-lighten-3;
padding: 2;
}
/* five_by_five.css ends here */

17
examples/five_by_five.md Normal file
View File

@@ -0,0 +1,17 @@
# 5x5
## Introduction
An annoying puzzle for the terminal, built with
[Textual](https://www.textualize.io/).
## Objective
The object of the game is to fill all of the squares. When you click on a
square, it, and the squares above, below and to the sides will be toggled.
It is possible to solve the puzzle in as few as 14 moves.
Good luck!
[//]: # (README.md ends here)

329
examples/five_by_five.py Normal file
View File

@@ -0,0 +1,329 @@
"""Simple version of 5x5, developed for/with Textual."""
from pathlib import Path
from typing import cast
import sys
if sys.version_info >= (3, 8):
from typing import Final
else:
from typing_extensions import Final
from textual.containers import Horizontal
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widget import Widget
from textual.widgets import Footer, Button, Static
from textual.css.query import DOMQuery
from textual.reactive import reactive
from textual.binding import Binding
from rich.markdown import Markdown
class Help(Screen):
"""The help screen for the application."""
#: Bindings for the help screen.
BINDINGS = [("escape,space,q,question_mark", "pop_screen", "Close")]
def compose(self) -> ComposeResult:
"""Compose the game's help.
Returns:
ComposeResult: The result of composing the help screen.
"""
yield Static(Markdown(Path(__file__).with_suffix(".md").read_text()))
class WinnerMessage(Static):
"""Widget to tell the user they have won."""
#: The minimum number of moves you can solve the puzzle in.
MIN_MOVES: Final = 14
@staticmethod
def _plural(value: int) -> str:
return "" if value == 1 else "s"
def show(self, moves: int) -> None:
"""Show the winner message.
Args:
moves (int): The number of moves required to win.
"""
self.update(
"W I N N E R !\n\n\n"
f"You solved the puzzle in {moves} move{self._plural(moves)}."
+ (
(
f" It is possible to solve the puzzle in {self.MIN_MOVES}, "
f"you were {moves - self.MIN_MOVES} move{self._plural(moves - self.MIN_MOVES)} over."
)
if moves > self.MIN_MOVES
else " Well done! That's the minimum number of moves to solve the puzzle!"
)
)
self.add_class("visible")
def hide(self) -> None:
"""Hide the winner message."""
self.remove_class("visible")
class GameHeader(Widget):
"""Header for the game.
Comprises of the title (``#app-title``), the number of moves ``#moves``
and the count of how many cells are turned on (``#progress``).
"""
#: Keep track of how many moves the player has made.
moves = reactive(0)
#: Keep track of how many cells are filled.
filled = reactive(0)
def compose(self) -> ComposeResult:
"""Compose the game header.
Returns:
ComposeResult: The result of composing the game header.
"""
yield Horizontal(
Static(self.app.title, id="app-title"),
Static(id="moves"),
Static(id="progress"),
)
def watch_moves(self, moves: int):
"""Watch the moves reactive and update when it changes.
Args:
moves (int): The number of moves made.
"""
self.query_one("#moves", Static).update(f"Moves: {moves}")
def watch_filled(self, filled: int):
"""Watch the on-count reactive and update when it changes.
Args:
filled (int): The number of cells that are currently on.
"""
self.query_one("#progress", Static).update(f"Filled: {filled}")
class GameCell(Button):
"""Individual playable cell in the game."""
@staticmethod
def at(row: int, col: int) -> str:
"""Get the ID of the cell at the given location.
Args:
row (int): The row of the cell.
col (int): The column of the cell.
Returns:
str: A string ID for the cell.
"""
return f"cell-{row}-{col}"
def __init__(self, row: int, col: int) -> None:
"""Initialise the game cell.
Args:
row (int): The row of the cell.
col (int): The column of the cell.
"""
super().__init__("", id=self.at(row, col))
self.row = row
self.col = col
class GameGrid(Widget):
"""The main playable grid of game cells."""
def compose(self) -> ComposeResult:
"""Compose the game grid.
Returns:
ComposeResult: The result of composing the game grid.
"""
for row in range(Game.SIZE):
for col in range(Game.SIZE):
yield GameCell(row, col)
class Game(Screen):
"""Main 5x5 game grid screen."""
#: The size of the game grid. Clue's in the name really.
SIZE = 5
#: The bindings for the main game grid.
BINDINGS = [
Binding("n", "new_game", "New Game"),
Binding("question_mark", "push_screen('help')", "Help", key_display="?"),
Binding("q", "quit", "Quit"),
Binding("up,w,k", "navigate(-1,0)", "Move Up", False),
Binding("down,s,j", "navigate(1,0)", "Move Down", False),
Binding("left,a,h", "navigate(0,-1)", "Move Left", False),
Binding("right,d,l", "navigate(0,1)", "Move Right", False),
Binding("space", "move", "Toggle", False),
]
@property
def filled_cells(self) -> DOMQuery[GameCell]:
"""DOMQuery[GameCell]: The collection of cells that are currently turned on."""
return cast(DOMQuery[GameCell], self.query("GameCell.filled"))
@property
def filled_count(self) -> int:
"""int: The number of cells that are currently filled."""
return len(self.filled_cells)
@property
def all_filled(self) -> bool:
"""bool: Are all the cells filled?"""
return self.filled_count == self.SIZE * self.SIZE
def game_playable(self, playable: bool) -> None:
"""Mark the game as playable, or not.
Args:
playable (bool): Should the game currently be playable?
"""
for cell in self.query(GameCell):
cell.disabled = not playable
def cell(self, row: int, col: int) -> GameCell:
"""Get the cell at a given location.
Args:
row (int): The row of the cell to get.
col (int): The column of the cell to get.
Returns:
GameCell: The cell at that location.
"""
return self.query_one(f"#{GameCell.at(row,col)}", GameCell)
def compose(self) -> ComposeResult:
"""Compose the game screen.
Returns:
ComposeResult: The result of composing the game screen.
"""
yield GameHeader()
yield GameGrid()
yield Footer()
yield WinnerMessage()
def toggle_cell(self, row: int, col: int) -> None:
"""Toggle an individual cell, but only if it's in bounds.
If the row and column would place the cell out of bounds for the
game grid, this function call is a no-op. That is, it's safe to call
it with an invalid cell coordinate.
Args:
row (int): The row of the cell to toggle.
col (int): The column of the cell to toggle.
"""
if 0 <= row <= (self.SIZE - 1) and 0 <= col <= (self.SIZE - 1):
self.cell(row, col).toggle_class("filled")
_PATTERN: Final = (-1, 1, 0, 0, 0)
def toggle_cells(self, cell: GameCell) -> None:
"""Toggle a 5x5 pattern around the given cell.
Args:
cell (GameCell): The cell to toggle the cells around.
"""
for row, col in zip(self._PATTERN, reversed(self._PATTERN)):
self.toggle_cell(cell.row + row, cell.col + col)
self.query_one(GameHeader).filled = self.filled_count
def make_move_on(self, cell: GameCell) -> None:
"""Make a move on the given cell.
All relevant cells around the given cell are toggled as per the
game's rules.
Args:
cell (GameCell): The cell to make a move on
"""
self.toggle_cells(cell)
self.query_one(GameHeader).moves += 1
if self.all_filled:
self.query_one(WinnerMessage).show(self.query_one(GameHeader).moves)
self.game_playable(False)
def on_button_pressed(self, event: GameCell.Pressed) -> None:
"""React to a press of a button on the game grid.
Args:
event (GameCell.Pressed): The event to react to.
"""
self.make_move_on(cast(GameCell, event.button))
def action_new_game(self) -> None:
"""Start a new game."""
self.query_one(GameHeader).moves = 0
self.filled_cells.remove_class("filled")
self.query_one(WinnerMessage).hide()
middle = self.cell(self.SIZE // 2, self.SIZE // 2)
self.toggle_cells(middle)
self.set_focus(middle)
self.game_playable(True)
def action_navigate(self, row: int, col: int) -> None:
"""Navigate to a new cell by the given offsets.
Args:
row (int): The row of the cell to navigate to.
col (int): The column of the cell to navigate to.
"""
if isinstance(self.focused, GameCell):
self.set_focus(
self.cell(
(self.focused.row + row) % self.SIZE,
(self.focused.col + col) % self.SIZE,
)
)
def action_move(self) -> None:
"""Make a move on the current cell."""
if isinstance(self.focused, GameCell):
self.focused.press()
def on_mount(self) -> None:
"""Get the game started when we first mount."""
self.action_new_game()
class FiveByFive(App[None]):
"""Main 5x5 application class."""
#: The name of the stylesheet for the app.
CSS_PATH = "five_by_five.css"
#: The pre-loaded screens for the application.
SCREENS = {"help": Help()}
#: App-level bindings.
BINDINGS = [("ctrl+d", "toggle_dark", "Toggle Dark Mode")]
# Set the title
TITLE = "5x5 -- A little annoying puzzle"
def on_mount(self) -> None:
"""Set up the application on startup."""
self.push_screen(Game())
if __name__ == "__main__":
FiveByFive().run()

View File

@@ -1,4 +1,4 @@
from textual.app import App
from textual.app import App, ComposeResult
from textual.widgets import Static
@@ -7,7 +7,7 @@ class PrideApp(App):
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
def compose(self):
def compose(self) -> ComposeResult:
for color in self.COLORS:
stripe = Static()
stripe.styles.height = "1fr"

BIN
imgs/calculator.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 38 KiB

BIN
imgs/demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

299
imgs/demo.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 124 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 36 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 520 KiB

After

Width:  |  Height:  |  Size: 398 KiB

View File

@@ -24,11 +24,6 @@ nav:
- "guide/animation.md"
- "guide/screens.md"
- "roadmap.md"
- How to:
- "how-to/index.md"
- "how-to/animation.md"
- "how-to/mouse-and-keyboard.md"
- "how-to/scroll.md"
- Events:
- "events/index.md"
- "events/blur.md"
@@ -171,7 +166,7 @@ theme:
accent: purple
toggle:
icon: material/weather-sunny
name: Switch to dark modeTask was destroyed but it is pending!
name: Switch to dark mode
- media: "(prefers-color-scheme: dark)"
scheme: slate
primary: black

View File

@@ -1,5 +0,0 @@
# Layout
## rich.layout.Layout
The Layout class is responsible for arranging widget within a defined area. There are several concrete Layout objects with different strategies for positioning widgets.

39
poetry.lock generated
View File

@@ -66,11 +66,11 @@ tests_no_zope = ["coverage[toml] (>=5.0.2)", "hypothesis", "pympler", "pytest (>
[[package]]
name = "black"
version = "22.8.0"
version = "22.10.0"
description = "The uncompromising code formatter."
category = "dev"
optional = false
python-versions = ">=3.6.2"
python-versions = ">=3.7"
[package.dependencies]
click = ">=8.0.0"
@@ -318,7 +318,7 @@ python-versions = ">=3.6"
[[package]]
name = "mkdocs"
version = "1.4.0"
version = "1.4.1"
description = "Project documentation with Markdown."
category = "dev"
optional = false
@@ -326,18 +326,20 @@ python-versions = ">=3.7"
[package.dependencies]
click = ">=7.0"
colorama = {version = ">=0.4", markers = "platform_system == \"Windows\""}
ghp-import = ">=1.0"
importlib-metadata = {version = ">=4.3", markers = "python_version < \"3.10\""}
Jinja2 = ">=2.11.1"
Markdown = ">=3.2.1,<3.4"
jinja2 = ">=2.11.1"
markdown = ">=3.2.1,<3.4"
mergedeep = ">=1.3.4"
packaging = ">=20.5"
PyYAML = ">=5.1"
pyyaml = ">=5.1"
pyyaml-env-tag = ">=0.1"
typing-extensions = {version = ">=3.10", markers = "python_version < \"3.8\""}
watchdog = ">=2.0"
[package.extras]
min-versions = ["watchdog (==2.0)", "typing-extensions (==3.10)", "pyyaml (==5.1)", "pyyaml-env-tag (==0.1)", "packaging (==20.5)", "mergedeep (==1.3.4)", "markupsafe (==2.0.1)", "markdown (==3.2.1)", "jinja2 (==2.11.1)", "importlib-metadata (==4.3)", "ghp-import (==1.0)", "colorama (==0.4)", "click (==7.0)", "babel (==2.9.0)"]
i18n = ["babel (>=2.9.0)"]
[[package]]
@@ -354,7 +356,7 @@ mkdocs = ">=1.1"
[[package]]
name = "mkdocs-material"
version = "8.5.6"
version = "8.5.7"
description = "Documentation that simply works"
category = "dev"
optional = false
@@ -371,11 +373,11 @@ requests = ">=2.26"
[[package]]
name = "mkdocs-material-extensions"
version = "1.0.3"
description = "Extension pack for Python Markdown."
version = "1.1"
description = "Extension pack for Python Markdown and MkDocs Material."
category = "dev"
optional = false
python-versions = ">=3.6"
python-versions = ">=3.7"
[[package]]
name = "mkdocstrings"
@@ -554,7 +556,7 @@ plugins = ["importlib-metadata"]
[[package]]
name = "pymdown-extensions"
version = "9.6"
version = "9.7"
description = "Extension pack for Python Markdown."
category = "dev"
optional = false
@@ -613,7 +615,7 @@ testing = ["coverage (==6.2)", "mypy (==0.931)"]
[[package]]
name = "pytest-asyncio"
version = "0.19.0"
version = "0.20.1"
description = "Pytest support for asyncio"
category = "dev"
optional = false
@@ -763,7 +765,7 @@ python-versions = ">=3.6"
[[package]]
name = "typing-extensions"
version = "4.3.0"
version = "4.4.0"
description = "Backported and Experimental Type Hints for Python 3.7+"
category = "main"
optional = false
@@ -826,15 +828,15 @@ typing-extensions = {version = ">=3.7.4", markers = "python_version < \"3.8\""}
[[package]]
name = "zipp"
version = "3.8.1"
version = "3.9.0"
description = "Backport of pathlib-compatible object wrapper for zip files"
category = "main"
optional = false
python-versions = ">=3.7"
[package.extras]
docs = ["sphinx", "jaraco.packaging (>=9)", "rst.linker (>=1.9)", "jaraco.tidelift (>=1.4)"]
testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-cov", "pytest-enabler (>=1.3)", "jaraco.itertools", "func-timeout", "pytest-black (>=0.3.7)", "pytest-mypy (>=0.9.1)"]
docs = ["sphinx (>=3.5)", "jaraco.packaging (>=9)", "rst.linker (>=1.9)", "furo", "jaraco.tidelift (>=1.4)"]
testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "flake8 (<5)", "pytest-cov", "pytest-enabler (>=1.3)", "jaraco.itertools", "func-timeout", "jaraco.functools", "more-itertools", "pytest-black (>=0.3.7)", "pytest-mypy (>=0.9.1)"]
[extras]
dev = ["aiohttp", "click", "msgpack"]
@@ -959,10 +961,7 @@ mkdocs-autorefs = [
{file = "mkdocs_autorefs-0.4.1-py3-none-any.whl", hash = "sha256:a2248a9501b29dc0cc8ba4c09f4f47ff121945f6ce33d760f145d6f89d313f5b"},
]
mkdocs-material = []
mkdocs-material-extensions = [
{file = "mkdocs-material-extensions-1.0.3.tar.gz", hash = "sha256:bfd24dfdef7b41c312ede42648f9eb83476ea168ec163b613f9abd12bbfddba2"},
{file = "mkdocs_material_extensions-1.0.3-py3-none-any.whl", hash = "sha256:a82b70e533ce060b2a5d9eb2bc2e1be201cf61f901f93704b4acf6e3d5983a44"},
]
mkdocs-material-extensions = []
mkdocstrings = [
{file = "mkdocstrings-0.19.0-py3-none-any.whl", hash = "sha256:3217d510d385c961f69385a670b2677e68e07b5fea4a504d86bf54c006c87c7d"},
{file = "mkdocstrings-0.19.0.tar.gz", hash = "sha256:efa34a67bad11229d532d89f6836a8a215937548623b64f3698a1df62e01cc3e"},

View File

@@ -1,13 +1,13 @@
[tool.poetry]
name = "textual"
version = "0.2.0b7"
version = "0.2.1"
homepage = "https://github.com/Textualize/textual"
description = "Modern Text User Interface framework"
authors = ["Will McGugan <will@textualize.io>"]
license = "MIT"
readme = "README.md"
classifiers = [
"Development Status :: 1 - Planning",
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
"Operating System :: MacOS",

View File

@@ -1,252 +0,0 @@
/* CSS file for basic.py */
* {
transition: color 300ms linear, background 300ms linear;
}
*:hover {
/* tint: 30% red;
/* outline: heavy red; */
}
App > Screen {
background: $surface;
color: $text;
layers: base sidebar;
color: $text;
background: $background;
layout: vertical;
overflow: hidden;
}
#tree-container {
overflow-y: auto;
height: 20;
margin: 1 2;
background: $panel;
padding: 1 2;
}
DirectoryTree {
padding: 0 1;
height: auto;
}
DataTable {
/*border:heavy red;*/
/* tint: 10% green; */
/* opacity: 50%; */
padding: 1;
margin: 1 2;
height: 24;
}
#sidebar {
color: $text;
background: $panel;
dock: left;
width: 30;
margin-bottom: 1;
offset-x: -100%;
transition: offset 500ms in_out_cubic 2s;
layer: sidebar;
}
#sidebar.-active {
offset-x: 0;
}
#sidebar .title {
height: 1;
background: $primary-background-darken-1;
color: $text-muted;
border-right: wide $background;
content-align: center middle;
}
#sidebar .user {
height: 8;
background: $panel-darken-1;
color: $text-muted;
border-right: wide $background;
content-align: center middle;
}
#sidebar .content {
background: $panel-darken-2;
color: $text;
border-right: wide $background;
content-align: center middle;
}
Tweet {
height:12;
width: 100%;
background: $panel;
color: $text;
layout: vertical;
/* border: outer $primary; */
padding: 1;
border: wide $panel;
overflow: auto;
/* scrollbar-gutter: stable; */
align-horizontal: center;
box-sizing: border-box;
}
.scrollable {
overflow-x: auto;
overflow-y: scroll;
margin: 1 2;
height: 24;
align-horizontal: center;
layout: vertical;
}
.code {
height: auto;
}
TweetHeader {
height:1;
background: $accent;
color: $text
}
TweetBody {
width: 100%;
background: $panel;
color: $text;
height: auto;
padding: 0 1 0 0;
}
Tweet.scroll-horizontal TweetBody {
width: 350;
}
.button {
background: $accent;
color: $text;
width:20;
height: 3;
/* border-top: hidden $accent-darken-3; */
border: tall $accent-darken-2;
/* border-left: tall $accent-darken-1; */
/* padding: 1 0 0 0 ; */
transition: background 400ms in_out_cubic, color 400ms in_out_cubic;
}
.button:hover {
background: $accent-lighten-1;
color: $text-disabled;
width: 20;
height: 3;
border: tall $accent-darken-1;
/* border-left: tall $accent-darken-3; */
}
#footer {
color: $text;
background: $accent;
height: 1;
content-align: center middle;
dock:bottom;
}
#sidebar .content {
layout: vertical
}
OptionItem {
height: 3;
background: $panel;
border-right: wide $background;
border-left: blank;
content-align: center middle;
}
OptionItem:hover {
height: 3;
color: $text;
background: $primary-darken-1;
/* border-top: hkey $accent2-darken-3;
border-bottom: hkey $accent2-darken-3; */
text-style: bold;
border-left: outer $secondary-darken-2;
}
Error {
width: 100%;
height:3;
background: $error;
color: $text;
border-top: tall $error-darken-2;
border-bottom: tall $error-darken-2;
padding: 0;
text-style: bold;
align-horizontal: center;
}
Warning {
width: 100%;
height:3;
background: $warning;
color: $text-muted;
border-top: tall $warning-darken-2;
border-bottom: tall $warning-darken-2;
text-style: bold;
align-horizontal: center;
}
Success {
width: 100%;
height:auto;
box-sizing: border-box;
background: $success;
color: $text-muted;
border-top: hkey $success-darken-2;
border-bottom: hkey $success-darken-2;
text-style: bold ;
align-horizontal: center;
}
.horizontal {
layout: horizontal
}

View File

@@ -1,235 +0,0 @@
from rich.console import RenderableType
from rich.syntax import Syntax
from rich.text import Text
from textual.app import App, ComposeResult
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer
from textual.containers import Container
CODE = '''
from __future__ import annotations
from typing import Iterable, TypeVar
T = TypeVar("T")
def loop_first(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for first value."""
iter_values = iter(values)
try:
value = next(iter_values)
except StopIteration:
return
yield True, value
for value in iter_values:
yield False, value
def loop_last(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
lorem_short = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit liber a a a, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum."""
lorem = (
lorem_short
+ """ In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit libero, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum. In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. """
)
lorem_short_text = Text.from_markup(lorem_short)
lorem_long_text = Text.from_markup(lorem * 2)
class TweetHeader(Widget):
def render(self) -> RenderableType:
return Text("Lorem Impsum", justify="center")
class TweetBody(Widget):
short_lorem = Reactive(False)
def render(self) -> Text:
return lorem_short_text if self.short_lorem else lorem_long_text
class Tweet(Widget):
pass
class OptionItem(Widget):
def render(self) -> Text:
return Text("Option")
class Error(Widget):
def render(self) -> Text:
return Text("This is an error message", justify="center")
class Warning(Widget):
def render(self) -> Text:
return Text("This is a warning message", justify="center")
class Success(Widget):
def render(self) -> Text:
return Text("This is a success message", justify="center")
class BasicApp(App, css_path="basic.css"):
"""A basic app demonstrating CSS"""
def on_load(self):
"""Bind keys here."""
self.bind("s", "toggle_class('#sidebar', '-active')", description="Sidebar")
self.bind("d", "toggle_dark", description="Dark mode")
self.bind("q", "quit", description="Quit")
self.bind("f", "query_test", description="Query test")
def compose(self):
yield Header()
table = DataTable()
self.scroll_to_target = Tweet(TweetBody())
yield Container(
Tweet(TweetBody()),
Widget(
Static(
Syntax(CODE, "python", line_numbers=True, indent_guides=True),
classes="code",
),
classes="scrollable",
),
table,
Widget(DirectoryTree("~/code/textual"), id="tree-container"),
Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Success(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
)
yield Widget(
Widget(classes="title"),
Widget(classes="user"),
OptionItem(),
OptionItem(),
OptionItem(),
Widget(classes="content"),
id="sidebar",
)
yield Footer()
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.zebra_stripes = True
for n in range(100):
table.add_row(*[f"Cell ([b]{n}[/b], {col})" for col in range(6)])
def on_mount(self):
self.sub_title = "Widget demo"
async def on_key(self, event) -> None:
await self.dispatch_key(event)
def action_toggle_dark(self):
self.dark = not self.dark
def action_query_test(self):
query = self.query("Tweet")
self.log(query)
self.log(query.nodes)
self.log(query)
self.log(query.nodes)
query.set_styles("outline: outer red;")
query = query.exclude(".scroll-horizontal")
self.log(query)
self.log(query.nodes)
# query = query.filter(".rubbish")
# self.log(query)
# self.log(query.first())
async def key_q(self):
await self.shutdown()
def key_x(self):
self.panic(self.tree)
def key_escape(self):
self.app.bell()
def key_t(self):
# Pressing "t" toggles the content of the TweetBody widget, from a long "Lorem ipsum..." to a shorter one.
tweet_body = self.query("TweetBody").first()
tweet_body.short_lorem = not tweet_body.short_lorem
def key_v(self):
self.get_child(id="content").scroll_to_widget(self.scroll_to_target)
def key_space(self):
self.bell()
app = BasicApp()
if __name__ == "__main__":
app.run()
# from textual.geometry import Region
# from textual.color import Color
# print(Region.intersection.cache_info())
# print(Region.overlaps.cache_info())
# print(Region.union.cache_info())
# print(Region.split_vertical.cache_info())
# print(Region.__contains__.cache_info())
# from textual.css.scalar import Scalar
# print(Scalar.resolve_dimension.cache_info())
# from rich.style import Style
# from rich.cells import cached_cell_len
# print(Style._add.cache_info())
# print(cached_cell_len.cache_info())

View File

@@ -1,6 +0,0 @@
Button {
padding-left: 1;
padding-right: 1;
margin: 3;
text-opacity: 30%;
}

View File

@@ -1,34 +0,0 @@
from textual import events
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Button
class ButtonsApp(App[str]):
def compose(self) -> ComposeResult:
yield Vertical(
Button("default", id="foo"),
Button.success("success", id="bar"),
Button.warning("warning", id="baz"),
Button.error("error", id="baz"),
)
def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.bell()
async def on_key(self, event: events.Key) -> None:
await self.dispatch_key(event)
def key_d(self):
self.dark = not self.dark
app = ButtonsApp(
log_path="textual.log",
css_path="buttons.css",
watch_css=True,
)
if __name__ == "__main__":
result = app.run()
print(repr(result))

View File

@@ -1,28 +0,0 @@
Screen {
align: center middle;
}
Container {
width: 50;
height: 15;
background: $boost;
align: center middle;
}
Checkbox {
}
#check {
background: red;
border: none;
padding: 0;
}
#check > .checkbox--switch {
color: red;
background: blue;
}
#check:focus {
tint: magenta 60%;
}

View File

@@ -1,24 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Checkbox, Footer
class CheckboxApp(App):
BINDINGS = [("s", "switch", "Press switch"), ("d", "toggle_dark", "Dark mode")]
def compose(self) -> ComposeResult:
yield Footer()
yield Container(Checkbox(id="check", animate=True))
def action_switch(self) -> None:
checkbox = self.query_one(Checkbox)
checkbox.toggle()
def key_f(self):
print(self.app.focused)
app = CheckboxApp(css_path="check.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,8 +0,0 @@
* {
transition: color 300ms linear, background 300ms linear;
}
#another-box {
background: $boost;
padding: 1 2;
}

View File

@@ -1,28 +0,0 @@
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.color import Color
from textual.widgets import Static
START_COLOR = Color.parse("#FF0000EE")
END_COLOR = Color.parse("#0000FF0F")
class ColorAnimate(App):
BINDINGS = [Binding("d", action="toggle_dark", description="Dark mode")]
def compose(self) -> ComposeResult:
self.box = Static("Hello, world", id="box")
self.box.styles.background = START_COLOR
self.another_box = Static("Another box with $boost", id="another-box")
yield self.box
yield self.another_box
def key_a(self):
self.animator.animate(self.box.styles, "background", END_COLOR, duration=2.0)
app = ColorAnimate(css_path="color_animate.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,68 +0,0 @@
from __future__ import annotations
from pathlib import Path
from typing import Iterable
from rich.console import RenderableType
from rich.table import Table
from rich.text import Text
from textual.app import App
from textual.geometry import Size
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets._input import Input
def get_files() -> list[Path]:
files = list(Path.cwd().iterdir())
return files
class FileTable(Widget):
filter = Reactive("", layout=True)
def __init__(self, *args, files: Iterable[Path] | None = None, **kwargs):
super().__init__(*args, **kwargs)
self.files = files if files is not None else []
@property
def filtered_files(self) -> list[Path]:
return [
file
for file in self.files
if self.filter == "" or (self.filter and self.filter in file.name)
]
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return len(self.filtered_files)
def render(self) -> RenderableType:
grid = Table.grid()
grid.add_column()
for file in self.filtered_files:
file_text = Text(f" {file.name}")
if self.filter:
file_text.highlight_regex(self.filter, "black on yellow")
grid.add_row(file_text)
return grid
class FileSearchApp(App):
dark = True
def on_mount(self) -> None:
self.file_table = FileTable(id="file_table", files=list(Path.cwd().iterdir()))
self.search_bar = Input(placeholder="Search for files...")
# self.search_bar.focus()
self.mount(search_bar=self.search_bar)
self.mount(file_table_wrapper=Widget(self.file_table))
def on_input_changed(self, event: Input.Changed) -> None:
self.file_table.filter = event.value
app = FileSearchApp(css_path="file_search.scss", watch_css=True)
if __name__ == "__main__":
result = app.run()

View File

@@ -1,15 +0,0 @@
Screen {
}
#file_table_wrapper {
scrollbar-color: $accent-darken-1;
}
#file_table {
height: auto;
}
#search_bar {
height: 1;
}

View File

@@ -1,52 +0,0 @@
*:focus {
tint: red 20%;
}
#info {
background: $primary;
dock: top;
height: 3;
padding: 1;
}
#body {
dock: top;
}
#left_list {
width: 50%;
}
#right_list {
width: 50%;
}
#footer {
height: 1;
background: $secondary;
padding: 0 1;
dock: bottom;
}
.list:focus-within {
background: $panel-lighten-1;
outline-top: $accent-lighten-1;
outline-bottom: $accent-lighten-1;
}
.list {
background: $surface;
border-top: hkey $surface-darken-1;
}
.list-item {
background: $surface;
height: auto;
border: $surface-darken-1 tall;
padding: 0 1;
}
.list-item:focus {
background: $surface-darken-1;
outline: $accent tall;
}

View File

@@ -1,66 +0,0 @@
from textual import containers as layout
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.widgets import Static, Input
class Label(Static, can_focus=True):
pass
class FocusKeybindsApp(App):
dark = True
BINDINGS = [Binding("a", "private_handler", "Private Handler")]
def on_load(self) -> None:
self.bind("1", "focus('widget1')")
self.bind("2", "focus('widget2')")
self.bind("3", "focus('widget3')")
self.bind("4", "focus('widget4')")
self.bind("q", "focus('widgetq')")
self.bind("w", "focus('widgetw')")
self.bind("e", "focus('widgete')")
self.bind("r", "focus('widgetr')")
def compose(self) -> ComposeResult:
yield Static(
"Use keybinds to shift focus between the widgets in the lists below",
id="info",
)
yield layout.Horizontal(
layout.Vertical(
Label("Press 1 to focus", id="widget1", classes="list-item"),
Label("Press 2 to focus", id="widget2", classes="list-item"),
Input(placeholder="Enter some text..."),
Label("Press 3 to focus", id="widget3", classes="list-item"),
Label("Press 4 to focus", id="widget4", classes="list-item"),
classes="list",
id="left_list",
),
layout.Vertical(
Label("Press Q to focus", id="widgetq", classes="list-item"),
Label("Press W to focus", id="widgetw", classes="list-item"),
Label("Press E to focus", id="widgete", classes="list-item"),
Label("Press R to focus", id="widgetr", classes="list-item"),
classes="list",
id="right_list",
),
)
yield Static("No widget focused", id="footer")
def on_descendant_focus(self):
self.get_child("footer").update(
f"Focused: {self.focused.id}" or "No widget focused"
)
def key_p(self):
print(self.app.focused.parent)
print(self.app.focused)
def _action_private_handler(self):
print("inside private handler!")
app = FocusKeybindsApp(css_path="focus_keybinds.css", watch_css=True)
app.run()

View File

@@ -1,12 +0,0 @@
Screen {
align: center middle;
background: darkslategrey;
overflow: auto auto;
}
#box1 {
background: darkmagenta;
width: auto;
opacity: 0.5;
padding: 4 8;
}

View File

@@ -1,52 +0,0 @@
from __future__ import annotations
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Static, Footer, Header
class MainScreen(Screen):
BINDINGS = [
Binding(
key="ctrl+t", action="text_fade_out", description="text-opacity fade out"
),
(
"o,f,w",
"widget_fade_out",
"opacity fade out",
# key_display="o or f or w",
),
]
def compose(self) -> ComposeResult:
yield Header()
yield Static("Hello, world!", id="box1")
yield Footer()
def action_text_fade_out(self) -> None:
box = self.query_one("#box1")
self.app.animator.animate(box.styles, "text_opacity", value=0.0, duration=1)
def action_widget_fade_out(self) -> None:
box = self.query_one("#box1")
self.app.animator.animate(box.styles, "opacity", value=0.0, duration=1)
class JustABox(App):
def on_mount(self):
self.push_screen(MainScreen())
def key_d(self):
print(self.screen.styles.get_rules())
print(self.screen.styles.css)
def key_plus(self):
print("plus!")
app = JustABox(watch_css=True, css_path="../darren/just_a_box.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Focusable {
padding: 1 2;
background: $panel;
margin-bottom: 1;
}
Focusable:focus {
outline: solid dodgerblue;
}

View File

@@ -1,73 +0,0 @@
from textual.app import App, ComposeResult, ScreenStackError
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Static, Footer, Input
from some_text import TEXT
class Focusable(Static, can_focus=True):
pass
class CustomScreen(Screen):
def compose(self) -> ComposeResult:
yield Focusable(f"Screen {id(self)} - two {TEXT}")
yield Focusable(f"Screen {id(self)} - three")
yield Focusable(f"Screen {id(self)} - four")
yield Input(placeholder="Text input")
yield Footer()
class MyInstalledScreen(Screen):
def __init__(self, string: str):
super().__init__()
self.string = string
def compose(self) -> ComposeResult:
yield Static(f"Hello, world! {self.string}")
class ScreensFocusApp(App):
BINDINGS = [
Binding("plus", "push_new_screen", "Push"),
Binding("minus", "pop_top_screen", "Pop"),
Binding("d", "toggle_dark", "Toggle Dark"),
Binding("q", "push_screen('q')", "Screen Q"),
Binding("w", "push_screen('w')", "Screen W"),
Binding("e", "push_screen('e')", "Screen E"),
Binding("r", "push_screen('r')", "Screen R"),
]
SCREENS = {
"q": MyInstalledScreen("q"),
"w": MyInstalledScreen("w"),
"e": MyInstalledScreen("e"),
"r": MyInstalledScreen("r"),
}
def compose(self) -> ComposeResult:
yield Focusable("App - one")
yield Input(placeholder="Text input")
yield Input(placeholder="Text input")
yield Focusable("App - two")
yield Focusable("App - three")
yield Focusable("App - four")
yield Footer()
def action_push_new_screen(self):
self.push_screen(CustomScreen())
def action_pop_top_screen(self):
try:
self.pop_screen()
except ScreenStackError:
pass
def _action_toggle_dark(self):
self.dark = not self.dark
app = ScreensFocusApp(css_path="screens_focus.css")
if __name__ == "__main__":
app.run()

View File

@@ -1 +0,0 @@
TEXT = "ABCDEFG"

View File

@@ -1,147 +0,0 @@
from dataclasses import dataclass
from rich.console import RenderableType
from rich.padding import Padding
from rich.rule import Rule
from rich.style import Style
from textual import events
from textual.app import App
from textual.widget import Widget
from textual.widgets.tabs import Tabs, Tab
class Hr(Widget):
def render(self) -> RenderableType:
return Rule()
class Info(Widget):
def __init__(self, text: str) -> None:
super().__init__()
self.text = text
def render(self) -> RenderableType:
return Padding(f"{self.text}", pad=(0, 1))
@dataclass
class WidgetDescription:
description: str
widget: Widget
class BasicApp(App):
"""Sandbox application used for testing/development by Textual developers"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.keys_to_tabs = {
"1": Tab("January", name="one"),
"2": Tab("に月", name="two"),
"3": Tab("March", name="three"),
"4": Tab("April", name="four"),
"5": Tab("May", name="five"),
"6": Tab("And a really long tab!", name="six"),
}
tabs = list(self.keys_to_tabs.values())
self.examples = [
WidgetDescription(
"Customise the spacing between tabs, e.g. tab_padding=1",
Tabs(
tabs,
tab_padding=1,
),
),
WidgetDescription(
"Change the opacity of inactive tab text, e.g. inactive_text_opacity=.2",
Tabs(
tabs,
active_tab="two",
active_bar_style="#1493FF",
inactive_text_opacity=0.2,
tab_padding=2,
),
),
WidgetDescription(
"Change the color of the inactive portions of the underline, e.g. inactive_bar_style='blue'",
Tabs(
tabs,
active_tab="four",
inactive_bar_style="blue",
),
),
WidgetDescription(
"Change the color of the active portion of the underline, e.g. active_bar_style='red'",
Tabs(
tabs,
active_tab="five",
active_bar_style="red",
inactive_text_opacity=1,
),
),
WidgetDescription(
"Change the styling of active and inactive labels (active_tab_style, inactive_tab_style)",
Tabs(
tabs,
active_tab="one",
active_bar_style="#DA812D",
active_tab_style="bold #FFCB4D on #021720",
inactive_tab_style="italic #887AEF on #021720",
inactive_bar_style="#695CC8",
inactive_text_opacity=0.6,
),
),
WidgetDescription(
"Change the animation duration and function (animation_duration=1, animation_function='out_quad')",
Tabs(
tabs,
active_tab="one",
active_bar_style="#887AEF",
inactive_text_opacity=0.2,
animation_duration=1,
animation_function="out_quad",
),
),
WidgetDescription(
"Choose which tab to start on by name, e.g. active_tab='three'",
Tabs(
tabs,
active_tab="three",
active_bar_style="#FFCB4D",
tab_padding=3,
),
),
]
def on_load(self):
"""Bind keys here."""
self.bind("tab", "toggle_class('#sidebar', '-active')")
self.bind("a", "toggle_class('#header', '-visible')")
self.bind("c", "toggle_class('#content', '-content-visible')")
self.bind("d", "toggle_class('#footer', 'dim')")
def on_key(self, event: events.Key) -> None:
for example in self.examples:
tab = self.keys_to_tabs.get(event.key)
if tab:
example.widget._active_tab_name = tab.name
def on_mount(self):
"""Build layout here."""
self.mount(
info=Info(
"\n"
"• The examples below show customisation options for the [bold #1493FF]Tabs[/] widget.\n"
"• Press keys 1-6 on your keyboard to switch tabs, or click on a tab.",
)
)
for example in self.examples:
info = Info(example.description)
self.mount(Hr())
self.mount(info)
self.mount(example.widget)
app = BasicApp(css_path="tabs.scss", watch_css=True, log_path="textual.log")
app.run()

View File

@@ -1,9 +0,0 @@
$background: #021720;
App > View {
background: $background;
}
#info {
height: 4;
}

View File

@@ -1,33 +0,0 @@
from __future__ import annotations
from textual.app import App, ComposeResult
from textual.widgets import Static
TEXT = (
"I must not fear. Fear is the mind-killer. Fear is the little-death that "
"brings total obliteration. I will face my fear. I will permit it to pass over "
"me and through me. And when it has gone past, I will turn the inner eye to "
"see its path. Where the fear has gone there will be nothing. Only I will "
"remain. "
)
class TextAlign(App):
def compose(self) -> ComposeResult:
left = Static("[b]Left aligned[/]\n" + TEXT, id="one")
yield left
right = Static("[b]Center aligned[/]\n" + TEXT, id="two")
yield right
center = Static("[b]Right aligned[/]\n" + TEXT, id="three")
yield center
full = Static("[b]Fully justified[/]\n" + TEXT, id="four")
yield full
app = TextAlign(css_path="text_align.scss", watch_css=True)
if __name__ == "__main__":
app.run()

View File

@@ -1,24 +0,0 @@
#one {
text-align: left;
background: lightblue;
}
#two {
text-align: center;
background: indianred;
}
#three {
text-align: right;
background: palegreen;
}
#four {
text-align: justify;
background: palevioletred;
}
Static {
padding: 1;
}

View File

@@ -1,2 +0,0 @@
all:
poetry run textual run --dev focus_removal_tester.py

View File

@@ -1,45 +0,0 @@
"""Focus removal tester.
https://github.com/Textualize/textual/issues/939
"""
from textual.app import App
from textual.containers import Container
from textual.widgets import Static, Header, Footer, Button
class LeftButton(Button):
pass
class RightButton(Button):
pass
class NonFocusParent(Static):
def compose(self):
yield LeftButton("Do Not Press")
yield Static("Test")
yield RightButton("Really Do Not Press")
class FocusRemovalTester(App[None]):
BINDINGS = [("a", "add_widget", "Add Widget"), ("d", "del_widget", "Delete Widget")]
def compose(self):
yield Header()
yield Container()
yield Footer()
def action_add_widget(self):
self.query_one(Container).mount(NonFocusParent())
def action_del_widget(self):
candidates = self.query(NonFocusParent)
if candidates:
candidates.last().remove()
if __name__ == "__main__":
FocusRemovalTester().run()

View File

@@ -1,71 +0,0 @@
import random
from textual.containers import Horizontal, Vertical
from textual.app import App, ComposeResult
from textual.widgets import Button, Static
class Thing(Static):
def on_show(self) -> None:
self.scroll_visible()
class AddRemoveApp(App):
DEFAULT_CSS = """
#buttons {
dock: top;
height: auto;
}
#buttons Button {
width: 1fr;
}
#items {
height: 100%;
overflow-y: scroll;
}
Thing {
height: 5;
background: $panel;
border: tall $primary;
margin: 1 1;
content-align: center middle;
}
"""
def on_mount(self) -> None:
self.count = 0
def compose(self) -> ComposeResult:
yield Vertical(
Horizontal(
Button("Add", variant="success", id="add"),
Button("Remove", variant="error", id="remove"),
Button("Remove random", variant="warning", id="remove_random"),
id="buttons",
),
Vertical(id="items"),
)
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "add":
self.count += 1
self.query("#items").first().mount(
Thing(f"Thing {self.count}", id=f"thing{self.count}")
)
elif event.button.id == "remove":
things = self.query("#items Thing")
if things:
things.last().remove()
elif event.button.id == "remove_random":
things = self.query("#items Thing")
if things:
random.choice(things).remove()
self.app.bell()
app = AddRemoveApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,14 +0,0 @@
Screen {
align: center middle;
}
Label {
width: 20;
height: 5;
background: blue;
color: white;
border: tall white;
margin: 1;
content-align: center middle;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class Label(Static):
pass
class AlignApp(App):
CSS_PATH = "align.css"
def compose(self) -> ComposeResult:
yield Label("Hello")
yield Label("World!")
if __name__ == "__main__":
app = AlignApp()
app.run()

View File

@@ -1,265 +0,0 @@
/* CSS file for basic.py */
* {
transition: color 300ms linear, background 300ms linear;
}
Tweet.tall {
height: 24;
}
*:hover {
/* tint: 30% red;
/* outline: heavy red; */
}
App > Screen {
color: $text;
layers: base sidebar;
layout: vertical;
overflow: hidden;
}
#tree-container {
background: $panel;
overflow-y: auto;
height: 20;
margin: 1 2;
padding: 1 2;
}
DirectoryTree {
padding: 0 1;
height: auto;
}
#table-container {
background: $panel;
height: auto;
margin: 1 2;
}
DataTable {
/*border:heavy red;*/
/* tint: 10% green; */
/* text-opacity: 50%; */
margin: 1 2;
height: 24;
}
#sidebar {
background: $panel;
color: $text;
dock: left;
width: 30;
margin-bottom: 1;
offset-x: -100%;
transition: offset 500ms in_out_cubic;
layer: sidebar;
}
#sidebar.-active {
offset-x: 0;
}
#sidebar .title {
height: 1;
background: $primary-background-darken-1;
color: $text;
border-right: wide $background;
content-align: center middle;
}
#sidebar .user {
height: 8;
background: $panel-darken-1;
color: $text;
border-right: wide $background;
content-align: center middle;
}
#sidebar .content {
background: $panel-darken-2;
color: $text;
border-right: wide $background;
content-align: center middle;
}
Tweet {
height:12;
width: 100%;
margin: 0 2;
margin:0 2;
background: $panel;
color: $text;
layout: vertical;
/* border: outer $primary; */
padding: 1;
border: wide $panel;
/* scrollbar-gutter: stable; */
box-sizing: border-box;
}
.scrollable {
overflow-x: auto;
overflow-y: scroll;
padding: 0 2;
margin: 1 2;
height: 24;
layout: vertical;
}
.code {
height: auto;
}
TweetHeader {
height:1;
background: $accent;
color: $text;
}
TweetBody {
width: 100%;
background: $panel;
color: $text;
height: auto;
padding: 0 1 0 0;
}
Tweet.scroll-horizontal {
overflow-x: auto;
}
Tweet.scroll-horizontal TweetBody {
width: 350;
}
.button {
background: $accent;
color: $text;
width:20;
height: 3;
/* border-top: hidden $accent-darken-3; */
border: tall $accent-darken-2;
/* border-left: tall $accent-darken-1; */
/* padding: 1 0 0 0 ; */
transition: background 400ms in_out_cubic, color 400ms in_out_cubic;
}
.button:hover {
background: $accent-lighten-1;
color: $text;
width: 20;
height: 3;
border: tall $accent-darken-1;
/* border-left: tall $accent-darken-3; */
}
#footer {
color: $text;
background: $accent;
height: 1;
content-align: center middle;
dock:bottom;
}
#sidebar .content {
layout: vertical;
}
OptionItem {
height: 3;
background: $panel;
border-right: wide $background;
border-left: blank;
content-align: center middle;
}
OptionItem:hover {
height: 3;
color: $text;
background: $primary-darken-1;
/* border-top: hkey $accent2-darken-3;
border-bottom: hkey $accent2-darken-3; */
text-style: bold;
border-left: outer $secondary-darken-2;
}
Error {
width: 100%;
height:3;
background: $error;
color: $text;
border-top: tall $error-darken-2;
border-bottom: tall $error-darken-2;
padding: 0;
text-style: bold;
content-align: center middle;
}
Warning {
width: 100%;
height:3;
background: $warning;
color: $text;
border-top: tall $warning-darken-2;
border-bottom: tall $warning-darken-2;
text-style: bold;
content-align: center middle;
}
Success {
width: 100%;
height:auto;
box-sizing: border-box;
background: $success;
color: $text;
border-top: hkey $success-darken-2;
border-bottom: hkey $success-darken-2;
text-style: bold ;
content-align: center middle;
}
.horizontal {
layout: horizontal
}

View File

@@ -1,243 +0,0 @@
from rich.console import RenderableType
from rich.syntax import Syntax
from rich.text import Text
from textual.app import App, ComposeResult
from textual.reactive import Reactive
from textual.widget import Widget
from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer
from textual.containers import Container, Vertical
CODE = '''
from __future__ import annotations
from typing import Iterable, TypeVar
T = TypeVar("T")
def loop_first(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for first value."""
iter_values = iter(values)
try:
value = next(iter_values)
except StopIteration:
return
yield True, value
for value in iter_values:
yield False, value
def loop_last(values: Iterable[T]) -> Iterable[tuple[bool, T]]:
"""Iterate and generate a tuple with a flag for last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
for value in iter_values:
yield False, previous_value
previous_value = value
yield True, previous_value
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value
'''
lorem_short = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit liber a a a, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum."""
lorem = (
lorem_short
+ """ In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit libero, volutpat nec hendrerit at, faucibus in odio. Aliquam hendrerit nibh sed quam volutpat maximus. Nullam suscipit convallis lorem quis sodales. In tristique lobortis ante et dictum. Ut at finibus ipsum. In urna dolor, placerat et mi facilisis, congue sollicitudin massa. Phasellus felis turpis, cursus eu lectus et, porttitor malesuada augue. Sed feugiat volutpat velit, sollicitudin fringilla velit bibendum faucibus. """
)
lorem_short_text = Text.from_markup(lorem_short)
lorem_long_text = Text.from_markup(lorem * 2)
class TweetHeader(Static):
def render(self) -> RenderableType:
return Text("Lorem Impsum", justify="center")
class TweetBody(Static):
short_lorem = Reactive(False)
def render(self) -> Text:
return lorem_short_text if self.short_lorem else lorem_long_text
class Tweet(Vertical):
pass
class OptionItem(Static):
def render(self) -> Text:
return Text("Option")
class Error(Static):
def render(self) -> Text:
return Text("This is an error message", justify="center")
class Warning(Static):
def render(self) -> Text:
return Text("This is a warning message", justify="center")
class Success(Static):
def render(self) -> Text:
return Text("This is a success message", justify="center")
class BasicApp(App):
"""A basic app demonstrating CSS"""
CSS_PATH = "basic.css"
def on_load(self):
"""Bind keys here."""
self.bind("s", "toggle_class('#sidebar', '-active')", description="Sidebar")
self.bind("d", "toggle_dark", description="Dark mode")
self.bind("q", "quit", description="Quit")
self.bind("f", "query_test", description="Query test")
def compose(self):
yield Header()
table = DataTable()
self.scroll_to_target = Tweet(TweetBody())
yield Vertical(
Tweet(TweetBody()),
Tweet(
Static(
Syntax(
CODE,
"python",
theme="ansi_dark",
line_numbers=True,
indent_guides=True,
),
classes="code",
),
classes="tall",
),
Container(table, id="table-container"),
Container(DirectoryTree("~/"), id="tree-container"),
Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Success(),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
Tweet(TweetBody(), classes="scroll-horizontal"),
)
yield Widget(
Static("Title", classes="title"),
Static("Content", classes="user"),
OptionItem(),
OptionItem(),
OptionItem(),
Static(classes="content"),
id="sidebar",
)
yield Footer()
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.add_column("Foo", width=20)
table.add_column("Bar", width=20)
table.add_column("Baz", width=20)
table.zebra_stripes = True
for n in range(100):
table.add_row(*[f"Cell ([b]{n}[/b], {col})" for col in range(6)])
def on_mount(self):
self.sub_title = "Widget demo"
async def on_key(self, event) -> None:
await self.dispatch_key(event)
def action_toggle_dark(self):
self.dark = not self.dark
def action_query_test(self):
query = self.query("Tweet")
self.log(query)
self.log(query.nodes)
self.log(query)
self.log(query.nodes)
query.set_styles("outline: outer red;")
query = query.exclude(".scroll-horizontal")
self.log(query)
self.log(query.nodes)
# query = query.filter(".rubbish")
# self.log(query)
# self.log(query.first())
async def key_q(self):
await self.shutdown()
def key_x(self):
self.panic(self.tree)
def key_escape(self):
self.app.bell()
def key_t(self):
# Pressing "t" toggles the content of the TweetBody widget, from a long "Lorem ipsum..." to a shorter one.
tweet_body = self.query("TweetBody").first()
tweet_body.short_lorem = not tweet_body.short_lorem
def key_v(self):
self.get_child(id="content").scroll_to_widget(self.scroll_to_target)
def key_space(self):
self.bell()
app = BasicApp()
if __name__ == "__main__":
app.run()
# from textual.geometry import Region
# from textual.color import Color
# print(Region.intersection.cache_info())
# print(Region.overlaps.cache_info())
# print(Region.union.cache_info())
# print(Region.split_vertical.cache_info())
# print(Region.__contains__.cache_info())
# from textual.css.scalar import Scalar
# print(Scalar.resolve_dimension.cache_info())
# from rich.style import Style
# from rich.cells import cached_cell_len
# print(Style._add.cache_info())
# print(cached_cell_len.cache_info())

View File

@@ -1,54 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Footer, Static
class Focusable(Static, can_focus=True):
DEFAULT_CSS = """
Focusable {
background: blue 20%;
height: 1fr;
padding: 1;
}
Focusable:hover {
outline: solid white;
}
Focusable:focus {
background: red 20%;
}
"""
class Focusable1(Focusable):
BINDINGS = [
("a", "app.bell", "Ding"),
]
def render(self) -> str:
return repr(self)
class Focusable2(Focusable):
CSS = ""
BINDINGS = [
("b", "app.bell", "Beep"),
("f1", "app.quit", "QUIT"),
]
def render(self) -> str:
return repr(self)
class BindingApp(App):
BINDINGS = [("f1", "app.bell", "Bell")]
def compose(self) -> ComposeResult:
yield Focusable1()
yield Focusable2()
yield Footer()
app = BindingApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,28 +0,0 @@
Screen {
background: white;
color:black;
}
#box1 {
width: 10;
height: 5;
background: red 40%;
box-sizing: content-box;
}
#box2 {
width: 10;
height: 5;
padding: 1;
background:blue 40%;
box-sizing: content-box;
}
#box3 {
width: 10;
height: 5;
background:green 40%;
border: heavy;
box-sizing: content-box;
}

View File

@@ -1,13 +0,0 @@
from textual.app import App
from textual.widgets import Static
class BoxApp(App):
def compose(self):
yield Static("0123456789", id="box1")
yield Static("0123456789", id="box2")
yield Static("0123456789", id="box3")
app = BoxApp(css_path="box.css")
app.run()

View File

@@ -1,24 +0,0 @@
Button {
margin: 1;
width: 100%;
}
Vertical {
height: auto;
}
Horizontal {
height: auto;
}
Horizontal Button {
width: 20;
margin: 1 2 ;
}
#scroll {
height: 10;
}

View File

@@ -1,32 +0,0 @@
Screen {
overflow: auto;
}
#calculator {
layout: grid;
grid-size: 4;
grid-gutter: 1 2;
grid-columns: 1fr;
grid-rows: 2fr 1fr 1fr 1fr 1fr 1fr;
margin: 1 2;
min-height:25;
min-width: 26;
}
Button {
width: 100%;
height: 100%;
}
#numbers {
column-span: 4;
content-align: right middle;
padding: 0 1;
height: 100%;
background: $primary-lighten-2;
color: $text;
}
#number-0 {
column-span: 2;
}

View File

@@ -1,144 +0,0 @@
from decimal import Decimal
from textual.app import App, ComposeResult
from textual import events
from textual.containers import Container
from textual.reactive import Reactive
from textual.widgets import Button, Static
class CalculatorApp(App):
"""A working 'desktop' calculator."""
numbers = Reactive.var("0")
show_ac = Reactive.var(True)
left = Reactive.var(Decimal("0"))
right = Reactive.var(Decimal("0"))
value = Reactive.var("")
operator = Reactive.var("plus")
KEY_MAP = {
"+": "plus",
"-": "minus",
".": "point",
"*": "multiply",
"/": "divide",
"_": "plus-minus",
"%": "percent",
"=": "equals",
}
def watch_numbers(self, value: str) -> None:
"""Called when numbers is updated."""
# Update the Numbers widget
self.query_one("#numbers", Static).update(value)
def compute_show_ac(self) -> bool:
"""Compute switch to show AC or C button"""
return self.value in ("", "0") and self.numbers == "0"
def watch_show_ac(self, show_ac: bool) -> None:
"""Called when show_ac changes."""
self.query_one("#c").display = not show_ac
self.query_one("#ac").display = show_ac
def compose(self) -> ComposeResult:
"""Add our buttons."""
yield Container(
Static(id="numbers"),
Button("AC", id="ac", variant="primary"),
Button("C", id="c", variant="primary"),
Button("+/-", id="plus-minus", variant="primary"),
Button("%", id="percent", variant="primary"),
Button("÷", id="divide", variant="warning"),
Button("7", id="number-7"),
Button("8", id="number-8"),
Button("9", id="number-9"),
Button("×", id="multiply", variant="warning"),
Button("4", id="number-4"),
Button("5", id="number-5"),
Button("6", id="number-6"),
Button("-", id="minus", variant="warning"),
Button("1", id="number-1"),
Button("2", id="number-2"),
Button("3", id="number-3"),
Button("+", id="plus", variant="warning"),
Button("0", id="number-0"),
Button(".", id="point"),
Button("=", id="equals", variant="warning"),
id="calculator",
)
def on_key(self, event: events.Key) -> None:
"""Called when the user presses a key."""
print(f"KEY {event} was pressed!")
def press(button_id: str) -> None:
self.query_one(f"#{button_id}", Button).press()
self.set_focus(None)
key = event.key
if key.isdecimal():
press(f"number-{key}")
elif key == "c":
press("c")
press("ac")
elif key in self.KEY_MAP:
press(self.KEY_MAP[key])
def on_button_pressed(self, event: Button.Pressed) -> None:
"""Called when a button is pressed."""
button_id = event.button.id
assert button_id is not None
self.bell() # Terminal bell
def do_math() -> None:
"""Does the math: LEFT OPERATOR RIGHT"""
try:
if self.operator == "plus":
self.left += self.right
elif self.operator == "minus":
self.left -= self.right
elif self.operator == "divide":
self.left /= self.right
elif self.operator == "multiply":
self.left *= self.right
self.numbers = str(self.left)
self.value = ""
except Exception:
self.numbers = "Error"
if button_id.startswith("number-"):
number = button_id.partition("-")[-1]
self.numbers = self.value = self.value.lstrip("0") + number
elif button_id == "plus-minus":
self.numbers = self.value = str(Decimal(self.value or "0") * -1)
elif button_id == "percent":
self.numbers = self.value = str(Decimal(self.value or "0") / Decimal(100))
elif button_id == "point":
if "." not in self.value:
self.numbers = self.value = (self.value or "0") + "."
elif button_id == "ac":
self.value = ""
self.left = self.right = Decimal(0)
self.operator = "plus"
self.numbers = "0"
elif button_id == "c":
self.value = ""
self.numbers = "0"
elif button_id in ("plus", "minus", "divide", "multiply"):
self.right = Decimal(self.value or "0")
do_math()
self.operator = button_id
elif button_id == "equals":
if self.value:
self.right = Decimal(self.value)
do_math()
app = CalculatorApp(css_path="calculator.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,28 +0,0 @@
from textual.app import App
from textual.widgets import Static
class CenterApp(App):
DEFAULT_CSS = """
CenterApp Screen {
layout: center;
overflow: auto auto;
}
CenterApp Static {
border: wide $primary;
background: $panel;
width: 50;
height: 20;
margin: 1 2;
content-align: center middle;
}
"""
def compose(self):
yield Static("Hello World!")
app = CenterApp()

View File

@@ -1,55 +0,0 @@
from textual.app import App
from textual.containers import Vertical, Center
from textual.widgets import Static
class CenterApp(App):
DEFAULT_CSS = """
#sidebar {
dock: left;
width: 32;
height: 100%;
border-right: vkey $primary;
}
#bottombar {
dock: bottom;
height: 12;
width: 100%;
border-top: hkey $primary;
}
#hello {
border: wide $primary;
width: 40;
height: 16;
margin: 2 4;
}
#sidebar.hidden {
width: 0;
}
Static {
background: $panel;
color: $text;
content-align: center middle;
}
"""
def on_mount(self) -> None:
self.bind("t", "toggle_class('#sidebar', 'hidden')")
def compose(self):
yield Static("Sidebar", id="sidebar")
yield Vertical(
Static("Bottom bar", id="bottombar"),
Center(
Static("Hello World!", id="hello"),
),
)
app = CenterApp()

View File

@@ -1,10 +0,0 @@
Screen {
align: center middle;
}
Container {
width: 50;
height: 15;
background: $boost;
align: center middle;
}

View File

@@ -1,21 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Checkbox, Footer
class CheckboxApp(App):
BINDINGS = [("s", "switch", "Press switch"), ("d", "toggle_dark", "Dark mode")]
def compose(self) -> ComposeResult:
yield Footer()
yield Container(Checkbox())
def action_switch(self) -> None:
checkbox = self.query_one(Checkbox)
checkbox.value = not checkbox.value
app = CheckboxApp(css_path="check.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,23 +0,0 @@
Screen {
background: $surface;
}
Container {
height: auto;
background: $boost;
}
Panel {
height: auto;
background: $boost;
margin: 1 2;
}
Content {
background: $boost;
padding: 1 2;
margin: 1 2;
color: auto 95%;
}

View File

@@ -1,35 +0,0 @@
from textual.app import App
from textual.containers import Container
from textual.widgets import Header, Footer, Static
class Content(Static):
pass
class Panel(Container):
pass
class Panel2(Container):
pass
class DesignApp(App):
BINDINGS = [("d", "toggle_dark", "Toggle dark mode")]
def compose(self):
yield Header()
yield Footer()
yield Container(
Content("content"),
Panel(
Content("more content"),
Content("more content"),
),
)
app = DesignApp(css_path="design.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,52 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class DockApp(App):
def compose(self) -> ComposeResult:
self.screen.styles.layers = "base sidebar"
header = Static("Header", id="header")
header.styles.dock = "top"
header.styles.height = "3"
header.styles.background = "blue"
header.styles.color = "white"
header.styles.margin = 0
header.styles.align_horizontal = "center"
# header.styles.layer = "base"
header.styles.box_sizing = "border-box"
yield header
footer = Static("Footer")
footer.styles.dock = "bottom"
footer.styles.height = 1
footer.styles.background = "green"
footer.styles.color = "white"
yield footer
sidebar = Static("Sidebar", id="sidebar")
sidebar.styles.dock = "right"
sidebar.styles.width = 20
sidebar.styles.height = "100%"
sidebar.styles.background = "magenta"
# sidebar.styles.layer = "sidebar"
yield sidebar
for n, color in zip(range(5), ["red", "green", "blue", "yellow", "magenta"]):
thing = Static(f"Thing {n}", id=f"#thing{n}")
thing.styles.border = ("heavy", "rgba(0,0,0,0.2)")
thing.styles.background = f"{color} 20%"
thing.styles.height = 15
yield thing
app = DockApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,36 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class OrderApp(App):
CSS = """
Screen {
layout: center;
}
Static {
border: heavy white;
}
#one {
background: red;
width:20;
height: 30;
dock:left;
}
#two {
background: blue;
width:30;
height: 20;
dock:left;
}
"""
def compose(self) -> ComposeResult:
yield Static("One", id="one")
yield Static("Two", id="two")
app = OrderApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,8 +0,0 @@
App Static {
border: heavy white;
background: blue;
color: white;
height: 100%;
box-sizing: border-box;
}

View File

@@ -1,10 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class FillApp(App):
def compose(self) -> ComposeResult:
yield Static("Hello")
app = FillApp(css_path="fill.css")

View File

@@ -1,17 +0,0 @@
from textual.app import App
from textual.widgets import Header, Footer
class FooterApp(App):
def on_mount(self):
self.sub_title = "Header and footer example"
self.bind("b", "app.bell", description="Play the Bell")
self.bind("d", "dark", description="Toggle dark")
self.bind("f1", "app.bell", description="Hello World")
def action_dark(self):
self.dark = not self.dark
def compose(self):
yield Header()
yield Footer()

View File

@@ -1,19 +0,0 @@
from textual.app import App
from textual.widgets import Input
class InputApp(App):
CSS = """
Input {
width: 20;
}
"""
def compose(self):
yield Input("你123456789界", placeholder="Type something")
if __name__ == "__main__":
app = InputApp()
app.run()

View File

@@ -1,24 +0,0 @@
Screen {
background: lightcoral;
}
#left_pane {
background: red;
width: 30;
height: auto;
}
#middle_pane {
background: green;
width: 140;
}
#right_pane {
background: blue;
width: 30;
}
.box {
height: 5;
width: 15;
}

View File

@@ -1,43 +0,0 @@
from __future__ import annotations
from rich.console import RenderableType
from rich.panel import Panel
from textual import events
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal, Vertical
from textual.widget import Widget
class Box(Widget, can_focus=True):
DEFAULT_CSS = "#box {background: blue;}"
def render(self) -> RenderableType:
return Panel("Box")
class JustABox(App):
def compose(self) -> ComposeResult:
# yield Container(Box(classes="box"))
yield Horizontal(
Vertical(
Box(id="box1", classes="box"),
Box(id="box2", classes="box"),
id="left_pane",
),
id="horizontal",
)
def key_p(self):
for k, v in self.app.stylesheet.source.items():
print(k)
print(self.query_one("#horizontal").styles.layout)
async def on_key(self, event: events.Key) -> None:
await self.dispatch_key(event)
app = JustABox(css_path="just_a_box.css", watch_css=True)
if __name__ == "__main__":
app.run()

View File

@@ -1,39 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static
class OrderApp(App):
CSS = """
Screen {
layout: center;
}
Static {
border: heavy white;
}
#one {
background: red;
width:20;
height: 30;
}
#two {
background: blue;
width:30;
height: 20;
}
#three {
background: green;
width:40;
height:10
}
"""
def compose(self) -> ComposeResult:
yield Static("One", id="one")
yield Static("Two", id="two")
yield Static("Three", id="three")
app = OrderApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,35 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widget import Widget
from textual.widgets import Static
class MountWidget(Widget):
def on_mount(self) -> None:
print("Widget mounted")
class MountContainer(Container):
def compose(self) -> ComposeResult:
yield Container(MountWidget(id="bar"))
def on_mount(self) -> None:
bar = self.query_one("#bar")
print("MountContainer got", bar)
class MountApp(App):
def compose(self) -> ComposeResult:
yield MountContainer(id="foo")
def on_mount(self) -> None:
foo = self.query_one("#foo")
print("foo is", foo)
static = self.query_one("#bar")
print("App got", static)
if __name__ == "__main__":
app = MountApp()
app.run()

View File

@@ -1,21 +0,0 @@
Screen {
align: center middle;
}
#parent {
width: 32;
height: 8;
background: $panel;
}
#tag {
color: $text;
background: $success;
padding: 2 4;
width: auto;
offset: -8 -4;
}
#child {
background: red;
}

View File

@@ -1,14 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Static
class OffsetExample(App):
def compose(self) -> ComposeResult:
yield Vertical(Static("Child", id="child"), id="parent")
yield Static("Tag", id="tag")
app = OffsetExample(css_path="offset.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,27 +0,0 @@
import asyncio
from textual.app import App
from textual import events
from textual.widget import Widget
class OrderWidget(Widget, can_focus=True):
def on_key(self, event) -> None:
self.log("PRESS", event.key)
class OrderApp(App):
def compose(self):
yield OrderWidget()
async def on_mount(self):
async def send_keys():
self.query_one(OrderWidget).focus()
chars = ["tab", "enter", "h", "e", "l", "l", "o"]
for char in chars:
self.log("SENDING", char)
await self.post_message(events.Key(self, key=char))
self.set_timer(1, lambda: asyncio.create_task(send_keys()))
app = OrderApp()

View File

@@ -1,24 +0,0 @@
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Footer
class DefaultScreen(Screen):
BINDINGS = [("f", "foo", "FOO")]
def compose(self) -> ComposeResult:
yield Footer()
def action_foo(self) -> None:
self.app.bell()
class ScreenApp(App):
def on_mount(self) -> None:
self.push_screen(DefaultScreen())
app = ScreenApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,69 +0,0 @@
from textual.app import App, Screen, ComposeResult
from textual.widgets import Static, Footer, Pretty
class ModalScreen(Screen):
def compose(self) -> ComposeResult:
yield Pretty(self.app.screen_stack)
yield Footer()
def on_mount(self) -> None:
pretty = self.query_one("Pretty")
def on_screen_resume(self):
self.query_one(Pretty).update(self.app.screen_stack)
class NewScreen(Screen):
def compose(self):
yield Pretty(self.app.screen_stack)
yield Footer()
def on_screen_resume(self):
self.query_one(Pretty).update(self.app.screen_stack)
class ScreenApp(App):
DEFAULT_CSS = """
ScreenApp Screen {
background: #111144;
color: white;
}
ScreenApp ModalScreen {
background: #114411;
color: white;
}
ScreenApp Pretty {
height: auto;
content-align: center middle;
background: white 20%;
}
"""
def compose(self) -> ComposeResult:
yield Static("On Screen 1")
yield Footer()
def on_mount(self) -> None:
self.install_screen(NewScreen("Screen1"), name="1")
self.install_screen(NewScreen("Screen2"), name="2")
self.install_screen(NewScreen("Screen3"), name="3")
self.bind("1", "switch_screen('1')", description="Screen 1")
self.bind("2", "switch_screen('2')", description="Screen 2")
self.bind("3", "switch_screen('3')", description="Screen 3")
self.bind("s", "modal_screen", description="add screen")
self.bind("escape", "back", description="Go back")
def action_modal_screen(self) -> None:
self.push_screen(ModalScreen())
app = ScreenApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Focusable {
padding: 3 6;
background: blue 20%;
}
Focusable :focus {
border: solid red;
}

View File

@@ -1,20 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import Static, Footer
class Focusable(Static, can_focus=True):
pass
class ScreensFocusApp(App):
def compose(self) -> ComposeResult:
yield Focusable("App - one")
yield Focusable("App - two")
yield Focusable("App - three")
yield Focusable("App - four")
yield Footer()
app = ScreensFocusApp(css_path="screens_focus.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,9 +0,0 @@
Screen {
align: center middle;
}
#test {
border: solid white;
background: blue;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Container
from textual.widgets import Header, Footer
class ScrollApp(App):
BINDINGS = [("q", "quit", "QUIT")]
CSS_PATH = "scrollbug.css"
def compose(self) -> ComposeResult:
yield Header()
yield Container(id="test")
yield Footer()
app = ScrollApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,32 +0,0 @@
from rich.text import Text
from textual.app import App, ComposeResult
from textual.widgets import Static
text = "\n".join("FOO BAR bazz etc sdfsdf " * 20 for n in range(1000))
class Content(Static):
DEFAULT_CSS = """
Content {
width: auto;
}
"""
def render(self):
return Text(text, no_wrap=False)
class ScrollApp(App):
CSS = """
Screen {
overflow: auto;
}
"""
def compose(self) -> ComposeResult:
yield Content()
app = ScrollApp()
if __name__ == "__main__":
app.run()

View File

@@ -1,13 +0,0 @@
Screen {
overflow: auto;
}
Static {
background: blue 20%;
height: 100%;
margin: 2 4;
min-width: 80;
min-height: 40;
}

View File

@@ -1,15 +0,0 @@
from textual.app import App
from textual.widgets import Static
class Clickable(Static):
def on_click(self):
self.app.bell()
class SpacingApp(App):
def compose(self):
yield Static(id="2332")
app = SpacingApp(css_path="spacing.css")

View File

@@ -1,85 +0,0 @@
from textual.app import App, ComposeResult
from textual.widgets import DataTable
from rich.syntax import Syntax
from rich.table import Table
CODE = '''\
def loop_first_last(values: Iterable[T]) -> Iterable[tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
iter_values = iter(values)
try:
previous_value = next(iter_values)
except StopIteration:
return
first = True
for value in iter_values:
yield first, False, previous_value
first = False
previous_value = value
yield first, True, previous_value'''
test_table = Table(title="Star Wars Movies")
test_table.add_column("Released", style="cyan", no_wrap=True)
test_table.add_column("Title", style="magenta")
test_table.add_column("Box Office", justify="right", style="green")
test_table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
test_table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
test_table.add_row(
"Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889"
)
test_table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
class TableApp(App):
def compose(self) -> ComposeResult:
table = self.table = DataTable(id="data")
yield table
table.add_column("Foo")
table.add_column("Bar")
table.add_column("Baz")
table.add_column("Foo")
table.add_column("Bar")
table.add_column("Baz")
for n in range(200):
height = 1
row = [f"row [b]{n}[/b] col [i]{c}[/i]" for c in range(6)]
if n == 10:
row[1] = Syntax(
CODE,
"python",
theme="ansi_dark",
line_numbers=True,
indent_guides=True,
)
height = 13
if n == 30:
row[1] = test_table
height = 13
table.add_row(*row, height=height)
table.focus()
def on_mount(self):
self.bind("d", "toggle_dark")
self.bind("z", "toggle_zebra")
self.bind("x", "exit")
def action_toggle_dark(self) -> None:
self.app.dark = not self.app.dark
def action_toggle_zebra(self) -> None:
self.table.zebra_stripes = not self.table.zebra_stripes
def action_exit(self) -> None:
pass
app = TableApp()
if __name__ == "__main__":
print(app.run())

View File

@@ -1,23 +0,0 @@
Screen {
layout: grid;
grid-columns: 2fr 1fr 1fr;
grid-rows: 1fr 1fr;
grid-gutter: 1 2;
}
Static {
border: solid white;
background: blue 20%;
height: 100%;
width: 100%;
}
#foo {
row-span: 2;
}
#last {
column-span: 3;
margin: 1;
}

View File

@@ -1,19 +0,0 @@
from textual.app import App
from textual.widgets import Static
class TableLayoutApp(App):
def compose(self):
yield Static("foo", id="foo")
yield Static("bar")
yield Static("baz")
yield Static("foo")
yield Static("bar")
yield Static("baz", id="last")
app = TableLayoutApp(css_path="table_layout.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,16 +0,0 @@
from textual.app import App
from textual.containers import Container
from textual.widgets import DirectoryTree
class TreeApp(App):
def compose(self):
tree = DirectoryTree("~/projects")
yield Container(tree)
tree.focus()
app = TreeApp()
if __name__ == "__main__":
app.run()

View File

@@ -52,7 +52,7 @@ class Logger:
app = active_app.get()
except LookupError:
raise LoggerError("Unable to log without an active app.") from None
if not app.devtools.is_connected:
if app.devtools is None or not app.devtools.is_connected:
return
previous_frame = inspect.currentframe().f_back

6
src/textual/__main__.py Normal file
View File

@@ -0,0 +1,6 @@
from .demo import DemoApp
if __name__ == "__main__":
app = DemoApp()
app.run()

View File

@@ -86,6 +86,7 @@ class SimpleAnimation(Animation):
assert isinstance(
self.end_value, (int, float)
), f"`end_value` must be float, not {self.end_value!r}"
if self.end_value > self.start_value:
eased_factor = self.easing(factor)
value = (

View File

@@ -35,6 +35,9 @@ ANSI_SEQUENCES_KEYS: Mapping[str, Tuple[Keys, ...]] = {
"\x19": (Keys.ControlY,), # Control-Y (25)
"\x1a": (Keys.ControlZ,), # Control-Z
"\x1b": (Keys.Escape,), # Also Control-[
"\x1b\x1b": (
Keys.Escape,
), # Windows issues esc esc for a single press of escape key
"\x9b": (Keys.ShiftEscape,),
"\x1c": (Keys.ControlBackslash,), # Both Control-\ (also Ctrl-| )
"\x1d": (Keys.ControlSquareClose,), # Control-]

View File

@@ -12,7 +12,7 @@ from contextlib import redirect_stderr, redirect_stdout
from datetime import datetime
from pathlib import Path, PurePath
from time import perf_counter
from typing import Any, Generic, Iterable, Type, TypeVar, cast, Union
from typing import Any, Generic, Iterable, Type, TYPE_CHECKING, TypeVar, cast, Union
from weakref import WeakSet, WeakValueDictionary
from ._ansi_sequences import SYNC_END, SYNC_START
@@ -36,8 +36,6 @@ from .binding import Binding, Bindings
from .css.query import NoMatches
from .css.stylesheet import Stylesheet
from .design import ColorSystem
from .devtools.client import DevtoolsClient, DevtoolsConnectionError, DevtoolsLog
from .devtools.redirect_output import StdoutRedirector
from .dom import DOMNode
from .driver import Driver
from .drivers.headless_driver import HeadlessDriver
@@ -51,6 +49,10 @@ from .renderables.blank import Blank
from .screen import Screen
from .widget import AwaitMount, Widget
if TYPE_CHECKING:
from .devtools.client import DevtoolsClient
PLATFORM = platform.system()
WINDOWS = PLATFORM == "Windows"
@@ -123,7 +125,6 @@ class App(Generic[ReturnType], DOMNode):
Args:
driver_class (Type[Driver] | None, optional): Driver class or ``None`` to auto-detect. Defaults to None.
title (str | None, optional): Title of the application. If ``None``, the title is set to the name of the ``App`` subclass. Defaults to ``None``.
css_path (str | PurePath | None, optional): Path to CSS or ``None`` for no CSS file. Defaults to None.
watch_css (bool, optional): Watch CSS for changes. Defaults to False.
"""
@@ -140,18 +141,18 @@ class App(Generic[ReturnType], DOMNode):
"""
SCREENS: dict[str, Screen] = {}
_BASE_PATH: str | None = None
CSS_PATH: CSSPathType = None
TITLE: str | None = None
SUB_TITLE: str | None = None
title: Reactive[str] = Reactive("Textual")
title: Reactive[str] = Reactive("")
sub_title: Reactive[str] = Reactive("")
dark: Reactive[bool] = Reactive(True)
def __init__(
self,
driver_class: Type[Driver] | None = None,
title: str | None = None,
css_path: CSSPathType = None,
watch_css: bool = False,
):
@@ -190,10 +191,10 @@ class App(Generic[ReturnType], DOMNode):
self._animator = Animator(self)
self._animate = self._animator.bind(self)
self.mouse_position = Offset(0, 0)
if title is None:
self.title = f"{self.__class__.__name__}"
else:
self.title = title
self.title = (
self.TITLE if self.TITLE is not None else f"{self.__class__.__name__}"
)
self.sub_title = self.SUB_TITLE if self.SUB_TITLE is not None else ""
self._logger = Logger(self._log)
@@ -221,7 +222,16 @@ class App(Generic[ReturnType], DOMNode):
] = WeakValueDictionary()
self._installed_screens.update(**self.SCREENS)
self.devtools = DevtoolsClient()
self.devtools: DevtoolsClient | None = None
if "devtools" in self.features:
try:
from .devtools.client import DevtoolsClient
except ImportError:
# Dev dependencies not installed
pass
else:
self.devtools = DevtoolsClient()
self._return_value: ReturnType | None = None
self.css_monitor = (
@@ -267,16 +277,6 @@ class App(Generic[ReturnType], DOMNode):
on_complete=on_complete,
)
@property
def devtools_enabled(self) -> bool:
"""Check if devtools are enabled.
Returns:
bool: True if devtools are enabled.
"""
return "devtools" in self.features
@property
def debug(self) -> bool:
"""Check if debug mode is enabled.
@@ -448,15 +448,18 @@ class App(Generic[ReturnType], DOMNode):
verbosity (int, optional): Verbosity level 0-3. Defaults to 1.
"""
if not self.devtools.is_connected:
devtools = self.devtools
if devtools is None or not devtools.is_connected:
return
if verbosity.value > LogVerbosity.NORMAL.value and not self.devtools.verbose:
if verbosity.value > LogVerbosity.NORMAL.value and not devtools.verbose:
return
try:
from .devtools.client import DevtoolsLog
if len(objects) == 1 and not kwargs:
self.devtools.log(
devtools.log(
DevtoolsLog(objects, caller=_textual_calling_frame),
group,
verbosity,
@@ -468,7 +471,7 @@ class App(Generic[ReturnType], DOMNode):
f"{key}={value!r}" for key, value in kwargs.items()
)
output = f"{output} {key_values}" if output else key_values
self.devtools.log(
devtools.log(
DevtoolsLog(output, caller=_textual_calling_frame),
group,
verbosity,
@@ -480,7 +483,7 @@ class App(Generic[ReturnType], DOMNode):
"""Action to toggle dark mode."""
self.dark = not self.dark
def action_screenshot(self, filename: str | None, path: str = "~/") -> None:
def action_screenshot(self, filename: str | None = None, path: str = "./") -> None:
"""Save an SVG "screenshot". This action will save an SVG file containing the current contents of the screen.
Args:
@@ -612,6 +615,13 @@ class App(Generic[ReturnType], DOMNode):
print(f"(pause {wait_ms}ms)")
await asyncio.sleep(float(wait_ms) / 1000)
else:
if len(key) == 1 and not key.isalnum():
key = (
unicodedata.name(key)
.lower()
.replace("-", "_")
.replace(" ", "_")
)
original_key = REPLACED_KEYS.get(key, key)
try:
char = unicodedata.lookup(
@@ -620,7 +630,8 @@ class App(Generic[ReturnType], DOMNode):
except KeyError:
char = key if len(key) == 1 else None
print(f"press {key!r} (char={char!r})")
driver.send_event(events.Key(self, key, char))
key_event = events.Key(self, key, char)
driver.send_event(key_event)
await asyncio.sleep(0.01)
await app._animator.wait_for_idle()
@@ -978,7 +989,9 @@ class App(Generic[ReturnType], DOMNode):
) -> None:
self._set_active()
if self.devtools_enabled:
if self.devtools is not None:
from .devtools.client import DevtoolsConnectionError
try:
await self.devtools.connect()
self.log.system(f"Connected to devtools ( {self.devtools.url} )")
@@ -1066,10 +1079,21 @@ class App(Generic[ReturnType], DOMNode):
if self.is_headless:
await run_process_messages()
else:
redirector = StdoutRedirector(self.devtools)
with redirect_stderr(redirector):
with redirect_stdout(redirector): # type: ignore
await run_process_messages()
if self.devtools is not None:
devtools = self.devtools
assert devtools is not None
from .devtools.redirect_output import StdoutRedirector
redirector = StdoutRedirector(devtools)
with redirect_stderr(redirector):
with redirect_stdout(redirector): # type: ignore
await run_process_messages()
else:
null_file = _NullFile()
with redirect_stderr(null_file):
with redirect_stdout(null_file):
await run_process_messages()
finally:
driver.stop_application_mode()
except Exception as error:
@@ -1077,7 +1101,7 @@ class App(Generic[ReturnType], DOMNode):
finally:
self._running = False
self._print_error_renderables()
if self.devtools.is_connected:
if self.devtools is not None and self.devtools.is_connected:
await self._disconnect_devtools()
async def _pre_process(self) -> None:
@@ -1177,7 +1201,8 @@ class App(Generic[ReturnType], DOMNode):
self._registry.discard(widget)
async def _disconnect_devtools(self):
await self.devtools.disconnect()
if self.devtools is not None:
await self.devtools.disconnect()
def _start_widget(self, parent: Widget, widget: Widget) -> None:
"""Start a widget (run it's task) so that it can receive messages.
@@ -1345,6 +1370,7 @@ class App(Generic[ReturnType], DOMNode):
Returns:
bool: True if the event has handled.
"""
print("ACTION", action, default_namespace)
if isinstance(action, str):
target, params = actions.parse(action)
else:
@@ -1353,7 +1379,7 @@ class App(Generic[ReturnType], DOMNode):
if "." in target:
destination, action_name = target.split(".", 1)
if destination not in self._action_targets:
raise ActionError("Action namespace {destination} is not known")
raise ActionError(f"Action namespace {destination} is not known")
action_target = getattr(self, destination)
implicit_destination = True
else:

Some files were not shown because too many files have changed in this diff Show More