Merge branch 'main' into footer-fix

This commit is contained in:
Will McGugan
2022-10-31 14:46:59 +00:00
committed by GitHub
6 changed files with 190 additions and 98 deletions

View File

@@ -13,12 +13,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed fr units for horizontal and vertical layouts https://github.com/Textualize/textual/pull/1067
- Fixed `textual run` breaking sys.argv https://github.com/Textualize/textual/issues/1064
- Fixed footer not updating styles when toggling dark mode
- Fixed how the app title in a `Header` is centred https://github.com/Textualize/textual/issues/1060
- Fixed the swapping of button variants https://github.com/Textualize/textual/issues/1048
- Fixed reserved characters in screenshots https://github.com/Textualize/textual/issues/993
### Changed
- DOMQuery now raises InvalidQueryFormat in response to invalid query strings, rather than cryptic CSS error
- Dropped quit_after, screenshot, and screenshot_title from App.run, which can all be done via auto_pilot
- Widgets are now closed in reversed DOM order
- Changed `textual run` so that it patches `argv` in more situations
### Added

View File

@@ -1,96 +0,0 @@
from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
from textual.widgets import Static
class StaticText(Static):
pass
class Header(Static):
pass
class Footer(Static):
pass
class FrApp(App):
CSS = """
Screen {
layout: horizontal;
align: center middle;
}
Vertical {
}
Header {
background: $boost;
content-align: center middle;
text-align: center;
color: $text;
height: 3;
border: tall $warning;
}
Horizontal {
height: 1fr;
align: center middle;
}
Footer {
background: $boost;
content-align: center middle;
text-align: center;
color: $text;
height: 6;
border: tall $warning;
}
StaticText {
background: $boost;
height: 8;
content-align: center middle;
text-align: center;
color: $text;
}
#foo {
width: 10;
border: tall $primary;
}
#bar {
width: 1fr;
border: tall $error;
}
#baz {
width: 20;
border: tall $success;
}
"""
def compose(self) -> ComposeResult:
yield Vertical(
Header("HEADER"),
Horizontal(
StaticText("foo", id="foo"),
StaticText("bar", id="bar"),
StaticText("baz", id="baz"),
),
Footer("FOOTER"),
)
app = FrApp()
app.run()

View File

@@ -46,6 +46,7 @@ class TextLog(ScrollView, can_focus=True):
) -> None:
super().__init__(name=name, id=id, classes=classes)
self.max_lines = max_lines
self._start_line: int = 0
self.lines: list[list[Segment]] = []
self._line_cache: LRUCache[tuple[int, int, int, int], list[Segment]]
self._line_cache = LRUCache(1024)
@@ -95,7 +96,9 @@ class TextLog(ScrollView, can_focus=True):
)
self.lines.extend(lines)
if self.max_lines is not None:
if self.max_lines is not None and len(self.lines) > self.max_lines:
self._start_line += len(self.lines) - self.max_lines
self.refresh()
self.lines = self.lines[-self.max_lines :]
self.virtual_size = Size(self.max_width, len(self.lines))
self.scroll_end(animate=False, speed=100)
@@ -103,8 +106,10 @@ class TextLog(ScrollView, can_focus=True):
def clear(self) -> None:
"""Clear the text log."""
del self.lines[:]
self._start_line = 0
self.max_width = 0
self.virtual_size = Size(self.max_width, len(self.lines))
self.refresh()
def render_line(self, y: int) -> list[Segment]:
scroll_x, scroll_y = self.scroll_offset
@@ -129,7 +134,7 @@ class TextLog(ScrollView, can_focus=True):
if y >= len(self.lines):
return [Segment(" " * width, self.rich_style)]
key = (y, scroll_x, width, self.max_width)
key = (y + self._start_line, scroll_x, width, self.max_width)
if key in self._line_cache:
return self._line_cache[key]

View File

