Merge pull request #787 from Textualize/docs-events

Docs events
This commit is contained in:
Will McGugan
2022-09-20 14:34:45 +01:00
committed by GitHub
71 changed files with 1033 additions and 1414 deletions

View File

@@ -0,0 +1,48 @@
from textual.app import App, ComposeResult
from textual.color import Color
from textual.message import Message, MessageTarget
from textual.widgets import Static
class ColorButton(Static):
"""A color button."""
class Selected(Message):
"""Color selected message."""
def __init__(self, sender: MessageTarget, color: Color) -> None:
self.color = color
super().__init__(sender)
def __init__(self, color: Color) -> None:
self.color = color
super().__init__()
def on_mount(self) -> None:
self.styles.margin = (1, 2)
self.styles.content_align = ("center", "middle")
self.styles.background = Color.parse("#ffffff33")
self.styles.border = ("tall", self.color)
async def on_click(self) -> None:
# The emit method sends an event to a widget's parent
await self.emit(self.Selected(self, self.color))
def render(self) -> str:
return str(self.color)
class ColorApp(App):
def compose(self) -> ComposeResult:
yield ColorButton(Color.parse("#008080"))
yield ColorButton(Color.parse("#808000"))
yield ColorButton(Color.parse("#E9967A"))
yield ColorButton(Color.parse("#121212"))
def on_color_button_selected(self, message: ColorButton.Selected) -> None:
self.screen.styles.animate("background", message.color, duration=0.5)
app = ColorApp()
if __name__ == "__main__":
app.run()

View File

@@ -0,0 +1,29 @@
Screen {
background: $panel;
}
TextInput {
dock: top;
border: tall $background;
width: 100%;
height: 1;
padding: 0 1;
margin: 1 1 0 1;
background: $boost;
}
TextInput:focus {
border: tall $accent;
}
#results {
width: auto;
min-height: 100%;
}
#results-container {
background: $background 50%;
overflow: auto;
margin: 1 2;
height: 100%;
}

View File

@@ -0,0 +1,43 @@
import asyncio
try:
import httpx
except ImportError:
raise ImportError("Please install httpx with 'pip install httpx' ")
from rich.json import JSON
from textual.app import App, ComposeResult
from textual.layout import Vertical
from textual.widgets import Static, TextInput
class DictionaryApp(App):
"""Searches ab dictionary API as-you-type."""
def compose(self) -> ComposeResult:
yield TextInput(placeholder="Search for a word")
yield Vertical(Static(id="results", fluid=False), id="results-container")
async def on_text_input_changed(self, message: TextInput.Changed) -> None:
"""A coroutine to handle a text changed message."""
if message.value:
# Look up the word in the background
asyncio.create_task(self.lookup_word(message.value))
else:
# Clear the results
self.query_one("#results", Static).update()
async def lookup_word(self, word: str) -> None:
"""Looks up a word."""
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
async with httpx.AsyncClient() as client:
results = (await client.get(url)).text
if word == self.query_one(TextInput).value:
self.query_one("#results", Static).update(JSON(results))
app = DictionaryApp(css_path="dictionary.css")
if __name__ == "__main__":
app.run()

View File

@@ -1,19 +0,0 @@
from textual.app import App
from textual.widgets import Button
class ButtonApp(App):
DEFAULT_CSS = """
Button {
width: 100%;
}
"""
def compose(self):
yield Button("Lights off")
def on_button_pressed(self, event):
self.dark = not self.dark
self.bell()
event.button.label = "Lights ON" if self.dark else "Lights OFF"

View File

@@ -2,7 +2,7 @@ All you need to get started building Textual apps.
## Requirements ## Requirements
Textual requires Python 3.7 or later. Textual runs on Linux, MacOS, Windows and probably any OS where Python also runs. Textual requires Python 3.7 or later (if you have a choice, pick the most recent Python). Textual runs on Linux, MacOS, Windows and probably any OS where Python also runs.
!!! info inline end "Your platform" !!! info inline end "Your platform"

View File

