Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
]

Expand Down
2 changes: 1 addition & 1 deletion src/rich_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
80 changes: 44 additions & 36 deletions src/rich_cli/pager.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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()