From 68dbf9e1cd61dad6d0b293c93ab05d333ab6076c Mon Sep 17 00:00:00 2001 From: Pattems Date: Tue, 13 Oct 2026 18:07:07 -0400 Subject: [PATCH] Port the pager to current Textual The pager was written for Textual 0.1 (ScrollView, App.run() as a class method, view.dock()), so rich-cli pinned textual <0.2 and, through it, rich <13. Those pins make rich-cli impossible to install next to anything that uses a current Rich or Textual. Rewrite PagerApp on top of ScrollableContainer and a Static holding the pre-rendered lines. The existing keys are kept as bindings (q to quit, j/k for one line, space for a page, ctrl+d/ctrl+u for half a page), and the container adds the arrow, page up/down, home and end keys. Require textual >= 1.0.0 and rich >= 13.3.3 (the minimum Rich of Textual 1.0). --- CHANGELOG.md | 4 +- pyproject.toml | 4 +- src/rich_cli/__main__.py | 2 +- src/rich_cli/pager.py | 80 ++++++++++++++++++++++------------------ 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab90849..a07413a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet +### Changed + +- The `--pager` viewer was ported to current Textual; Rich-CLI now requires textual >= 1.0.0 and rich >= 13.3.3 ## [1.8.1] - 2025-07-04 diff --git a/pyproject.toml b/pyproject.toml index f6dd652..9ad4b6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,10 +22,10 @@ classifiers = [ ] requires-python = ">=3.9" dependencies = [ - "rich (>=12.4.0,<13.0.0)", + "rich (>=13.3.3)", "click (>=8.0.0,<9.0.0)", "requests (>=2.0.0,<3.0.0)", - "textual (>=0.1.18,<0.2.0)", + "textual (>=1.0.0)", "rich-rst (>=1.1.7,<2.0.0)", ] diff --git a/src/rich_cli/__main__.py b/src/rich_cli/__main__.py index 98df6bb..41eb7db 100644 --- a/src/rich_cli/__main__.py +++ b/src/rich_cli/__main__.py @@ -707,7 +707,7 @@ def print_usage() -> None: width = console.width render_options = console.options.update(width=width - 1) lines = console.render_lines(renderable, render_options, new_lines=True) - PagerApp.run(title=resource, content=PagerRenderable(lines, width=width)) + PagerApp(content=PagerRenderable(lines, width=width), title=resource).run() else: try: diff --git a/src/rich_cli/pager.py b/src/rich_cli/pager.py index 419d3a0..fc516dc 100644 --- a/src/rich_cli/pager.py +++ b/src/rich_cli/pager.py @@ -1,13 +1,13 @@ -from enum import auto -from typing import Iterable, List +from typing import Iterable, List, Optional -from rich.console import Console, ConsoleOptions, RenderResult +from rich.console import Console, ConsoleOptions, RenderableType, RenderResult from rich.measure import Measurement from rich.segment import Segment -from textual import events -from textual.app import App -from textual.widgets import ScrollView +from textual.app import App, ComposeResult +from textual.binding import Binding +from textual.containers import ScrollableContainer +from textual.widgets import Static class PagerRenderable: @@ -43,38 +43,46 @@ def __rich_measure__( return Measurement(self.width, self.width) -class PagerApp(App): +class PagerView(ScrollableContainer, can_focus=True): + """Scrollable view with less / vi style keys, in addition to the + arrow, page up / page down, home and end keys of ScrollableContainer.""" + + BINDINGS = [ + Binding("j", "scroll_down", "Down", show=False), + Binding("k", "scroll_up", "Up", show=False), + Binding("space", "page_down", "Page down", show=False), + Binding("ctrl+d", "half_page_down", "Half page down", show=False), + Binding("ctrl+u", "half_page_up", "Half page up", show=False), + ] + + def action_half_page_down(self) -> None: + self.scroll_relative(y=self.size.height // 2, easing="out_cubic") + + def action_half_page_up(self) -> None: + self.scroll_relative(y=-(self.size.height // 2), easing="out_cubic") + + +class PagerApp(App[None]): """App to scroll renderable""" + CSS = """ + PagerView > Static { + width: auto; + } + """ + + BINDINGS = [Binding("q", "quit", "Quit")] + def __init__( - self, - *args, - content=None, - **kwargs, + self, content: RenderableType, title: Optional[str] = None ) -> None: self.content = content - super().__init__(*args, **kwargs) - - async def on_load(self, event: events.Load) -> None: - await self.bind("q", "quit", "Quit") - - async def on_key(self, event: events.Key) -> None: - if event.key == "j": - self.body.scroll_up() - elif event.key == "k": - self.body.scroll_down() - elif event.key == " ": - self.body.page_down() - elif event.key == "ctrl+u": - self.body.target_y -= self.body.size.height // 2 - self.body.animate("y", self.body.target_y, easing="out_cubic") - elif event.key == "ctrl+d": - self.body.target_y += self.body.size.height // 2 - self.body.animate("y", self.body.target_y, easing="out_cubic") - - async def on_mount(self, event: events.Mount) -> None: - self.body = body = ScrollView(auto_width=True) - - await self.view.dock(body) - await body.focus() - await body.update(self.content) + super().__init__() + if title: + self.title = title + + def compose(self) -> ComposeResult: + yield PagerView(Static(self.content)) + + def on_mount(self) -> None: + self.query_one(PagerView).focus()