@@ -1,13 +1,189 @@
## Events # Events and Messages
TODO: events docs We've used event handler methods in many of the examples in this guide. This chapter explores [events](../events/index.md) and messages (see below) in more detail.
- What are events ## Messages
- Handling events
- Auto calling base classes Events are a particular kind of *message* sent by Textual in response to input and other state changes. Events are reserved for use by Textual but you can also create custom messages for the purpose of coordinating between widgets in your app.
- Event bubbling
- Posting / emitting events More on that later, but for now keep in mind that events are also messages, and anything that is true of messages is true of events.
## Message Queue
Every [App][textual.app.App] and [Widget][textual.widget.Widget] object contains a *message queue*. You can think of a message queue as orders at a restaurant. The chef takes an order and makes the dish. Orders that arrive while the chef is cooking are placed in a line. When the chef has finished a dish they pick up the next order in the line.
Textual processes messages in the same way. Messages are picked off a queue and processed (cooked) by a handler method. This guarantees messages and events are processed even if your code can not handle them right way.
This processing of messages is done within an asyncio Task which is started when you mount the widget. The task monitors a queue for new messages and dispatches them to the appropriate handler when they arrive.
!!! tip
The FastAPI docs have an [excellent introduction](https://fastapi.tiangolo.com/async/) to Python async programming.
By way of an example, let's consider what happens if you were to type "Text" in to a `TextInput` widget. When you hit the ++t++ key, Textual creates a [key][textual.events.Key] event and sends it to the widget's message queue. Ditto for ++e++, ++x++, and ++t++.
The widget's task will pick the first message from the queue (a key event for the ++t++ key) and call the `on_key` method with the event as the first argument. In other words it will call `TextInput.on_key(event)`, which updates the display to show the new letter.
<div class="excalidraw"> <div class="excalidraw">
--8<-- "docs/images/test.excalidraw.svg" --8<-- "docs/images/events/queue.excalidraw.svg"
</div> </div>
When the `on_key` method returns, Textual will get the next event from the the queue and repeat the process for the remaining keys. At some point the queue will be empty and the widget is said to be in an *idle* state.
!!! note
This example illustrates a point, but a typical app will be fast enough to have processed a key before the next event arrives. So it is unlikely you will have so many key events in the message queue.
<div class="excalidraw">
--8<-- "docs/images/events/queue2.excalidraw.svg"
</div>
## Default behaviors
You may be familiar with Python's [super](https://docs.python.org/3/library/functions.html#super) function to call a function defined in a base class. You will not have to use this in event handlers as Textual will automatically call handler methods defined in a widget's base class(es).
For instance, let's say we are building the classic game of Pong and we have written a `Paddle` widget which extends [Static][textual.widgets.Static]. When a [Key][textual.events.Key] event arrives, Textual calls `Paddle.on_key` (to respond to ++left++ and ++right++ keys), then `Static.on_key`, and finally `Widget.on_key`.
### Preventing default behaviors
If you don't want this behavior you can call [prevent_default()][textual.message.Message.prevent_default] on the event object. This tells Textual not to call any more handlers on base classes.
!!! warning
You won't need `prevent_default` very often. Be sure to know what your base classes do before calling it, or you risk disabling some core features builtin to Textual.
## Bubbling
Messages have a `bubble` attribute. If this is set to `True` then events will be sent to a widget's parent after processing. Input events typically bubble so that a widget will have the opportunity to respond to input events if they aren't handled by their children.
The following diagram shows an (abbreviated) DOM for a UI with a container and two buttons. With the "No" button [focused](#), it will receive the key event first.
<div class="excalidraw">
--8<-- "docs/images/events/bubble1.excalidraw.svg"
</div>
After Textual calls `Button.on_key` the event _bubbles_ to the button's parent and will call `Container.on_key` (if it exists).
<div class="excalidraw">
--8<-- "docs/images/events/bubble2.excalidraw.svg"
</div>
As before, the event bubbles to it's parent (the App class).
<div class="excalidraw">
--8<-- "docs/images/events/bubble3.excalidraw.svg"
</div>
The App class is always the root of the DOM, so there is no where for the event to bubble to.
### Stopping bubbling
Event handlers may stop this bubble behavior by calling the [stop()][textual.message.Message.stop] method on the event or message. You might want to do this if a widget has responded to the event in an authoritative way. For instance when a text input widget responds to a key event it stops the bubbling so that the key doesn't also invoke a key binding.
## Custom messages
You can create custom messages for your application that may be used in the same way as events (recall that events are simply messages reserved for use by Textual).
The most common reason to do this is if you are building a custom widget and you need to inform a parent widget about a state change.
Let's look at an example which defines a custom message. The following example creates color buttons which&mdash;when clicked&mdash;send a custom message.
=== "custom01.py"
```python title="custom01.py" hl_lines="10-15 27-29 42-43"
--8<-- "docs/examples/events/custom01.py"
```
=== "Output"
```{.textual path="docs/examples/events/custom01.py"}
```
Note the custom message class which extends [Message][textual.message.Message]. The constructor stores a [color][textual.color.Color] object which handler methods will be able to inspect.
The message class is defined within the widget class itself. This is not strictly required but recommended, for these reasons:
- It reduces the amount of imports. If you import `ColorButton`, you have access to the message class via `ColorButton.Selected`.
- It creates a namespace for the handler. So rather than `on_selected`, the handler name becomes `on_color_button_selected`. This makes it less likely that your chosen name will clash with another message.
## Sending events
In the previous example we used [emit()][textual.message_pump.MessagePump.emit] to send an event to it's parent. We could also have used [emit_no_wait()][textual.message_pump.MessagePump.emit_no_wait] for non async code. Sending messages in this way allows you to write custom widgets without needing to know in what context they will be used.
There are other ways of sending (posting) messages, which you may need to use less frequently.
- [post_message][textual.message_pump.MessagePump.post_message] To post a message to a particular widget.
- [post_message_no_wait][textual.message_pump.MessagePump.post_message_no_wait] The non-async version of `post_message`.
## Message handlers
Most of the logic in a Textual app will be written in message handlers. Let's explore handlers in more detail.
### Handler naming
Textual uses the following scheme to map messages classes on to a Python method.
- Start with `"on_"`.
- Add the messages namespace (if any) converted from CamelCase to snake_case plus an underscore `"_"`
- Add the name of the class converted from CamelCase to snake_case.
<div class="excalidraw">
--8<-- "docs/images/events/naming.excalidraw.svg"
</div>
### Handler arguments
Message handler methods can be written with or without a positional argument. If you add a positional argument, Textual will call the handler with the event object. The following handler (taken from custom01.py above) contains a `message` parameter. The body of the code makes use of the message to set a preset color.
```python
def on_color_button_selected(self, message: ColorButton.Selected) -> None:
self.screen.styles.animate("background", message.color, duration=0.5)
```
If the body of your handler doesn't require any information in the message you can omit it from the method signature. If we just want to play a bell noise when the button is clicked, we could write our handler like this:
```python
def on_color_button_selected(self) -> None:
self.app.bell()
```
This pattern is a convenience that saves writing out a parameter that may not be used.
### Async handlers
Message handlers may be coroutines. If you prefix your handlers with the `async` keyword, Textual will `await` them. This lets your handler use the `await` keyword for asynchronous APIs.
If your event handlers are coroutines it will allow multiple events to be processed concurrently, but bear in mind an individual widget (or app) will not be able to pick up a new message from its message queue until the handler has returned. This is rarely a problem in practice; as long has handlers return within a few milliseconds the UI will remain responsive. But slow handlers might make your app hard to use.
!!! info
To re-use the chef analogy, if an order comes in for beef wellington (which takes a while to cook), orders may start to pile up and customers may have to wait for their meal. The solution would be to have another chef work on the wellington while the first chef picks up new orders.
Network access is a common cause of slow handlers. If you try to retrieve a file from the internet, the message handler may take anything up to a few seconds to return, which would prevent the widget or app from updating during that time. The solution is to launch a new asyncio task to do the network task in the background.
Let's look at an example which looks up word definitions from an [api](https://dictionaryapi.dev/) as you type.
!!! note
You will need to install [httpx](https://www.python-httpx.org/) with `pip install httpx` to run this example.
=== "dictionary.py"
```python title="dictionary.py" hl_lines="26"
--8<-- "docs/examples/events/dictionary.py"
```
=== "dictionary.css"
```python title="dictionary.css"
--8<-- "docs/examples/events/dictionary.css"
```
=== "Output"
```{.textual path="docs/examples/events/dictionary.py" press="tab,t,e,x,t,_,_,_,_,_,_,_,_,_,_,_"}
```
Note the highlighted line in the above code which calls `asyncio.create_task` to run coroutine in the background. Without this you would find typing in to the text box to be unresponsive.

View File

@@ -206,7 +206,7 @@ The same units may also be used to set limits on a dimension. The following styl
- [min-width](../styles/min_width.md) sets a minimum width. - [min-width](../styles/min_width.md) sets a minimum width.
- [max-width](../styles/max_width.md) sets a maximum width. - [max-width](../styles/max_width.md) sets a maximum width.
- [min-height](../styles/min_height.md) sets a minimum height. - [min-height](../styles/min_height.md) sets a minimum height.
- [max-height](../styles/max_hright.md) sets a maximum height. - [max-height](../styles/max_height.md) sets a maximum height.
### Padding ### Padding

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 24 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 34 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 42 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 55 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 90 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 48 KiB

View File

@@ -59,7 +59,7 @@ Textual is a framework for building applications that run within your terminal.
<hr> <hr>
```{.textual path="examples/calculator.py" columns=100 lines=40} ```{.textual path="examples/calculator.py" columns=100 lines=41 press="3,.,1,4,5,9,2,_,_"}
``` ```
@@ -70,6 +70,9 @@ Textual is a framework for building applications that run within your terminal.
```{.textual path="docs/examples/tutorial/stopwatch.py" press="tab,enter,_,_"} ```{.textual path="docs/examples/tutorial/stopwatch.py" press="tab,enter,_,_"}
``` ```
```{.textual path="docs/examples/guide/layout/combining_layouts.py"}
```
```{.textual path="docs/examples/app/widgets01.py"} ```{.textual path="docs/examples/app/widgets01.py"}
``` ```

View File

@@ -0,0 +1 @@
::: textual.message.Message

1
docs/reference/static.md Normal file
View File

@@ -0,0 +1 @@
::: textual.widgets.Static

View File

@@ -6,7 +6,7 @@ By the end of this page you should have a solid understanding of app development
!!! quote !!! quote
I've always thought the secret sauce in making a popular framework is for it to be fun. If you want people to build things, make it fun.
&mdash; **Will McGugan** (creator of Rich and Textual) &mdash; **Will McGugan** (creator of Rich and Textual)
@@ -329,7 +329,7 @@ The `on_button_pressed` method is an *event handler*. Event handlers are methods
If you run "stopwatch04.py" now you will be able to toggle between the two states by clicking the first button: If you run "stopwatch04.py" now you will be able to toggle between the two states by clicking the first button:
```{.textual path="docs/examples/tutorial/stopwatch04.py" title="stopwatch04.py" press="tab,tab,tab,enter,_,_"} ```{.textual path="docs/examples/tutorial/stopwatch04.py" title="stopwatch04.py" press="tab,tab,tab,_,enter,_,_,_"}
``` ```
## Reactive attributes ## Reactive attributes

View File

@@ -16,10 +16,6 @@ CodeBrowser.-show-tree #tree-view {
background: $surface; background: $surface;
} }
CodeBrowser{
background: $background;
}
DirectoryTree { DirectoryTree {
padding-right: 1; padding-right: 1;
padding-right: 1; padding-right: 1;

View File

@@ -92,16 +92,20 @@ nav:
- "widgets/text_input.md" - "widgets/text_input.md"
- "widgets/tree_control.md" - "widgets/tree_control.md"
- Reference: - Reference:
- "reference/index.md"
- "reference/app.md" - "reference/app.md"
- "reference/button.md" - "reference/button.md"
- "reference/color.md" - "reference/color.md"
- "reference/dom_node.md" - "reference/dom_node.md"
- "reference/events.md" - "reference/events.md"
- "reference/geometry.md" - "reference/geometry.md"
- "reference/index.md"
- "reference/message_pump.md" - "reference/message_pump.md"
- "reference/timer.md" - "reference/message.md"
- "reference/query.md" - "reference/query.md"
- "reference/reactive.md"
- "reference/screen.md"
- "reference/static.md"
- "reference/timer.md"
- "reference/widget.md" - "reference/widget.md"

View File

@@ -1,11 +0,0 @@
These examples probably don't rub. We will be back-porting them to the examples dir
# Examples
Run any of these examples to demonstrate a Textual features.
The example code will generate a log file called "textual.log". Tail this file to gain insight in to what Textual is doing.
```
tail -f textual
```

View File

@@ -1,38 +0,0 @@
from textual.app import App
from textual.reactive import Reactive
from textual.widgets import Footer, Placeholder
class SmoothApp(App):
"""Demonstrates smooth animation. Press 'b' to see it in action."""
async def on_load(self) -> None:
"""Bind keys here."""
await self.bind("b", "toggle_sidebar", "Toggle sidebar")
await self.bind("q", "quit", "Quit")
show_bar = Reactive(False)
def watch_show_bar(self, show_bar: bool) -> None:
"""Called when show_bar changes."""
self.bar.animate("layout_offset_x", 0 if show_bar else -40)
def action_toggle_sidebar(self) -> None:
"""Called when user hits 'b' key."""
self.show_bar = not self.show_bar
async def on_mount(self) -> None:
"""Build layout here."""
footer = Footer()
self.bar = Placeholder(name="left")
await self.screen.dock(footer, edge="bottom")
await self.screen.dock(Placeholder(), Placeholder(), edge="top")
await self.screen.dock(self.bar, edge="left", size=40, z=1)
self.bar.layout_offset_x = -40
# self.set_timer(10, lambda: self.action("quit"))
SmoothApp.run(log_path="textual.log")

View File

@@ -1,33 +0,0 @@
from rich.table import Table
from textual import events
from textual.app import App
from textual.widgets import ScrollView
class MyApp(App):
"""An example of a very simple Textual App"""
async def on_load(self, event: events.Load) -> None:
await self.bind("q", "quit", "Quit")
async def on_mount(self, event: events.Mount) -> None:
self.body = body = ScrollView(auto_width=True)
await self.screen.dock(body)
async def add_content():
table = Table(title="Demo")
for i in range(20):
table.add_column(f"Col {i + 1}", style="magenta")
for i in range(100):
table.add_row(*[f"cell {i},{j}" for j in range(20)])
await body.update(table)
await self.call_later(add_content)
MyApp.run(title="Simple App", log_path="textual.log")

View File

@@ -1,59 +0,0 @@
Screen {
/* text-background: #212121; */
}
#borders {
layout: vertical;
background: #212121;
overflow-y: scroll;
}
Lorem.border {
height: 12;
margin: 2 4;
background: #303f9f;
}
Lorem.round {
border: round #8bc34a;
}
Lorem.solid {
border: solid #8bc34a;
}
Lorem.double {
border: double #8bc34a;
}
Lorem.dashed {
border: dashed #8bc34a;
}
Lorem.heavy {
border: heavy #8bc34a;
}
Lorem.inner {
border: inner #8bc34a;
}
Lorem.outer {
border: outer #8bc34a;
}
Lorem.hkey {
border: hkey #8bc34a;
}
Lorem.vkey {
border: vkey #8bc34a;
}
Lorem.tall {
border: tall #8bc34a;
}
Lorem.wide {
border: wide #8bc34a;
}

View File

@@ -1,57 +0,0 @@
from rich.padding import Padding
from rich.style import Style
from rich.text import Text
from textual.app import App
from textual.renderables.gradient import VerticalGradient
from textual.widget import Widget
lorem = Text.from_markup(
"""[#C5CAE9]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 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. """,
justify="full",
)
class Lorem(Widget):
def render(self) -> Text:
return Padding(lorem, 1)
class Background(Widget):
def render(self):
return VerticalGradient("#212121", "#212121")
class BordersApp(App):
"""Sandbox application used for testing/development by Textual developers"""
def on_load(self):
self.bind("q", "quit", "Quit")
def on_mount(self):
"""Build layout here."""
borders = [
Lorem(classes={"border", border})
for border in (
"round",
"solid",
"double",
"dashed",
"heavy",
"inner",
"outer",
"hkey",
"vkey",
"tall",
"wide",
)
]
borders_view = Background(*borders)
borders_view.show_vertical_scrollbar = True
self.mount(borders=borders_view)
app = BordersApp(css_path="borders.css", log_path="textual.log")
app.run()

View File

@@ -1,216 +0,0 @@
"""
A Textual app to create a fully working calculator, modelled after MacOS Calculator.
"""
from decimal import Decimal
from rich.align import Align
from rich.console import Console, ConsoleOptions, RenderResult, RenderableType
from rich.padding import Padding
from rich.style import Style
from rich.text import Text
from textual.app import App
from textual.reactive import Reactive
from textual.views import GridView
from textual.widget import Widget
from textual.widgets import Button
try:
from pyfiglet import Figlet
except ImportError:
print("Please install pyfiglet to run this example")
raise
class FigletText:
"""A renderable to generate figlet text that adapts to fit the container."""
def __init__(self, text: str) -> None:
self.text = text
def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
"""Build a Rich renderable to render the Figlet text."""
size = min(options.max_width / 2, options.max_height)
if size < 4:
yield Text(self.text, style="bold")
else:
if size < 7:
font_name = "mini"
elif size < 8:
font_name = "small"
elif size < 10:
font_name = "standard"
else:
font_name = "big"
font = Figlet(font=font_name, width=options.max_width)
yield Text(font.renderText(self.text).rstrip("\n"), style="bold")
class Numbers(Widget):
"""The digital display of the calculator."""
value = Reactive("0")
def render(self) -> RenderableType:
"""Build a Rich renderable to render the calculator display."""
return Padding(
Align.right(FigletText(self.value), vertical="middle"),
(0, 1),
style="white on rgb(51,51,51)",
)
class Calculator(GridView):
"""A working calculator app."""
DARK = "white on rgb(51,51,51)"
LIGHT = "black on rgb(165,165,165)"
YELLOW = "white on rgb(255,159,7)"
BUTTON_STYLES = {
"AC": LIGHT,
"C": LIGHT,
"+/-": LIGHT,
"%": LIGHT,
"/": YELLOW,
"X": YELLOW,
"-": YELLOW,
"+": YELLOW,
"=": YELLOW,
}
display = Reactive("0")
show_ac = Reactive(True)
def watch_display(self, value: str) -> None:
"""Called when self.display is modified."""
# self.numbers is a widget that displays the calculator result
# Setting the attribute value changes the display
# This allows us to write self.display = "100" to update the display
self.numbers.value = value
def compute_show_ac(self) -> bool:
"""Compute show_ac reactive value."""
# Condition to show AC button over C
return self.value in ("", "0") and self.display == "0"
def watch_show_ac(self, show_ac: bool) -> None:
"""When the show_ac attribute change we need to update the buttons."""
# Show AC and hide C or vice versa
self.c.display = not show_ac
self.ac.display = show_ac
def on_mount(self) -> None:
"""Event when widget is first mounted (added to a parent view)."""
# Attributes to store the current calculation
self.left = Decimal("0")
self.right = Decimal("0")
self.value = ""
self.operator = "+"
# The calculator display
self.numbers = Numbers()
self.numbers.style_border = "bold"
def make_button(text: str, style: str) -> Button:
"""Create a button with the given Figlet label."""
return Button(FigletText(text), style=style, name=text)
# Make all the buttons
self.buttons = {
name: make_button(name, self.BUTTON_STYLES.get(name, self.DARK))
for name in "+/-,%,/,7,8,9,X,4,5,6,-,1,2,3,+,.,=".split(",")
}
# Buttons that have to be treated specially
self.zero = make_button("0", self.DARK)
self.ac = make_button("AC", self.LIGHT)
self.c = make_button("C", self.LIGHT)
self.c.display = False
# Set basic grid settings
self.grid.set_gap(2, 1)
self.grid.set_gutter(1)
self.grid.set_align("center", "center")
# Create rows / columns / areas
self.grid.add_column("col", max_size=30, repeat=4)
self.grid.add_row("numbers", max_size=15)
self.grid.add_row("row", max_size=15, repeat=5)
self.grid.add_areas(
clear="col1,row1",
numbers="col1-start|col4-end,numbers",
zero="col1-start|col2-end,row5",
)
# Place out widgets in to the layout
self.grid.place(clear=self.c)
self.grid.place(
*self.buttons.values(), clear=self.ac, numbers=self.numbers, zero=self.zero
)
def handle_button_pressed(self, message: ButtonPressed) -> None:
"""A message sent by the button widget"""
assert isinstance(message.sender, Button)
button_name = message.sender.name
def do_math() -> None:
"""Does the math: LEFT OPERATOR RIGHT"""
self.log(self.left, self.operator, self.right)
try:
if self.operator == "+":
self.left += self.right
elif self.operator == "-":
self.left -= self.right
elif self.operator == "/":
self.left /= self.right
elif self.operator == "X":
self.left *= self.right
self.display = str(self.left)
self.value = ""
self.log("=", self.left)
except Exception:
self.display = "Error"
if button_name.isdigit():
self.display = self.value = self.value.lstrip("0") + button_name
elif button_name == "+/-":
self.display = self.value = str(Decimal(self.value or "0") * -1)
elif button_name == "%":
self.display = self.value = str(Decimal(self.value or "0") / Decimal(100))
elif button_name == ".":
if "." not in self.value:
self.display = self.value = (self.value or "0") + "."
elif button_name == "AC":
self.value = ""
self.left = self.right = Decimal(0)
self.operator = "+"
self.display = "0"
elif button_name == "C":
self.value = ""
self.display = "0"
elif button_name in ("+", "-", "/", "X"):
self.right = Decimal(self.value or "0")
do_math()
self.operator = button_name
elif button_name == "=":
if self.value:
self.right = Decimal(self.value)
do_math()
class CalculatorApp(App):
"""The Calculator Application"""
async def on_mount(self) -> None:
"""Mount the calculator widget."""
await self.screen.dock(Calculator())
CalculatorApp.run(title="Calculator Test", log_path="textual.log")

View File

@@ -1,70 +0,0 @@
import os
import sys
from rich.console import RenderableType
from rich.syntax import Syntax
from rich.traceback import Traceback
from textual.app import App
from textual.widgets import Header, Footer, FileClick, ScrollView, DirectoryTree
class MyApp(App):
"""An example of a very simple Textual App"""
async def on_load(self) -> None:
"""Sent before going in to application mode."""
# Bind our basic keys
await self.bind("b", "view.toggle('sidebar')", "Toggle sidebar")
await self.bind("q", "quit", "Quit")
# Get path to show
try:
self.path = sys.argv[1]
except IndexError:
self.path = os.path.abspath(
os.path.join(os.path.basename(__file__), "../../")
)
async def on_mount(self) -> None:
"""Call after terminal goes in to application mode"""
# Create our widgets
# In this a scroll view for the code and a directory tree
self.body = ScrollView()
self.directory = DirectoryTree(self.path, "Code")
# Dock our widgets
await self.screen.dock(Header(), edge="top")
await self.screen.dock(Footer(), edge="bottom")
# Note the directory is also in a scroll view
await self.screen.dock(
ScrollView(self.directory), edge="left", size=48, name="sidebar"
)
await self.screen.dock(self.body, edge="top")
async def handle_file_click(self, message: FileClick) -> None:
"""A message sent by the directory tree when a file is clicked."""
syntax: RenderableType
try:
# Construct a Syntax object for the path in the message
syntax = Syntax.from_path(
message.path,
line_numbers=True,
word_wrap=True,
indent_guides=True,
theme="monokai",
)
except Exception:
# Possibly a binary file
# For demonstration purposes we will show the traceback
syntax = Traceback(theme="monokai", width=None, show_locals=True)
self.app.sub_title = os.path.basename(message.path)
await self.body.update(syntax)
# Run our app class
MyApp.run(title="Code Viewer", log_path="textual.log")

View File

@@ -1,10 +0,0 @@
header blue white on #173f5f
sidebar #09312e on #3caea3
sidebar border #09312e
content blue white #20639b
content border #0f2b41
footer border #0f2b41
footer yellow #3a3009 on #f6d55c;

View File

@@ -1,45 +0,0 @@
from textual._easing import EASING
from textual.app import App
from textual.reactive import Reactive
from textual.views import DockView
from textual.widgets import Placeholder, TreeControl, ScrollView, TreeClick
class EasingApp(App):
"""An app do demonstrate easing."""
side = Reactive(False)
easing = Reactive("linear")
def watch_side(self, side: bool) -> None:
"""Animate when the side changes (False for left, True for right)."""
width = self.easing_view.outer_size.width
animate_x = (width - self.placeholder.outer_size.width) if side else 0
self.placeholder.animate(
"layout_offset_x", animate_x, easing=self.easing, duration=1
)
async def on_mount(self) -> None:
"""Called when application mode is ready."""
self.placeholder = Placeholder()
self.easing_view = DockView()
self.placeholder.style = "white on dark_blue"
tree = TreeControl("Easing", {})
for easing_key in sorted(EASING.keys()):
await tree.add(tree.root.id, easing_key, {"easing": easing_key})
await tree.root.expand()
await self.screen.dock(ScrollView(tree), edge="left", size=32)
await self.screen.dock(self.easing_view)
await self.easing_view.dock(self.placeholder, edge="left", size=32)
async def handle_tree_click(self, message: TreeClick[dict]) -> None:
"""Called in response to a tree click."""
self.easing = message.node.data.get("easing", "linear")
self.side = not self.side
EasingApp().run(log_path="textual.log")

View File

@@ -1,34 +0,0 @@
App > View {
layout: dock;
docks: side=left/1;
}
#header {
text: on #173f5f;
height: 3;
border: hkey white;
}
#content {
text: on #20639b;
}
#footer {
height: 3;
border-top: hkey #0f2b41;
text: #3a3009 on #f6d55c;
}
#sidebar {
text: #09312e on #3caea3;
dock: side;
width: 30;
border-right: outer #09312e;
offset-x: -100%;
transition: offset 400ms in_out_cubic;
}
#sidebar.-active {
offset-x: 0;
transition: offset 400ms in_out_cubic;
}

