|
2 | 2 | from typing import TYPE_CHECKING, List, NoReturn, Optional, Tuple |
3 | 3 |
|
4 | 4 | import click |
5 | | -from pygments.lexers.special import TextLexer |
6 | 5 | from pygments.util import ClassNotFound |
7 | 6 | from rich.console import Console, RenderableType |
8 | 7 | from rich.markup import escape |
|
37 | 36 | "toml": "toml", |
38 | 37 | } |
39 | 38 |
|
40 | | -VERSION = "1.2.2" |
| 39 | +VERSION = "1.3.0" |
41 | 40 |
|
42 | 41 |
|
43 | 42 | def on_error(message: str, error: Optional[Exception] = None, code=-1) -> NoReturn: |
@@ -98,10 +97,11 @@ def read_resource(path: str, lexer: Optional[str]) -> Tuple[str, Optional[str]]: |
98 | 97 | if not lexer: |
99 | 98 | from pygments.lexers import guess_lexer_for_filename |
100 | 99 |
|
101 | | - lexer = guess_lexer_for_filename(path, text).name |
| 100 | + try: |
| 101 | + lexer = guess_lexer_for_filename(path, text).name |
| 102 | + except ClassNotFound: |
| 103 | + return (text, "text") |
102 | 104 | return (text, lexer) |
103 | | - except ClassNotFound: |
104 | | - return (text, TextLexer()) |
105 | 105 | except Exception as error: |
106 | 106 | on_error(f"unable to read {escape(path)}", error) |
107 | 107 |
|
@@ -363,6 +363,7 @@ class OptionHighlighter(RegexHighlighter): |
363 | 363 | default="", |
364 | 364 | help="Write HTML to [b]PATH[/b].", |
365 | 365 | ) |
| 366 | +@click.option("--pager", is_flag=True, help="Display in an interactive pager.") |
366 | 367 | def main( |
367 | 368 | resource: str, |
368 | 369 | print: bool = False, |
@@ -399,6 +400,7 @@ def main( |
399 | 400 | hyperlinks: bool = False, |
400 | 401 | force_terminal: bool = False, |
401 | 402 | export_html: Optional[str] = None, |
| 403 | + pager: bool = False, |
402 | 404 | ): |
403 | 405 | """Rich toolbox for console output.""" |
404 | 406 | console = Console( |
@@ -548,12 +550,18 @@ def main( |
548 | 550 | elif center: |
549 | 551 | justify = "center" |
550 | 552 |
|
551 | | - console.print( |
552 | | - renderable, |
553 | | - width=None if max_width <= 0 else max_width, |
554 | | - justify=justify, |
555 | | - soft_wrap=soft, |
556 | | - ) |
| 553 | + if pager: |
| 554 | + from .pager import PagerApp |
| 555 | + |
| 556 | + PagerApp.run(title=resource, content=renderable) |
| 557 | + |
| 558 | + else: |
| 559 | + console.print( |
| 560 | + renderable, |
| 561 | + width=None if max_width <= 0 else max_width, |
| 562 | + justify=justify, |
| 563 | + soft_wrap=soft, |
| 564 | + ) |
557 | 565 |
|
558 | 566 | if export_html: |
559 | 567 | try: |
|
0 commit comments