@@ -6165,6 +6165,161 @@
'''
# ---
# name: test_textlog_max_lines
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io -->
<style>
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}
.terminal-2820474000-matrix {
font-family: Fira Code, monospace;
font-size: 20px;
line-height: 24.4px;
font-variant-east-asian: full-width;
}
.terminal-2820474000-title {
font-size: 18px;
font-weight: bold;
font-family: arial;
}
.terminal-2820474000-r1 { fill: #e1e1e1 }
.terminal-2820474000-r2 { fill: #c5c8c6 }
</style>
<defs>
<clipPath id="terminal-2820474000-clip-terminal">
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
</clipPath>
<clipPath id="terminal-2820474000-line-0">
<rect x="0" y="1.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-1">
<rect x="0" y="25.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-2">
<rect x="0" y="50.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-3">
<rect x="0" y="74.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-4">
<rect x="0" y="99.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-5">
<rect x="0" y="123.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-6">
<rect x="0" y="147.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-7">
<rect x="0" y="172.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-8">
<rect x="0" y="196.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-9">
<rect x="0" y="221.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-10">
<rect x="0" y="245.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-11">
<rect x="0" y="269.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-12">
<rect x="0" y="294.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-13">
<rect x="0" y="318.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-14">
<rect x="0" y="343.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-15">
<rect x="0" y="367.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-16">
<rect x="0" y="391.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-17">
<rect x="0" y="416.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-18">
<rect x="0" y="440.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-19">
<rect x="0" y="465.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-20">
<rect x="0" y="489.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-21">
<rect x="0" y="513.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-2820474000-line-22">
<rect x="0" y="538.3" width="976" height="24.65"/>
</clipPath>
</defs>
<rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-2820474000-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">TextLogLines</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>
<g transform="translate(9, 41)" clip-path="url(#terminal-2820474000-clip-terminal)">
<rect fill="#1e1e1e" x="0" y="1.5" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="1.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="25.9" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="50.3" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="50.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="74.7" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="99.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="123.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="147.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="172.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="196.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="221.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="245.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="269.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="294.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="318.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="343.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="367.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="391.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="416.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="440.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="465.1" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="489.5" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="513.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="538.3" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="951.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#14191f" x="951.6" y="562.7" width="24.4" height="24.65" shape-rendering="crispEdges"/>
<g class="terminal-2820474000-matrix">
<text class="terminal-2820474000-r1" x="0" y="20" textLength="951.6" clip-path="url(#terminal-2820474000-line-0)">Key&#160;press&#160;#3&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-2820474000-r2" x="976" y="20" textLength="12.2" clip-path="url(#terminal-2820474000-line-0)">
</text><text class="terminal-2820474000-r1" x="0" y="44.4" textLength="951.6" clip-path="url(#terminal-2820474000-line-1)">Key&#160;press&#160;#4&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-2820474000-r2" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-2820474000-line-1)">
</text><text class="terminal-2820474000-r1" x="0" y="68.8" textLength="951.6" clip-path="url(#terminal-2820474000-line-2)">Key&#160;press&#160;#5&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-2820474000-r2" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-2820474000-line-2)">
</text><text class="terminal-2820474000-r2" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-2820474000-line-3)">
</text><text class="terminal-2820474000-r2" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-2820474000-line-4)">
</text><text class="terminal-2820474000-r2" x="976" y="142" textLength="12.2" clip-path="url(#terminal-2820474000-line-5)">
</text><text class="terminal-2820474000-r2" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-2820474000-line-6)">
</text><text class="terminal-2820474000-r2" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-2820474000-line-7)">
</text><text class="terminal-2820474000-r2" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-2820474000-line-8)">
</text><text class="terminal-2820474000-r2" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-2820474000-line-9)">
</text><text class="terminal-2820474000-r2" x="976" y="264" textLength="12.2" clip-path="url(#terminal-2820474000-line-10)">
</text><text class="terminal-2820474000-r2" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-2820474000-line-11)">
</text><text class="terminal-2820474000-r2" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-2820474000-line-12)">
</text><text class="terminal-2820474000-r2" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-2820474000-line-13)">
</text><text class="terminal-2820474000-r2" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-2820474000-line-14)">
</text><text class="terminal-2820474000-r2" x="976" y="386" textLength="12.2" clip-path="url(#terminal-2820474000-line-15)">
</text><text class="terminal-2820474000-r2" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-2820474000-line-16)">
</text><text class="terminal-2820474000-r2" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-2820474000-line-17)">
</text><text class="terminal-2820474000-r2" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-2820474000-line-18)">
</text><text class="terminal-2820474000-r2" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-2820474000-line-19)">
</text><text class="terminal-2820474000-r2" x="976" y="508" textLength="12.2" clip-path="url(#terminal-2820474000-line-20)">
</text><text class="terminal-2820474000-r2" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-2820474000-line-21)">
</text><text class="terminal-2820474000-r2" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-2820474000-line-22)">
</text>
</g>
</g>
</svg>
'''
# ---
# name: test_vertical_layout
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">

View File

@@ -85,6 +85,10 @@ def test_header_render(snap_compare):
assert snap_compare("docs/examples/widgets/header.py")
def test_textlog_max_lines(snap_compare):
assert snap_compare("tests/snapshots/textlog_max_lines.py", press=[*"abcde", "_"])
def test_fr_units(snap_compare):
assert snap_compare("tests/snapshots/fr_units.py")

View File

@@ -0,0 +1,20 @@
from textual.app import App
from textual.widgets import TextLog
class TextLogLines(App):
count = 0
def compose(self):
yield TextLog(max_lines=3)
async def on_key(self):
self.count += 1
log_widget = self.query_one(TextLog)
log_widget.write(f"Key press #{self.count}")
app = TextLogLines()
if __name__ == "__main__":
app.run()