View File

@@ -1,34 +0,0 @@
from textual.app import App
from textual.widgets import Placeholder
class GridTest(App):
async def on_mount(self) -> None:
"""Make a simple grid arrangement."""
grid = await self.screen.dock_grid(edge="left", name="left")
grid.add_column(fraction=1, name="left", min_size=20)
grid.add_column(size=30, name="center")
grid.add_column(fraction=1, name="right")
grid.add_row(fraction=1, name="top", min_size=2)
grid.add_row(fraction=2, name="middle")
grid.add_row(fraction=1, name="bottom")
grid.add_areas(
area1="left,top",
area2="center,middle",
area3="left-start|right-end,bottom",
area4="right,top-start|middle-end",
)
grid.place(
area1=Placeholder(name="area1"),
area2=Placeholder(name="area2"),
area3=Placeholder(name="area3"),
area4=Placeholder(name="area4"),
)
GridTest.run(title="Grid Test", log_path="textual.log")

View File

@@ -1,22 +0,0 @@
from textual.app import App
from textual import events
from textual.widgets import Placeholder
class GridTest(App):
async def on_mount(self, event: events.Mount) -> None:
"""Create a grid with auto-arranging cells."""
grid = await self.screen.dock_grid()
grid.add_column("col", fraction=1, max_size=20)
grid.add_row("row", fraction=1, max_size=10)
grid.set_repeat(True, True)
grid.add_areas(center="col-2-start|col-4-end,row-2-start|row-3-end")
grid.set_align("stretch", "center")
placeholders = [Placeholder() for _ in range(20)]
grid.place(*placeholders, center=Placeholder())
GridTest.run(title="Grid Test", log_path="textual.log")

View File

