Files
rich-print/src/rp/pager.py

41 lines
1.1 KiB
Python

"""Custom pager with mouse scroll support."""
from __future__ import annotations
import shutil
import subprocess
from rich.pager import Pager, SystemPager
def _is_mouse_capable_less() -> bool:
less = shutil.which("less")
if less is None:
return False
try:
result = subprocess.run(
[less, "--version"], capture_output=True, text=True, timeout=5
)
version_str = result.stdout or result.stderr or ""
return "GNU" in version_str or "less" in version_str.lower()
except (subprocess.TimeoutExpired, OSError):
return False
class MousePager(Pager):
"""Pager that prefers ``less --mouse`` and falls back to the system pager."""
def show(self, content: str) -> None:
if _is_mouse_capable_less():
try:
subprocess.run(
["less", "-R", "--mouse"],
input=content,
text=True,
check=True,
)
return
except (subprocess.CalledProcessError, FileNotFoundError):
pass
SystemPager().show(content)