@@ -1,444 +0,0 @@
[![Downloads](https://pepy.tech/badge/rich/month)](https://pepy.tech/project/rich)
[![PyPI version](https://badge.fury.io/py/rich.svg)](https://badge.fury.io/py/rich)
[![codecov](https://codecov.io/gh/willmcgugan/rich/branch/master/graph/badge.svg)](https://codecov.io/gh/willmcgugan/rich)
[![Rich blog](https://img.shields.io/badge/blog-rich%20news-yellowgreen)](https://www.willmcgugan.com/tag/rich/)
[![Twitter Follow](https://img.shields.io/twitter/follow/willmcgugan.svg?style=social)](https://twitter.com/willmcgugan)
![Logo](https://github.com/willmcgugan/rich/raw/master/imgs/logo.svg)
[中文 readme](https://github.com/willmcgugan/rich/blob/master/README.cn.md) • [Lengua española readme](https://github.com/willmcgugan/rich/blob/master/README.es.md) • [Deutsche readme](https://github.com/willmcgugan/rich/blob/master/README.de.md) • [Läs på svenska](https://github.com/willmcgugan/rich/blob/master/README.sv.md) • [日本語 readme](https://github.com/willmcgugan/rich/blob/master/README.ja.md) • [한국어 readme](https://github.com/willmcgugan/rich/blob/master/README.kr.md)
Rich is a Python library for _rich_ text and beautiful formatting in the terminal.
The [Rich API](https://rich.readthedocs.io/en/latest/) makes it easy to add color and style to terminal output. Rich can also render pretty tables, progress bars, markdown, syntax highlighted source code, tracebacks, and more — out of the box.
![Features](https://github.com/willmcgugan/rich/raw/master/imgs/features.png)
For a video introduction to Rich see [calmcode.io](https://calmcode.io/rich/introduction.html) by [@fishnets88](https://twitter.com/fishnets88).
See what [people are saying about Rich](https://www.willmcgugan.com/blog/pages/post/rich-tweets/).
## Compatibility
Rich works with Linux, OSX, and Windows. True color / emoji works with new Windows Terminal, classic terminal is limited to 16 colors. Rich requires Python 3.6.1 or later.
Rich works with [Jupyter notebooks](https://jupyter.org/) with no additional configuration required.
## Installing
Install with `pip` or your favorite PyPi package manager.
```
pip install rich
```
Run the following to test Rich output on your terminal:
```
python -m rich
```
## Rich Print
To effortlessly add rich output to your application, you can import the [rich print](https://rich.readthedocs.io/en/latest/introduction.html#quick-start) method, which has the same signature as the builtin Python function. Try this:
```python
from rich import print
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
```
![Hello World](https://github.com/willmcgugan/rich/raw/master/imgs/print.png)
## Rich REPL
Rich can be installed in the Python REPL, so that any data structures will be pretty printed and highlighted.
```python
>>> from rich import pretty
>>> pretty.install()
```
![REPL](https://github.com/willmcgugan/rich/raw/master/imgs/repl.png)
## Using the Console
For more control over rich terminal content, import and construct a [Console](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console) object.
```python
from rich.console import Console
console = Console()
```
The Console object has a `print` method which has an intentionally similar interface to the builtin `print` function. Here's an example of use:
```python
console.print("Hello", "World!")
```
As you might expect, this will print `"Hello World!"` to the terminal. Note that unlike the builtin `print` function, Rich will word-wrap your text to fit within the terminal width.
There are a few ways of adding color and style to your output. You can set a style for the entire output by adding a `style` keyword argument. Here's an example:
```python
console.print("Hello", "World!", style="bold red")
```
The output will be something like the following:
![Hello World](https://github.com/willmcgugan/rich/raw/master/imgs/hello_world.png)
That's fine for styling a line of text at a time. For more finely grained styling, Rich renders a special markup which is similar in syntax to [bbcode](https://en.wikipedia.org/wiki/BBCode). Here's an example:
```python
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")
```
![Console Markup](https://github.com/willmcgugan/rich/raw/master/imgs/where_there_is_a_will.png)
You can use a Console object to generate sophisticated output with minimal effort. See the [Console API](https://rich.readthedocs.io/en/latest/console.html) docs for details.
## Rich Inspect
Rich has an [inspect](https://rich.readthedocs.io/en/latest/reference/init.html?highlight=inspect#rich.inspect) function which can produce a report on any Python object, such as class, instance, or builtin.
```python
>>> my_list = ["foo", "bar"]
>>> from rich import inspect
>>> inspect(my_list, methods=True)
```
![Log](https://github.com/willmcgugan/rich/raw/master/imgs/inspect.png)
See the [inspect docs](https://rich.readthedocs.io/en/latest/reference/init.html#rich.inspect) for details.
# Rich Library
Rich contains a number of builtin _renderables_ you can use to create elegant output in your CLI and help you debug your code.
Click the following headings for details:
<details>
<summary>Log</summary>
The Console object has a `log()` method which has a similar interface to `print()`, but also renders a column for the current time and the file and line which made the call. By default Rich will do syntax highlighting for Python structures and for repr strings. If you log a collection (i.e. a dict or a list) Rich will pretty print it so that it fits in the available space. Here's an example of some of these features.
```python
from rich.console import Console
console = Console()
test_data = [
{"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "id": "1",},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"},
]
def test_log():
enabled = False
context = {
"foo": "bar",
}
movies = ["Deadpool", "Rise of the Skywalker"]
console.log("Hello from", console, "!")
console.log(test_data, log_locals=True)
test_log()
```
The above produces the following output:
![Log](https://github.com/willmcgugan/rich/raw/master/imgs/log.png)
Note the `log_locals` argument, which outputs a table containing the local variables where the log method was called.
The log method could be used for logging to the terminal for long running applications such as servers, but is also a very nice debugging aid.
</details>
<details>
<summary>Logging Handler</summary>
You can also use the builtin [Handler class](https://rich.readthedocs.io/en/latest/logging.html) to format and colorize output from Python's logging module. Here's an example of the output:
![Logging](https://github.com/willmcgugan/rich/raw/master/imgs/logging.png)
</details>
<details>
<summary>Emoji</summary>
To insert an emoji in to console output place the name between two colons. Here's an example:
```python
>>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
😃 🧛 💩 👍 🦝
```
Please use this feature wisely.
</details>
<details>
<summary>Tables</summary>
Rich can render flexible [tables](https://rich.readthedocs.io/en/latest/tables.html) with unicode box characters. There is a large variety of formatting options for borders, styles, cell alignment etc.
![table movie](https://github.com/willmcgugan/rich/raw/master/imgs/table_movie.gif)
The animation above was generated with [table_movie.py](https://github.com/willmcgugan/rich/blob/master/examples/table_movie.py) in the examples directory.
Here's a simpler table example:
```python
from rich.console import Console
from rich.table import Table
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")
table.add_row(
"Dev 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
"May 25, 2018",
"[red]Solo[/red]: A Star Wars Story",
"$275,000,000",
"$393,151,347",
)
table.add_row(
"Dec 15, 2017",
"Star Wars Ep. VIII: The Last Jedi",
"$262,000,000",
"[bold]$1,332,539,889[/bold]",
)
console.print(table)
```
This produces the following output:
![table](https://github.com/willmcgugan/rich/raw/master/imgs/table.png)
Note that console markup is rendered in the same way as `print()` and `log()`. In fact, anything that is renderable by Rich may be included in the headers / rows (even other tables).
The `Table` class is smart enough to resize columns to fit the available width of the terminal, wrapping text as required. Here's the same example, with the terminal made smaller than the table above:
![table2](https://github.com/willmcgugan/rich/raw/master/imgs/table2.png)
</details>
<details>
<summary>Progress Bars</summary>
Rich can render multiple flicker-free [progress](https://rich.readthedocs.io/en/latest/progress.html) bars to track long-running tasks.
For basic usage, wrap any sequence in the `track` function and iterate over the result. Here's an example:
```python
from rich.progress import track
for step in track(range(100)):
do_step(step)
```
It's not much harder to add multiple progress bars. Here's an example taken from the docs:
![progress](https://github.com/willmcgugan/rich/raw/master/imgs/progress.gif)
The columns may be configured to show any details you want. Built-in columns include percentage complete, file size, file speed, and time remaining. Here's another example showing a download in progress:
![progress](https://github.com/willmcgugan/rich/raw/master/imgs/downloader.gif)
To try this out yourself, see [examples/downloader.py](https://github.com/willmcgugan/rich/blob/master/examples/downloader.py) which can download multiple URLs simultaneously while displaying progress.
</details>
<details>
<summary>Status</summary>
For situations where it is hard to calculate progress, you can use the [status](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.status) method which will display a 'spinner' animation and message. The animation won't prevent you from using the console as normal. Here's an example:
```python
from time import sleep
from rich.console import Console
console = Console()
tasks = [f"task {n}" for n in range(1, 11)]
with console.status("[bold green]Working on tasks...") as status:
while tasks:
task = tasks.pop(0)
sleep(1)
console.log(f"{task} complete")
```
This generates the following output in the terminal.
![status](https://github.com/willmcgugan/rich/raw/master/imgs/status.gif)
The spinner animations were borrowed from [cli-spinners](https://www.npmjs.com/package/cli-spinners). You can select a spinner by specifying the `spinner` parameter. Run the following command to see the available values:
```
python -m rich.spinner
```
The above command generate the following output in the terminal:
![spinners](https://github.com/willmcgugan/rich/raw/master/imgs/spinners.gif)
</details>
<details>
<summary>Tree</summary>
Rich can render a [tree](https://rich.readthedocs.io/en/latest/tree.html) with guide lines. A tree is ideal for displaying a file structure, or any other hierarchical data.
The labels of the tree can be simple text or anything else Rich can render. Run the following for a demonstration:
```
python -m rich.tree
```
This generates the following output:
![markdown](https://github.com/willmcgugan/rich/raw/master/imgs/tree.png)
See the [tree.py](https://github.com/willmcgugan/rich/blob/master/examples/tree.py) example for a script that displays a tree view of any directory, similar to the linux `tree` command.
</details>
<details>
<summary>Columns</summary>
Rich can render content in neat [columns](https://rich.readthedocs.io/en/latest/columns.html) with equal or optimal width. Here's a very basic clone of the (MacOS / Linux) `ls` command which displays a directory listing in columns:
```python
import os
import sys
from rich import print
from rich.columns import Columns
directory = os.listdir(sys.argv[1])
print(Columns(directory))
```
The following screenshot is the output from the [columns example](https://github.com/willmcgugan/rich/blob/master/examples/columns.py) which displays data pulled from an API in columns:
![columns](https://github.com/willmcgugan/rich/raw/master/imgs/columns.png)
</details>
<details>
<summary>Markdown</summary>
Rich can render [markdown](https://rich.readthedocs.io/en/latest/markdown.html) and does a reasonable job of translating the formatting to the terminal.
To render markdown import the `Markdown` class and construct it with a string containing markdown code. Then print it to the console. Here's an example:
```python
from rich.console import Console
from rich.markdown import Markdown
console = Console()
with open("README.md") as readme:
markdown = Markdown(readme.read())
console.print(markdown)
```
This will produce output something like the following:
![markdown](https://github.com/willmcgugan/rich/raw/master/imgs/markdown.png)
</details>
<details>
<summary>Syntax Highlighting</summary>
Rich uses the [pygments](https://pygments.org/) library to implement [syntax highlighting](https://rich.readthedocs.io/en/latest/syntax.html). Usage is similar to rendering markdown; construct a `Syntax` object and print it to the console. Here's an example:
```python
from rich.console import Console
from rich.syntax import Syntax
my_code = '''
def iter_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
'''
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
console = Console()
console.print(syntax)
```
This will produce the following output:
![syntax](https://github.com/willmcgugan/rich/raw/master/imgs/syntax.png)
</details>
<details>
<summary>Tracebacks</summary>
Rich can render [beautiful tracebacks](https://rich.readthedocs.io/en/latest/traceback.html) which are easier to read and show more code than standard Python tracebacks. You can set Rich as the default traceback handler so all uncaught exceptions will be rendered by Rich.
Here's what it looks like on OSX (similar on Linux):
![traceback](https://github.com/willmcgugan/rich/raw/master/imgs/traceback.png)
</details>
All Rich renderables make use of the [Console Protocol](https://rich.readthedocs.io/en/latest/protocol.html), which you can also use to implement your own Rich content.
# Rich for enterprise
Available as part of the Tidelift Subscription.
The maintainers of Rich and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. [Learn more.](https://tidelift.com/subscription/pkg/pypi-rich?utm_source=pypi-rich&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
# Project using Rich
Here are a few projects using Rich:
- [BrancoLab/BrainRender](https://github.com/BrancoLab/BrainRender)
a python package for the visualization of three dimensional neuro-anatomical data
- [Ciphey/Ciphey](https://github.com/Ciphey/Ciphey)
Automated decryption tool
- [emeryberger/scalene](https://github.com/emeryberger/scalene)
a high-performance, high-precision CPU and memory profiler for Python
- [hedythedev/StarCli](https://github.com/hedythedev/starcli)
Browse GitHub trending projects from your command line
- [intel/cve-bin-tool](https://github.com/intel/cve-bin-tool)
This tool scans for a number of common, vulnerable components (openssl, libpng, libxml2, expat and a few others) to let you know if your system includes common libraries with known vulnerabilities.
- [nf-core/tools](https://github.com/nf-core/tools)
Python package with helper tools for the nf-core community.
- [cansarigol/pdbr](https://github.com/cansarigol/pdbr)
pdb + Rich library for enhanced debugging
- [plant99/felicette](https://github.com/plant99/felicette)
Satellite imagery for dummies.
- [seleniumbase/SeleniumBase](https://github.com/seleniumbase/SeleniumBase)
Automate & test 10x faster with Selenium & pytest. Batteries included.
- [smacke/ffsubsync](https://github.com/smacke/ffsubsync)
Automagically synchronize subtitles with video.
- [tryolabs/norfair](https://github.com/tryolabs/norfair)
Lightweight Python library for adding real-time 2D object tracking to any detector.
- [ansible/ansible-lint](https://github.com/ansible/ansible-lint) Ansible-lint checks playbooks for practices and behaviour that could potentially be improved
- [ansible-community/molecule](https://github.com/ansible-community/molecule) Ansible Molecule testing framework
- +[Many more](https://github.com/willmcgugan/rich/network/dependents)!
<!-- This is a test, no need to translate -->

View File

@@ -1,51 +0,0 @@
from rich.markdown import Markdown
from textual.app import App
from textual.widgets import Header, Footer, Placeholder, ScrollView
class MyApp(App):
"""An example of a very simple Textual App"""
stylesheet = """
App > View {
layout: dock
}
#body {
padding: 1
}
#sidebar {
edge left
size: 40
}
"""
async def on_load(self) -> None:
"""Bind keys with the app loads (but before entering application mode)"""
await self.bind("b", "view.toggle('sidebar')", "Toggle sidebar")
await self.bind("q", "quit", "Quit")
async def on_mount(self) -> None:
"""Create and dock the widgets."""
body = ScrollView()
await self.screen.mount(
Header(),
Footer(),
body=body,
sidebar=Placeholder(),
)
async def get_markdown(filename: str) -> None:
with open(filename, "rt") as fh:
readme = Markdown(fh.read(), hyperlinks=True)
await body.update(readme)
await self.call_later(get_markdown, "richreadme.md")
MyApp.run(title="Simple App", log_path="textual.log")

View File

@@ -1,7 +0,0 @@
Header {
border: solid #122233;
}
App > View > Widget {
display: none;
}

View File

@@ -58,7 +58,7 @@ class FileSearchApp(App):
self.mount(file_table_wrapper=Widget(self.file_table)) self.mount(file_table_wrapper=Widget(self.file_table))
self.mount(search_bar=self.search_bar) self.mount(search_bar=self.search_bar)
def on_text_widget_base_changed(self, event: TextWidgetBase.Changed) -> None: def on_text_input_changed(self, event: TextInput.Changed) -> None:
self.file_table.filter = event.value self.file_table.filter = event.value

View File

@@ -48,7 +48,7 @@ class InputApp(App[str]):
) )
self.mount(text_area=TextArea()) self.mount(text_area=TextArea())
def on_text_widget_base_changed(self, event: TextWidgetBase.Changed) -> None: def on_text_input_changed_changed(self, event: TextInput.Changed) -> None:
try: try:
value = float(event.value) value = float(event.value)
except ValueError: except ValueError:

View File

@@ -13,8 +13,7 @@
} }
App > Screen { App > Screen {
background: $background;
color: $text; color: $text;
layers: base sidebar; layers: base sidebar;
layout: vertical; layout: vertical;
@@ -22,10 +21,11 @@ App > Screen {
} }
#tree-container { #tree-container {
background: $panel;
overflow-y: auto; overflow-y: auto;
height: 20; height: 20;
margin: 1 2; margin: 1 2;
background: $surface;
padding: 1 2; padding: 1 2;
} }
@@ -36,13 +36,18 @@ DirectoryTree {
} }
#table-container {
background: $panel;
height: auto;
margin: 1 2;
}
DataTable { DataTable {
/*border:heavy red;*/ /*border:heavy red;*/
/* tint: 10% green; */ /* tint: 10% green; */
/* text-opacity: 50%; */ /* text-opacity: 50%; */
padding: 1; background: $surface;
padding: 1 2;
margin: 1 2; margin: 1 2;
height: 24; height: 24;
} }
@@ -101,7 +106,7 @@ Tweet {
/* border: outer $primary; */ /* border: outer $primary; */
padding: 1; padding: 1;
border: wide $panel; border: wide $panel;
overflow: auto;
/* scrollbar-gutter: stable; */ /* scrollbar-gutter: stable; */
align-horizontal: center; align-horizontal: center;
box-sizing: border-box; box-sizing: border-box;
@@ -138,8 +143,14 @@ TweetBody {
padding: 0 1 0 0; padding: 0 1 0 0;
} }
Tweet.scroll-horizontal {
overflow-x: auto;
}
Tweet.scroll-horizontal TweetBody { Tweet.scroll-horizontal TweetBody {
width: 350; width: 350;
} }
.button { .button {
@@ -182,7 +193,7 @@ Tweet.scroll-horizontal TweetBody {
#sidebar .content { #sidebar .content {
layout: vertical layout: vertical;
} }
OptionItem { OptionItem {

View File

@@ -7,7 +7,7 @@ from textual.app import App, ComposeResult
from textual.reactive import Reactive from textual.reactive import Reactive
from textual.widget import Widget from textual.widget import Widget
from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer from textual.widgets import Static, DataTable, DirectoryTree, Header, Footer
from textual.layout import Container from textual.layout import Container, Vertical
CODE = ''' CODE = '''
from __future__ import annotations from __future__ import annotations
@@ -68,38 +68,38 @@ lorem_short_text = Text.from_markup(lorem_short)
lorem_long_text = Text.from_markup(lorem * 2) lorem_long_text = Text.from_markup(lorem * 2)
class TweetHeader(Widget): class TweetHeader(Static):
def render(self) -> RenderableType: def render(self) -> RenderableType:
return Text("Lorem Impsum", justify="center") return Text("Lorem Impsum", justify="center")
class TweetBody(Widget): class TweetBody(Static):
short_lorem = Reactive(False) short_lorem = Reactive(False)
def render(self) -> Text: def render(self) -> Text:
return lorem_short_text if self.short_lorem else lorem_long_text return lorem_short_text if self.short_lorem else lorem_long_text
class Tweet(Widget): class Tweet(Vertical):
pass pass
class OptionItem(Widget): class OptionItem(Static):
def render(self) -> Text: def render(self) -> Text:
return Text("Option") return Text("Option")
class Error(Widget): class Error(Static):
def render(self) -> Text: def render(self) -> Text:
return Text("This is an error message", justify="center") return Text("This is an error message", justify="center")
class Warning(Widget): class Warning(Static):
def render(self) -> Text: def render(self) -> Text:
return Text("This is a warning message", justify="center") return Text("This is a warning message", justify="center")
class Success(Widget): class Success(Static):
def render(self) -> Text: def render(self) -> Text:
return Text("This is a success message", justify="center") return Text("This is a success message", justify="center")
@@ -120,17 +120,22 @@ class BasicApp(App, css_path="basic.css"):
table = DataTable() table = DataTable()
self.scroll_to_target = Tweet(TweetBody()) self.scroll_to_target = Tweet(TweetBody())
yield Container( yield Vertical(
Tweet(TweetBody()), Tweet(TweetBody()),
Widget( Container(
Static( Static(
Syntax(CODE, "python", line_numbers=True, indent_guides=True), Syntax(
CODE,
"python",
line_numbers=True,
indent_guides=True,
),
classes="code", classes="code",
), ),
classes="scrollable", classes="scrollable",
), ),
table, Container(table, id="table-container"),
Widget(DirectoryTree("~/"), id="tree-container"), Container(DirectoryTree("~/"), id="tree-container"),
Error(), Error(),
Tweet(TweetBody(), classes="scrollbar-size-custom"), Tweet(TweetBody(), classes="scrollbar-size-custom"),
Warning(), Warning(),
@@ -143,12 +148,12 @@ class BasicApp(App, css_path="basic.css"):
Tweet(TweetBody(), classes="scroll-horizontal"), Tweet(TweetBody(), classes="scroll-horizontal"),
) )
yield Widget( yield Widget(
Widget(classes="title"), Static("Title", classes="title"),
Widget(classes="user"), Static("Content", classes="user"),
OptionItem(), OptionItem(),
OptionItem(), OptionItem(),
OptionItem(), OptionItem(),
Widget(classes="content"), Static(classes="content"),
id="sidebar", id="sidebar",
) )
yield Footer() yield Footer()

View File

@@ -1,44 +0,0 @@
from textual import layout, events
from textual.app import App, ComposeResult
from textual.widgets import Button
class ButtonsApp(App[str]):
def compose(self) -> ComposeResult:
yield layout.Vertical(
Button("default", id="foo"),
Button("Where there is a Will"),
Button("There is a Way"),
Button("There can be only one"),
Button.success("success", id="bar"),
layout.Horizontal(
Button("Where there is a Will"),
Button("There is a Way"),
Button("There can be only one"),
Button.warning("warning", id="baz"),
Button("Where there is a Will"),
Button("There is a Way"),
Button("There can be only one"),
id="scroll",
),
Button.error("error", id="baz"),
Button("Where there is a Will"),
Button("There is a Way"),
Button("There can be only one"),
)
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))

32
sandbox/will/scrolly.py Normal file
View File

@@ -0,0 +1,32 @@
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

@@ -117,7 +117,7 @@ class BoundAnimator:
def __call__( def __call__(
self, self,
attribute: str, attribute: str,
value: float, value: float | Animatable,
*, *,
final_value: object = ..., final_value: object = ...,
duration: float | None = None, duration: float | None = None,

View File

@@ -45,6 +45,14 @@ class LRUCache(Generic[CacheKey, CacheValue]):
self._lock = Lock() self._lock = Lock()
super().__init__() super().__init__()
@property
def maxsize(self) -> int:
return self._maxsize
@maxsize.setter
def maxsize(self, maxsize: int) -> None:
self._maxsize = maxsize
def __bool__(self) -> bool: def __bool__(self) -> bool:
return bool(self._cache) return bool(self._cache)

View File

@@ -73,7 +73,7 @@ def rich(source, language, css_class, options, md, attrs, **kwargs) -> str:
exec(source, globals) exec(source, globals)
except Exception: except Exception:
error_console.print_exception() error_console.print_exception()
console.bell() # console.bell()
if "output" in globals: if "output" in globals:
console.print(globals["output"]) console.print(globals["output"])

View File

@@ -17,7 +17,7 @@ class MessageTarget(Protocol):
async def post_message(self, message: "Message") -> bool: async def post_message(self, message: "Message") -> bool:
... ...
async def post_priority_message(self, message: "Message") -> bool: async def _post_priority_message(self, message: "Message") -> bool:
... ...
def post_message_no_wait(self, message: "Message") -> bool: def post_message_no_wait(self, message: "Message") -> bool:

View File

@@ -579,7 +579,7 @@ class App(Generic[ReturnType], DOMNode):
filename (str | None, optional): Filename of SVG screenshot, or None to auto-generate filename (str | None, optional): Filename of SVG screenshot, or None to auto-generate
a filename with the date and time. Defaults to None. a filename with the date and time. Defaults to None.
path (str, optional): Path to directory for output. Defaults to current working directory. path (str, optional): Path to directory for output. Defaults to current working directory.
time_format(str, optional): Time format to use if filename is None. Defaults to "%Y-%m-%d %X %f". time_format (str, optional): Time format to use if filename is None. Defaults to "%Y-%m-%d %X %f".
Returns: Returns:
str: Filename of screenshot. str: Filename of screenshot.
@@ -1209,6 +1209,8 @@ class App(Generic[ReturnType], DOMNode):
apply_stylesheet = self.stylesheet.apply apply_stylesheet = self.stylesheet.apply
for widget_id, widget in name_widgets: for widget_id, widget in name_widgets:
if not isinstance(widget, Widget):
raise AppError(f"Can't register {widget!r}; expected a Widget instance")
if widget not in self._registry: if widget not in self._registry:
if widget_id is not None: if widget_id is not None:
widget.id = widget_id widget.id = widget_id
@@ -1299,21 +1301,6 @@ class App(Generic[ReturnType], DOMNode):
self._end_update() self._end_update()
console.file.flush() console.file.flush()
def measure(self, renderable: RenderableType, max_width=100_000) -> int:
"""Get the optimal width for a widget or renderable.
Args:
renderable (RenderableType): A renderable (including Widget)
max_width ([type], optional): Maximum width. Defaults to 100_000.
Returns:
int: Number of cells required to render.
"""
measurement = Measurement.get(
self.console, self.console.options.update(max_width=max_width), renderable
)
return measurement.maximum
def get_widget_at(self, x: int, y: int) -> tuple[Widget, Region]: def get_widget_at(self, x: int, y: int) -> tuple[Widget, Region]:
"""Get the widget under the given coordinates. """Get the widget under the given coordinates.

View File

@@ -18,6 +18,7 @@ class BorderButtons(layout.Vertical):
BorderButtons { BorderButtons {
dock: left; dock: left;
width: 24; width: 24;
overflow-y: scroll;
} }
BorderButtons > Button { BorderButtons > Button {

View File

@@ -3,7 +3,7 @@ EasingButtons > Button {
} }
EasingButtons { EasingButtons {
dock: left; dock: left;
overflow: auto auto; overflow-y: scroll;
width: 20; width: 20;
} }

View File

@@ -111,7 +111,7 @@ class EasingApp(App):
self.animated_bar.position = value self.animated_bar.position = value
self.opacity_widget.styles.opacity = 1 - value / END_POSITION self.opacity_widget.styles.opacity = 1 - value / END_POSITION
def on_text_widget_base_changed(self, event: TextWidgetBase.Changed): def on_text_input_changed(self, event: TextInput.Changed):
if event.sender.id == "duration-input": if event.sender.id == "duration-input":
new_duration = _try_float(event.value) new_duration = _try_float(event.value)
if new_duration is not None: if new_duration is not None:

View File

@@ -10,8 +10,8 @@ from typing import TYPE_CHECKING, Any, Iterable, NamedTuple, cast
import rich.repr import rich.repr
from rich.style import Style from rich.style import Style
from textual._types import CallbackType from .._types import CallbackType
from .._animator import Animation, EasingFunction from .._animator import Animation, EasingFunction, BoundAnimator
from ..color import Color from ..color import Color
from ..geometry import Offset, Spacing from ..geometry import Offset, Spacing
from ._style_properties import ( from ._style_properties import (
@@ -850,6 +850,7 @@ class RenderStyles(StylesBase):
self.node = node self.node = node
self._base_styles = base self._base_styles = base
self._inline_styles = inline_styles self._inline_styles = inline_styles
self._animate: BoundAnimator | None = None
@property @property
def base(self) -> Styles: def base(self) -> Styles:
@@ -867,6 +868,23 @@ class RenderStyles(StylesBase):
assert self.node is not None assert self.node is not None
return self.node.rich_style return self.node.rich_style
@property
def animate(self) -> BoundAnimator:
"""Get an animator to animate style.
Example:
```python
self.animate("brightness", 0.5)
```
Returns:
BoundAnimator: An animator bound to this widget.
"""
if self._animate is None:
self._animate = self.node.app.animator.bind(self)
assert self._animate is not None
return self._animate
def __rich_repr__(self) -> rich.repr.Result: def __rich_repr__(self) -> rich.repr.Result:
for rule_name in RULE_NAMES: for rule_name in RULE_NAMES:
if self.has_rule(rule_name): if self.has_rule(rule_name):

View File

@@ -174,8 +174,6 @@ class ColorSystem:
for name, color in COLORS: for name, color in COLORS:
is_dark_shade = dark and name in DARK_SHADES is_dark_shade = dark and name in DARK_SHADES
spread = luminosity_spread spread = luminosity_spread
if name == "panel":
spread /= 2
for shade_name, luminosity_delta in luminosity_range(spread): for shade_name, luminosity_delta in luminosity_range(spread):
if is_dark_shade: if is_dark_shade:
dark_background = background.blend(color, 0.15) dark_background = background.blend(color, 0.15)
@@ -188,8 +186,8 @@ class ColorSystem:
colors[f"{name}{shade_name}"] = shade_color.hex colors[f"{name}{shade_name}"] = shade_color.hex
colors["text"] = "auto 95%" colors["text"] = "auto 95%"
colors["text-muted"] = "auto 80%" colors["text-muted"] = "auto 50%"
colors["text-disabled"] = "auto 60%" colors["text-disabled"] = "auto 30%"
return colors return colors

View File

@@ -20,13 +20,12 @@ if TYPE_CHECKING:
@rich.repr.auto @rich.repr.auto
class Event(Message): class Event(Message):
"""The base class for all events."""
def __rich_repr__(self) -> rich.repr.Result: def __rich_repr__(self) -> rich.repr.Result:
return return
yield yield
def __init_subclass__(cls, bubble: bool = True, verbose: bool = False) -> None:
super().__init_subclass__(bubble=bubble, verbose=verbose)
@rich.repr.auto @rich.repr.auto
class Callback(Event, bubble=False, verbose=True): class Callback(Event, bubble=False, verbose=True):

View File

@@ -6,7 +6,7 @@ import rich.repr
from . import _clock from . import _clock
from .case import camel_to_snake from .case import camel_to_snake
from ._types import MessageTarget from ._types import MessageTarget as MessageTarget
@rich.repr.auto @rich.repr.auto
@@ -36,7 +36,7 @@ class Message:
def __init__(self, sender: MessageTarget) -> None: def __init__(self, sender: MessageTarget) -> None:
self.sender = sender self.sender = sender
self.name = camel_to_snake(self.__class__.__name__.replace("Message", "")) self.name = camel_to_snake(self.__class__.__name__)
self.time = _clock.get_time_no_wait() self.time = _clock.get_time_no_wait()
self._forwarded = False self._forwarded = False
self._no_default_action = False self._no_default_action = False
@@ -71,10 +71,11 @@ class Message:
@property @property
def handler_name(self) -> str: def handler_name(self) -> str:
"""The name of the handler associated with this message."""
# Property to make it read only # Property to make it read only
return self._handler_name return self._handler_name
def set_forwarded(self) -> None: def _set_forwarded(self) -> None:
"""Mark this event as being forwarded.""" """Mark this event as being forwarded."""
self._forwarded = True self._forwarded = True
@@ -90,7 +91,8 @@ class Message:
return False return False
def prevent_default(self, prevent: bool = True) -> Message: def prevent_default(self, prevent: bool = True) -> Message:
"""Suppress the default action. """Suppress the default action(s). This will prevent handlers in any base classes
from being called.
Args: Args:
prevent (bool, optional): True if the default action should be suppressed, prevent (bool, optional): True if the default action should be suppressed,

View File

@@ -364,7 +364,7 @@ class MessagePump(metaclass=MessagePumpMeta):
if isinstance(message, Event): if isinstance(message, Event):
await self.on_event(message) await self.on_event(message)
else: else:
await self.on_message(message) await self._on_message(message)
def _get_dispatch_methods( def _get_dispatch_methods(
self, method_name: str, message: Message self, method_name: str, message: Message
@@ -390,9 +390,9 @@ class MessagePump(metaclass=MessagePumpMeta):
Args: Args:
event (events.Event): An Event object. event (events.Event): An Event object.
""" """
await self.on_message(event) await self._on_message(event)
async def on_message(self, message: Message) -> None: async def _on_message(self, message: Message) -> None:
"""Called to process a message. """Called to process a message.
Args: Args:
@@ -444,7 +444,7 @@ class MessagePump(metaclass=MessagePumpMeta):
# TODO: This may not be needed, or may only be needed by the timer # TODO: This may not be needed, or may only be needed by the timer
# Consider removing or making private # Consider removing or making private
async def post_priority_message(self, message: Message) -> bool: async def _post_priority_message(self, message: Message) -> bool:
"""Post a "priority" messages which will be processes prior to regular messages. """Post a "priority" messages which will be processes prior to regular messages.
Note that you should rarely need this in a regular app. It exists primarily to allow Note that you should rarely need this in a regular app. It exists primarily to allow
@@ -494,6 +494,14 @@ class MessagePump(metaclass=MessagePumpMeta):
await invoke(event.callback) await invoke(event.callback)
def emit_no_wait(self, message: Message) -> bool: def emit_no_wait(self, message: Message) -> bool:
"""Send a message to the _parent_, non async version.
Args:
message (Message): A message object.
Returns:
bool: True if the message was posted successfully.
"""
if self._parent: if self._parent:
return self._parent._post_message_from_child_no_wait(message) return self._parent._post_message_from_child_no_wait(message)
else: else:
@@ -506,7 +514,7 @@ class MessagePump(metaclass=MessagePumpMeta):
message (Message): A message object. message (Message): A message object.
Returns: Returns:
bool: _True if the message was posted successfully. bool: True if the message was posted successfully.
""" """
if self._parent: if self._parent:
return await self._parent._post_message_from_child(message) return await self._parent._post_message_from_child(message)

View File

@@ -30,7 +30,7 @@ class Update(Message, verbose=True):
def can_replace(self, message: Message) -> bool: def can_replace(self, message: Message) -> bool:
# Update messages can replace update for the same widget # Update messages can replace update for the same widget
return isinstance(message, Update) and self == message return isinstance(message, Update) and self.widget == message.widget
@rich.repr.auto @rich.repr.auto

View File

@@ -94,8 +94,9 @@ class Reactive(Generic[ReactiveType]):
for key in obj.__class__.__dict__.keys(): for key in obj.__class__.__dict__.keys():
if startswith(key, "_init_"): if startswith(key, "_init_"):
name = key[6:] name = key[6:]
default = getattr(obj, key) if not hasattr(obj, name):
setattr(obj, name, default() if callable(default) else default) default = getattr(obj, key)
setattr(obj, name, default() if callable(default) else default)
def __set_name__(self, owner: Type[MessageTarget], name: str) -> None: def __set_name__(self, owner: Type[MessageTarget], name: str) -> None:

21
src/textual/render.py Normal file
View File

@@ -0,0 +1,21 @@
from rich.console import Console, RenderableType
from rich.protocol import rich_cast
def measure(console: Console, renderable: RenderableType, default: int) -> int:
"""Measure a rich renderable.
Args:
console (Console): A console object.
renderable (RenderableType): Rich renderable.
default (int): Default width to use if renderable does not expose dimensions.
Returns:
int: Width in cells
"""
renderable = rich_cast(renderable)
get_console_width = getattr(renderable, "__rich_measure__", None)
if get_console_width is not None:
render_width = get_console_width(console, console.options).normalize().maximum
return max(0, render_width)
return default

View File

@@ -220,6 +220,7 @@ class Screen(Widget):
self, unclipped_region.size, virtual_size, container_size self, unclipped_region.size, virtual_size, container_size
) )
) )
except Exception as error: except Exception as error:
self.app._handle_exception(error) self.app._handle_exception(error)
return return
@@ -279,13 +280,13 @@ class Screen(Widget):
screen_y=event.screen_y, screen_y=event.screen_y,
style=event.style, style=event.style,
) )
mouse_event.set_forwarded() mouse_event._set_forwarded()
await widget._forward_event(mouse_event) await widget._forward_event(mouse_event)
async def _forward_event(self, event: events.Event) -> None: async def _forward_event(self, event: events.Event) -> None:
if event.is_forwarded: if event.is_forwarded:
return return
event.set_forwarded() event._set_forwarded()
if isinstance(event, (events.Enter, events.Leave)): if isinstance(event, (events.Enter, events.Leave)):
await self.post_message(event) await self.post_message(event)
@@ -310,7 +311,7 @@ class Screen(Widget):
return return
event.style = self.get_style_at(event.screen_x, event.screen_y) event.style = self.get_style_at(event.screen_x, event.screen_y)
if widget is self: if widget is self:
event.set_forwarded() event._set_forwarded()
await self.post_message(event) await self.post_message(event)
else: else:
await widget._forward_event(event.offset(-region.x, -region.y)) await widget._forward_event(event.offset(-region.x, -region.y))

View File

@@ -68,6 +68,25 @@ class ScrollView(Widget):
""" """
return self.virtual_size.height return self.virtual_size.height
def watch_virtual_size(self, virtual_size: Size) -> None:
self._scroll_update(virtual_size)
def watch_show_horizontal_scrollbar(self, value: bool) -> None:
"""Watch function for show_horizontal_scrollbar attribute.
Args:
value (bool): Show horizontal scrollbar flag.
"""
self.refresh(layout=True)
def watch_show_vertical_scrollbar(self, value: bool) -> None:
"""Watch function for show_vertical_scrollbar attribute.
Args:
value (bool): Show vertical scrollbar flag.
"""
self.refresh(layout=True)
def _size_updated( def _size_updated(
self, size: Size, virtual_size: Size, container_size: Size self, size: Size, virtual_size: Size, container_size: Size
) -> None: ) -> None:
@@ -78,27 +97,12 @@ class ScrollView(Widget):
virtual_size (Size): New virtual size. virtual_size (Size): New virtual size.
container_size (Size): New container size. container_size (Size): New container size.
""" """
virtual_size = self.virtual_size virtual_size = self.virtual_size
if self._size != size: if self._size != size:
self._size = size self._size = size
self._container_size = container_size self._container_size = size
self._scroll_update(virtual_size)
self._refresh_scrollbars()
width, height = self.container_size
if self.show_vertical_scrollbar:
self.vertical_scrollbar.window_virtual_size = virtual_size.height
self.vertical_scrollbar.window_size = (
height - self.scrollbar_size_horizontal
)
if self.show_horizontal_scrollbar:
self.horizontal_scrollbar.window_virtual_size = virtual_size.width
self.horizontal_scrollbar.window_size = (
width - self.scrollbar_size_vertical
)
self.scroll_x = self.validate_scroll_x(self.scroll_x)
self.scroll_y = self.validate_scroll_y(self.scroll_y)
self.refresh(layout=False)
self.scroll_to(self.scroll_x, self.scroll_y) self.scroll_to(self.scroll_x, self.scroll_y)
def render(self) -> RenderableType: def render(self) -> RenderableType:

View File

@@ -225,7 +225,9 @@ class ScrollBar(Widget):
scrollbar_style = Style.from_color(color.rich_color, background.rich_color) scrollbar_style = Style.from_color(color.rich_color, background.rich_color)
return ScrollBarRender( return ScrollBarRender(
virtual_size=self.window_virtual_size, virtual_size=self.window_virtual_size,
window_size=self.window_size, window_size=(
self.window_size if self.window_size < self.window_virtual_size else 0
),
position=self.position, position=self.position,
thickness=self.thickness, thickness=self.thickness,
vertical=self.vertical, vertical=self.vertical,

View File

@@ -167,4 +167,4 @@ class Timer:
count=count, count=count,
callback=self._callback, callback=self._callback,
) )
await self.target.post_priority_message(event) await self.target._post_priority_message(event)

View File

@@ -7,8 +7,13 @@ from operator import attrgetter
from typing import TYPE_CHECKING, ClassVar, Collection, Iterable, NamedTuple, cast from typing import TYPE_CHECKING, ClassVar, Collection, Iterable, NamedTuple, cast
import rich.repr import rich.repr
from rich.console import Console, ConsoleRenderable, JustifyMethod, RenderableType from rich.console import (
from rich.measure import Measurement Console,
ConsoleRenderable,
RichCast,
JustifyMethod,
RenderableType,
)
from rich.segment import Segment from rich.segment import Segment
from rich.style import Style from rich.style import Style
from rich.styled import Styled from rich.styled import Styled
@@ -30,6 +35,7 @@ from .geometry import Offset, Region, Size, Spacing, clamp
from .layouts.vertical import VerticalLayout from .layouts.vertical import VerticalLayout
from .message import Message from .message import Message
from .reactive import Reactive from .reactive import Reactive
from .render import measure
if TYPE_CHECKING: if TYPE_CHECKING:
from .app import App, ComposeResult from .app import App, ComposeResult
@@ -61,7 +67,9 @@ class RenderCache(NamedTuple):
@rich.repr.auto @rich.repr.auto
class Widget(DOMNode): class Widget(DOMNode):
""" """
A Widget is the base class for Textual widgets. Extent this class (or a sub-class) when defining your own widgets. A Widget is the base class for Textual widgets.
See also [static][textual.widgets._static.Static] for starting point for your own widgets.
""" """
@@ -71,7 +79,7 @@ class Widget(DOMNode):
scrollbar-background-hover: $panel-darken-2; scrollbar-background-hover: $panel-darken-2;
scrollbar-color: $primary-lighten-1; scrollbar-color: $primary-lighten-1;
scrollbar-color-active: $warning-darken-1; scrollbar-color-active: $warning-darken-1;
scrollbar-corner-color: $panel-darken-3; scrollbar-corner-color: $panel-darken-1;
scrollbar-size-vertical: 2; scrollbar-size-vertical: 2;
scrollbar-size-horizontal: 1; scrollbar-size-horizontal: 1;
} }
@@ -80,6 +88,7 @@ class Widget(DOMNode):
can_focus: bool = False can_focus: bool = False
can_focus_children: bool = True can_focus_children: bool = True
fluid = Reactive(True)
def __init__( def __init__(
self, self,
@@ -338,14 +347,12 @@ class Widget(DOMNode):
return self._content_width_cache[1] return self._content_width_cache[1]
console = self.app.console console = self.app.console
renderable = self.post_render(self.render()) renderable = self._render()
width = measure(console, renderable, container.width)
if self.fluid:
width = min(width, container.width)
measurement = Measurement.get(
console,
console.options.update_width(container.width),
renderable,
)
width = measurement.maximum
self._content_width_cache = (cache_key, width) self._content_width_cache = (cache_key, width)
return width return width
@@ -458,6 +465,7 @@ class Widget(DOMNode):
self._vertical_scrollbar = scroll_bar = ScrollBar( self._vertical_scrollbar = scroll_bar = ScrollBar(
vertical=True, name="vertical", thickness=self.scrollbar_size_vertical vertical=True, name="vertical", thickness=self.scrollbar_size_vertical
) )
self._vertical_scrollbar.display = False
self.app._start_widget(self, scroll_bar) self.app._start_widget(self, scroll_bar)
return scroll_bar return scroll_bar
@@ -475,6 +483,7 @@ class Widget(DOMNode):
self._horizontal_scrollbar = scroll_bar = ScrollBar( self._horizontal_scrollbar = scroll_bar = ScrollBar(
vertical=False, name="horizontal", thickness=self.scrollbar_size_horizontal vertical=False, name="horizontal", thickness=self.scrollbar_size_horizontal
) )
self._horizontal_scrollbar.display = False
self.app._start_widget(self, scroll_bar) self.app._start_widget(self, scroll_bar)
return scroll_bar return scroll_bar
@@ -846,7 +855,7 @@ class Widget(DOMNode):
y (int | None, optional): Y coordinate (row) to scroll to, or None for no change. Defaults to None. y (int | None, optional): Y coordinate (row) to scroll to, or None for no change. Defaults to None.
animate (bool, optional): Animate to new scroll position. Defaults to True. animate (bool, optional): Animate to new scroll position. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration. speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is False. duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if the scroll position changed, otherwise False. bool: True if the scroll position changed, otherwise False.
@@ -888,8 +897,6 @@ class Widget(DOMNode):
scroll_y = self.scroll_y scroll_y = self.scroll_y
self.scroll_target_y = self.scroll_y = y self.scroll_target_y = self.scroll_y = y
scrolled_y = scroll_y != self.scroll_y scrolled_y = scroll_y != self.scroll_y
if scrolled_x or scrolled_y:
self.refresh(repaint=False, layout=True)
return scrolled_x or scrolled_y return scrolled_x or scrolled_y
@@ -909,7 +916,7 @@ class Widget(DOMNode):
y (int | None, optional): Y distance (rows) to scroll, or ``None`` for no change. Defaults to None. y (int | None, optional): Y distance (rows) to scroll, or ``None`` for no change. Defaults to None.
animate (bool, optional): Animate to new scroll position. Defaults to False. animate (bool, optional): Animate to new scroll position. Defaults to False.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration. speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is False. duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if the scroll position changed, otherwise False. bool: True if the scroll position changed, otherwise False.
@@ -922,143 +929,258 @@ class Widget(DOMNode):
duration=duration, duration=duration,
) )
def scroll_home(self, *, animate: bool = True) -> bool: def scroll_home(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll to home position. """Scroll to home position.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(0, 0, animate=animate, duration=1) if speed is None and duration is None:
duration = 1.0
return self.scroll_to(0, 0, animate=animate, speed=speed, duration=duration)
def scroll_end(self, *, animate: bool = True) -> bool: def scroll_end(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll to the end of the container. """Scroll to the end of the container.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(0, self.max_scroll_y, animate=animate, duration=1) if speed is None and duration is None:
duration = 1.0
return self.scroll_to(
0, self.max_scroll_y, animate=animate, speed=speed, duration=duration
)
def scroll_left(self, *, animate: bool = True) -> bool: def scroll_left(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one cell left. """Scroll one cell left.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(x=self.scroll_target_x - 1, animate=animate) return self.scroll_to(
x=self.scroll_target_x - 1, animate=animate, speed=speed, duration=duration
)
def scroll_right(self, *, animate: bool = True) -> bool: def scroll_right(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll on cell right. """Scroll on cell right.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(x=self.scroll_target_x + 1, animate=animate) return self.scroll_to(
x=self.scroll_target_x + 1, animate=animate, speed=speed, duration=duration
)
def scroll_down(self, *, animate: bool = True) -> bool: def scroll_down(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one line down. """Scroll one line down.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(y=self.scroll_target_y + 1, animate=animate) return self.scroll_to(
y=self.scroll_target_y + 1, animate=animate, speed=speed, duration=duration
)
def scroll_up(self, *, animate: bool = True) -> bool: def scroll_up(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one line up. """Scroll one line up.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to(y=self.scroll_target_y - 1, animate=animate) return self.scroll_to(
y=self.scroll_target_y - 1, animate=animate, speed=speed, duration=duration
)
def scroll_page_up(self, *, animate: bool = True) -> bool: def scroll_page_up(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one page up. """Scroll one page up.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to( return self.scroll_to(
y=self.scroll_target_y - self.container_size.height, animate=animate y=self.scroll_target_y - self.container_size.height,
animate=animate,
speed=speed,
duration=duration,
) )
def scroll_page_down(self, *, animate: bool = True) -> bool: def scroll_page_down(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one page down. """Scroll one page down.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
return self.scroll_to( return self.scroll_to(
y=self.scroll_target_y + self.container_size.height, animate=animate y=self.scroll_target_y + self.container_size.height,
animate=animate,
speed=speed,
duration=duration,
) )
def scroll_page_left(self, *, animate: bool = True) -> bool: def scroll_page_left(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one page left. """Scroll one page left.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
if speed is None and duration is None:
duration = 0.3
return self.scroll_to( return self.scroll_to(
x=self.scroll_target_x - self.container_size.width, x=self.scroll_target_x - self.container_size.width,
animate=animate, animate=animate,
duration=0.3, speed=speed,
duration=duration,
) )
def scroll_page_right(self, *, animate: bool = True) -> bool: def scroll_page_right(
self,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll one page right. """Scroll one page right.
Args: Args:
animate (bool, optional): Animate scroll. Defaults to True. animate (bool, optional): Animate scroll. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling was done. bool: True if any scrolling was done.
""" """
if speed is None and duration is None:
duration = 0.3
return self.scroll_to( return self.scroll_to(
x=self.scroll_target_x + self.container_size.width, x=self.scroll_target_x + self.container_size.width,
animate=animate, animate=animate,
duration=0.3, speed=speed,
duration=duration,
) )
def scroll_to_widget(self, widget: Widget, *, animate: bool = True) -> bool: def scroll_to_widget(
self,
widget: Widget,
*,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> bool:
"""Scroll scrolling to bring a widget in to view. """Scroll scrolling to bring a widget in to view.
Args: Args:
widget (Widget): A descendant widget. widget (Widget): A descendant widget.
animate (bool, optional): True to animate, or False to jump. Defaults to True. animate (bool, optional): True to animate, or False to jump. Defaults to True.
speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
bool: True if any scrolling has occurred in any descendant, otherwise False. bool: True if any scrolling has occurred in any descendant, otherwise False.
@@ -1071,7 +1193,11 @@ class Widget(DOMNode):
while isinstance(widget.parent, Widget) and widget is not self: while isinstance(widget.parent, Widget) and widget is not self:
container = widget.parent container = widget.parent
scroll_offset = container.scroll_to_region( scroll_offset = container.scroll_to_region(
region, spacing=widget.parent.gutter, animate=animate region,
spacing=widget.parent.gutter,
animate=animate,
speed=speed,
duration=duration,
) )
if scroll_offset: if scroll_offset:
scrolled = True scrolled = True
@@ -1091,7 +1217,13 @@ class Widget(DOMNode):
return scrolled return scrolled
def scroll_to_region( def scroll_to_region(
self, region: Region, *, spacing: Spacing | None = None, animate: bool = True self,
region: Region,
*,
spacing: Spacing | None = None,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> Offset: ) -> Offset:
"""Scrolls a given region in to view, if required. """Scrolls a given region in to view, if required.
@@ -1101,8 +1233,9 @@ class Widget(DOMNode):
Args: Args:
region (Region): A region that should be visible. region (Region): A region that should be visible.
spacing (Spacing | None, optional): Optional spacing around the region. Defaults to None. spacing (Spacing | None, optional): Optional spacing around the region. Defaults to None.
animate (bool, optional): Enable animation. Defaults to True. animate (bool, optional): True to animate, or False to jump. Defaults to True.
spacing (Spacing): Space to subtract from the window region. speed (float | None, optional): Speed of scroll if animate is True. Or None to use duration.
duration (float | None, optional): Duration of animation, if animate is True and speed is None.
Returns: Returns:
Offset: The distance that was scrolled. Offset: The distance that was scrolled.
@@ -1121,19 +1254,39 @@ class Widget(DOMNode):
clamp(scroll_y + delta_y, 0, self.max_scroll_y) - scroll_y, clamp(scroll_y + delta_y, 0, self.max_scroll_y) - scroll_y,
) )
if delta: if delta:
if speed is None and duration is None:
duration = 0.2
self.scroll_relative( self.scroll_relative(
delta.x or None, delta.x or None,
delta.y or None, delta.y or None,
animate=animate if (abs(delta_y) > 1 or delta_x) else False, animate=animate if (abs(delta_y) > 1 or delta_x) else False,
duration=0.2, speed=speed,
duration=duration,
) )
return delta return delta
def scroll_visible(self) -> None: def scroll_visible(
"""Scroll the container to make this widget visible.""" self,
animate: bool = True,
speed: float | None = None,
duration: float | None = None,
) -> None:
"""Scroll the container to make this widget visible.
Args:
animate (bool, optional): _description_. Defaults to True.
speed (float | None, optional): _description_. Defaults to None.
duration (float | None, optional): _description_. Defaults to None.
"""
parent = self.parent parent = self.parent
if isinstance(parent, Widget): if isinstance(parent, Widget):
self.call_later(parent.scroll_to_widget, self) self.call_later(
parent.scroll_to_widget,
self,
animate=animate,
speed=speed,
duration=duration,
)
def __init_subclass__( def __init_subclass__(
cls, cls,
@@ -1261,13 +1414,14 @@ class Widget(DOMNode):
if isinstance(renderable, str): if isinstance(renderable, str):
renderable = Text.from_markup(renderable, justify=text_justify) renderable = Text.from_markup(renderable, justify=text_justify)
rich_style = self.rich_style if (
if isinstance(renderable, Text): isinstance(renderable, Text)
renderable.stylize(rich_style) and text_justify is not None
if text_justify is not None and renderable.justify is None: and renderable.justify is None
renderable.justify = text_justify ):
else: renderable.justify = text_justify
renderable = Styled(renderable, rich_style)
renderable = Styled(renderable, self.rich_style)
return renderable return renderable
@@ -1294,26 +1448,30 @@ class Widget(DOMNode):
self.virtual_size = virtual_size self.virtual_size = virtual_size
self._container_size = container_size self._container_size = container_size
if self.is_scrollable: if self.is_scrollable:
self._refresh_scrollbars() self._scroll_update(virtual_size)
width, height = self.container_size
if self.show_vertical_scrollbar:
self.vertical_scrollbar.window_virtual_size = virtual_size.height
self.vertical_scrollbar.window_size = (
height - self.scrollbar_size_horizontal
)
if self.show_horizontal_scrollbar:
self.horizontal_scrollbar.window_virtual_size = virtual_size.width
self.horizontal_scrollbar.window_size = (
width - self.scrollbar_size_vertical
)
self.scroll_x = self.validate_scroll_x(self.scroll_x)
self.scroll_y = self.validate_scroll_y(self.scroll_y)
self.refresh(layout=True)
self.scroll_to(self.scroll_x, self.scroll_y)
else: else:
self.refresh() self.refresh()
def _scroll_update(self, virtual_size: Size) -> None:
"""Update scrollbars visiblity and dimensions.
Args:
virtual_size (Size): Virtual size.
"""
self._refresh_scrollbars()
width, height = self.container_size
if self.show_vertical_scrollbar:
self.vertical_scrollbar.window_virtual_size = virtual_size.height
self.vertical_scrollbar.window_size = (
height - self.scrollbar_size_horizontal
)
if self.show_horizontal_scrollbar:
self.horizontal_scrollbar.window_virtual_size = virtual_size.width
self.horizontal_scrollbar.window_size = width - self.scrollbar_size_vertical
self.scroll_x = self.validate_scroll_x(self.scroll_x)
self.scroll_y = self.validate_scroll_y(self.scroll_y)
def _render_content(self) -> None: def _render_content(self) -> None:
"""Render all lines.""" """Render all lines."""
width, height = self.size width, height = self.size
@@ -1360,7 +1518,10 @@ class Widget(DOMNode):
""" """
if self._dirty_regions: if self._dirty_regions:
self._render_content() self._render_content()
line = self._render_cache.lines[y] try:
line = self._render_cache.lines[y]
except IndexError:
line = [Segment(" " * self.size.width, self.rich_style)]
return line return line
def render_lines(self, crop: Region) -> Lines: def render_lines(self, crop: Region) -> Lines:
@@ -1389,7 +1550,7 @@ class Widget(DOMNode):
return self.screen.get_style_at(x + offset_x, y + offset_y) return self.screen.get_style_at(x + offset_x, y + offset_y)
async def _forward_event(self, event: events.Event) -> None: async def _forward_event(self, event: events.Event) -> None:
event.set_forwarded() event._set_forwarded()
await self.post_message(event) await self.post_message(event)
def refresh( def refresh(
@@ -1443,6 +1604,17 @@ class Widget(DOMNode):
render = "" if self.is_container else self.css_identifier_styled render = "" if self.is_container else self.css_identifier_styled
return render return render
def _render(self) -> ConsoleRenderable | RichCast:
"""Get renderable, promoting str to text as required.
Returns:
ConsoleRenderable | RichCast: A renderable
"""
renderable = self.render()
if isinstance(renderable, str):
return Text(renderable)
return renderable
async def action(self, action: str) -> None: async def action(self, action: str) -> None:
"""Perform a given action, with this widget as the default namespace. """Perform a given action, with this widget as the default namespace.
@@ -1541,7 +1713,11 @@ class Widget(DOMNode):
def _on_mount(self, event: events.Mount) -> None: def _on_mount(self, event: events.Mount) -> None:
widgets = self.compose() widgets = self.compose()
self.mount(*widgets) self.mount(*widgets)
self.screen.refresh(repaint=False, layout=True) # Preset scrollbars if not automatic
if self.styles.overflow_y == "scroll":
self.show_vertical_scrollbar = True
if self.styles.overflow_x == "scroll":
self.show_horizontal_scrollbar = True
def _on_leave(self, event: events.Leave) -> None: def _on_leave(self, event: events.Leave) -> None:
self.mouse_over = False self.mouse_over = False

View File

@@ -20,6 +20,7 @@ __all__ = [
"Pretty", "Pretty",
"Static", "Static",
"TextInput", "TextInput",
"TextLog",
"TreeControl", "TreeControl",
"Welcome", "Welcome",
] ]

View File

@@ -8,5 +8,6 @@ from ._placeholder import Placeholder as Placeholder
from ._pretty import Pretty as Pretty from ._pretty import Pretty as Pretty
from ._static import Static as Static from ._static import Static as Static
from ._text_input import TextInput as TextInput from ._text_input import TextInput as TextInput
from ._text_log import TextLog as TextLog
from ._tree_control import TreeControl as TreeControl from ._tree_control import TreeControl as TreeControl
from ._welcome import Welcome as Welcome from ._welcome import Welcome as Welcome

View File

@@ -20,7 +20,7 @@ from ..geometry import clamp, Region, Size, Spacing
from ..reactive import Reactive from ..reactive import Reactive
from .._profile import timer from .._profile import timer
from ..scroll_view import ScrollView from ..scroll_view import ScrollView
from ..widget import Widget
from .. import messages from .. import messages
@@ -108,8 +108,8 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
DEFAULT_CSS = """ DEFAULT_CSS = """
DataTable { DataTable {
background: $surface;
color: $text; color: $text;
} }
DataTable > .datatable--header { DataTable > .datatable--header {
text-style: bold; text-style: bold;
@@ -155,6 +155,7 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
def __init__( def __init__(
self, self,
*,
name: str | None = None, name: str | None = None,
id: str | None = None, id: str | None = None,
classes: str | None = None, classes: str | None = None,
@@ -522,18 +523,6 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
return self._render_line(y, scroll_x, scroll_x + width, style) return self._render_line(y, scroll_x, scroll_x + width, style)
def render_lines(self, crop: Region) -> Lines:
"""Render the widget in to lines.
Args:
crop (Region): Region within visible area to.
Returns:
Lines: A list of list of segments
"""
lines = self._styles_cache.render_widget(self, crop)
return lines
def on_mouse_move(self, event: events.MouseMove): def on_mouse_move(self, event: events.MouseMove):
meta = event.style.meta meta = event.style.meta
if meta: if meta:

View File

@@ -56,7 +56,6 @@ class Footer(Widget):
watch(self.app, "focused", self._focus_changed) watch(self.app, "focused", self._focus_changed)
def _focus_changed(self, focused: Widget | None) -> None: def _focus_changed(self, focused: Widget | None) -> None:
self.log("FOCUS CHANGED", focused)
self._key_text = None self._key_text = None
self.refresh() self.refresh()

View File

@@ -2,8 +2,9 @@ from __future__ import annotations
from rich.console import RenderableType from rich.console import RenderableType
from rich.protocol import is_renderable from rich.protocol import is_renderable
from rich.text import Text
from ..reactive import Reactive from ..reactive import reactive
from ..errors import RenderError from ..errors import RenderError
from ..widget import Widget from ..widget import Widget
@@ -20,33 +21,76 @@ def _check_renderable(renderable: object):
""" """
if not is_renderable(renderable): if not is_renderable(renderable):
raise RenderError( raise RenderError(
f"unable to render {renderable!r}; A string, Text, or other Rich renderable is required" f"unable to render {renderable!r}; a string, Text, or other Rich renderable is required"
) )
class Static(Widget): class Static(Widget):
"""A widget to display simple static content, or use as a base- lass for more complex widgets.
Args:
renderable (RenderableType, optional): A Rich renderable, or string containing console markup.
Defaults to "".
fluid (bool, optional): Enable fluid content (adapts to size of window). Defaults to True.
name (str | None, optional): Name of widget. Defaults to None.
id (str | None, optional): ID of Widget. Defaults to None.
classes (str | None, optional): Space separated list of class names. Defaults to None.
"""
DEFAULT_CSS = """ DEFAULT_CSS = """
Static { Static {
height: auto; height: auto;
} }
""" """
fluid = reactive(True, layout=True)
_renderable: RenderableType
def __init__( def __init__(
self, self,
renderable: RenderableType = "", renderable: RenderableType = "",
*, *,
fluid: bool = True,
markup: bool = True,
name: str | None = None, name: str | None = None,
id: str | None = None, id: str | None = None,
classes: str | None = None, classes: str | None = None,
) -> None: ) -> None:
super().__init__(name=name, id=id, classes=classes) super().__init__(name=name, id=id, classes=classes)
self.fluid = fluid
self.markup = markup
self.renderable = renderable self.renderable = renderable
_check_renderable(renderable) _check_renderable(renderable)
def render(self) -> RenderableType: @property
return self.renderable def renderable(self) -> RenderableType:
return self._renderable or ""
def update(self, renderable: RenderableType) -> None: @renderable.setter
def renderable(self, renderable: RenderableType) -> None:
if isinstance(renderable, str):
if self.markup:
self._renderable = Text.from_markup(renderable)
else:
self._renderable = Text(renderable)
else:
self._renderable = renderable
def render(self) -> RenderableType:
"""Get a rich renderable for the widget's content.
Returns:
RenderableType: A rich renderable.
"""
return self._renderable
def update(self, renderable: RenderableType = "", home: bool = False) -> None:
"""Update the widget contents.
Args:
renderable (RenderableType): A new rich renderable.
"""
_check_renderable(renderable) _check_renderable(renderable)
self.renderable = renderable self.renderable = renderable
self.refresh(layout=True) self.refresh(layout=True)

View File

@@ -86,6 +86,8 @@ class TextWidgetBase(Widget):
return display_text return display_text
class Changed(Message, bubble=True): class Changed(Message, bubble=True):
namespace = "text_input"
def __init__(self, sender: MessageTarget, value: str) -> None: def __init__(self, sender: MessageTarget, value: str) -> None:
"""Message posted when the user changes the value in a TextInput """Message posted when the user changes the value in a TextInput
@@ -116,9 +118,17 @@ class TextInput(TextWidgetBase, can_focus=True):
padding: 1; padding: 1;
background: $surface; background: $surface;
content-align: left middle; content-align: left middle;
color: $text;
}
TextInput .text-input--placeholder {
color: $text-muted;
} }
""" """
COMPONENT_CLASSES = {
"text-input--placeholder",
}
def __init__( def __init__(
self, self,
*, *,
@@ -269,7 +279,10 @@ class TextInput(TextWidgetBase, can_focus=True):
else: else:
# The user has not entered text - show the placeholder # The user has not entered text - show the placeholder
display_text = Text( display_text = Text(
self.placeholder, "dim", no_wrap=True, overflow="ignore" self.placeholder,
self.get_component_rich_style("text-input--placeholder"),
no_wrap=True,
overflow="ignore",
) )
if show_cursor: if show_cursor:
display_text = self._apply_cursor_to_text(display_text, 0) display_text = self._apply_cursor_to_text(display_text, 0)

View File

@@ -0,0 +1,111 @@
from __future__ import annotations
from rich.console import RenderableType
from rich.segment import Segment
from ..reactive import var
from ..geometry import Size, Region
from ..scroll_view import ScrollView
from .._cache import LRUCache
from .._segment_tools import line_crop
from .._types import Lines
class TextLog(ScrollView, can_focus=True):
DEFAULT_CSS = """
TextLog{
background: $surface;
color: $text;
overflow-y: scroll;
}
"""
max_lines: var[int | None] = var(None)
min_width: var[int] = var(78)
wrap: var[bool] = var(False)
def __init__(
self,
*,
max_lines: int | None = None,
min_width: int = 78,
wrap: bool = False,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
) -> None:
self.max_lines = max_lines
self.lines: list[list[Segment]] = []
self._line_cache: LRUCache[tuple[int, int, int, int], list[Segment]]
self._line_cache = LRUCache(1024)
self.max_width: int = 0
self.min_width = min_width
self.wrap = wrap
super().__init__(name=name, id=id, classes=classes)
def _on_styles_updated(self) -> None:
self._line_cache.clear()
def write(self, content: RenderableType) -> None:
"""Write text or a rich renderable.
Args:
content (RenderableType): Rich renderable (or text).
"""
console = self.app.console
width = max(self.min_width, self.size.width or self.min_width)
render_options = console.options.update_width(width)
if not self.wrap:
render_options = render_options.update(overflow="ignore", no_wrap=True)
segments = self.app.console.render(content, render_options)
lines = list(Segment.split_lines(segments))
self.max_width = max(
self.max_width,
max(sum(segment.cell_length for segment in _line) for _line in lines),
)
self.lines.extend(lines)
if self.max_lines is not None:
self.lines = self.lines[-self.max_lines :]
self.virtual_size = Size(self.max_width, len(self.lines))
self.scroll_end(animate=False, speed=100)
def clear(self) -> None:
"""Clear the text log."""
del self.lines[:]
self.max_width = 0
self.virtual_size = Size(self.max_width, len(self.lines))
def render_line(self, y: int) -> list[Segment]:
scroll_x, scroll_y = self.scroll_offset
return self._render_line(scroll_y + y, scroll_x, self.size.width)
def render_lines(self, crop: Region) -> Lines:
"""Render the widget in to lines.
Args:
crop (Region): Region within visible area to.
Returns:
Lines: A list of list of segments
"""
lines = self._styles_cache.render_widget(self, crop)
return lines
def _render_line(self, y: int, scroll_x: int, width: int) -> list[Segment]:
if y >= len(self.lines):
return [Segment(" " * width, self.rich_style)]
key = (y, scroll_x, width, self.max_width)
if key in self._line_cache:
return self._line_cache[key]
line = self.lines[y]
line = Segment.adjust_line_length(
line, max(self.max_width, width), self.rich_style
)
line = line_crop(line, scroll_x, scroll_x + width, self.max_width)
self._line_cache[key] = line
return line

View File

@@ -163,8 +163,8 @@ class TreeNode(Generic[NodeDataType]):
class TreeControl(Generic[NodeDataType], Static, can_focus=True): class TreeControl(Generic[NodeDataType], Static, can_focus=True):
DEFAULT_CSS = """ DEFAULT_CSS = """
TreeControl { TreeControl {
background: $surface;
color: $text; color: $text;
height: auto; height: auto;
width: 100%; width: 100%;