From 672f794dd495aa99611c9cfb002bf4b8e440aa46 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 12 May 2020 21:25:39 +0100 Subject: [PATCH] added --page argument to markdown --- CHANGELOG.md | 8 +++++++- rich/console.py | 9 ++++++--- rich/markdown.py | 24 +++++++++++++++++++++--- setup.py | 2 +- tests/test_markdown.py | 2 +- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f8b40f..1e8a3ca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.2] - Unreleased + +### Added + +- Added -p switch to python -m rich.markdown to page output + ## [1.1.1] - 2020-05-12 -## Changed +### Changed - Stripped cursor moving control codes from text diff --git a/rich/console.py b/rich/console.py index 4a49bec1..7960400e 100644 --- a/rich/console.py +++ b/rich/console.py @@ -521,6 +521,8 @@ def render_lines( Args: renderables (Iterable[RenderableType]): Any object or objects renderable in the console. options (Optional[ConsoleOptions]): Console options used to render with. + style (Style, optional): Optional style to apply to renderables. Defaults to ``None``. + pad (bool, optional): Pad lines shorter than render width. Defaults to ``True``. Returns: List[List[Segment]]: A list of lines, where a line is a list of Segment objects. @@ -549,7 +551,8 @@ def render_str( markup: bool = None, highlighter: HighlighterType = None, ) -> "Text": - """Convert a string to a Text instance. + """Convert a string to a Text instance. This is is called automatically if + you print or log a string. Args: text (str): Text to render. @@ -581,7 +584,7 @@ def render_str( def get_style( self, name: Union[str, Style], *, default: Union[Style, str] = None ) -> Style: - """Get a style merged with the current style. + """Get a Style instance by it's theme name or parse a definition. Args: name (str): The name of a style or a style definition. @@ -598,7 +601,7 @@ def get_style( try: style = self._styles.get(name) - return style.copy() if style is not None else Style.parse(name) + return style.copy() if style is not None else Style.parse(name) except errors.StyleSyntaxError as error: if default is not None: return self.get_style(default) diff --git a/rich/markdown.py b/rich/markdown.py index 7a6c5238..fa8b3807 100644 --- a/rich/markdown.py +++ b/rich/markdown.py @@ -153,7 +153,7 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult ) else: # Styled text for h2 and beyond - if self.level: + if self.level == 2: yield Text("\n") yield text @@ -525,11 +525,17 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult action="store_true", help="enable full text justify", ) + parser.add_argument( + "-p", + "--page", + dest="page", + action="store_true", + help="use pager to scroll output", + ) args = parser.parse_args() from rich.console import Console - console = Console(force_terminal=args.force_color, width=args.width) with open(args.path, "rt") as markdown_file: markdown = Markdown( markdown_file.read(), @@ -537,4 +543,16 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult code_theme=args.code_theme, hyperlinks=args.hyperlinks, ) - console.print(markdown) + if args.page: + import pydoc + import io + + console = Console( + file=io.StringIO(), force_terminal=args.force_color, width=args.width + ) + console.print(markdown) + pydoc.pager(console.file.getvalue()) # type: ignore + + else: + console = Console(force_terminal=args.force_color, width=args.width) + console.print(markdown) diff --git a/setup.py b/setup.py index 0c972968..43a929cc 100644 --- a/setup.py +++ b/setup.py @@ -5,4 +5,4 @@ import setuptools if __name__ == "__main__": - setuptools.setup() + setuptools.setup(name="rich") diff --git a/tests/test_markdown.py b/tests/test_markdown.py index 5525281b..8d1c597d 100644 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -11,7 +11,7 @@ def test_markdown_render(): markdown = Markdown(MARKDOWN) rendered_markdown = render(markdown) - expected = "╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n║ \x1b[1mHeading\x1b[0m ║\n╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n\n\n \x1b[1;4mSub-heading\x1b[0m \n\n\n \x1b[1mHeading\x1b[0m \n\n\n \x1b[1;2mH4 Heading\x1b[0m \n\n\n \x1b[4mH5 Heading\x1b[0m \n\n\n \x1b[3mH6 Heading\x1b[0m \n\nParagraphs are separated by a blank line. \n\nTwo spaces at the end of a line \nproduces a line break. \n\nText attributes \x1b[3mitalic\x1b[0m, \x1b[1mbold\x1b[0m, \x1b[38;5;15;40mmonospace\x1b[0m. \n\nHorizontal rule: \n\n\x1b[2m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\nBullet list: \n\n\x1b[1;33m • \x1b[0mapples \n\x1b[1;33m • \x1b[0moranges \n\x1b[1;33m • \x1b[0mpears \n\nNumbered list: \n\n\x1b[1;33m 1 \x1b[0mlather \n\x1b[1;33m 2 \x1b[0mrinse \n\x1b[1;33m 3 \x1b[0mrepeat \n\nAn \x1b]8;id=3607223980;http://example.com\x1b\\\x1b[38;5;12mexample\x1b[0m\x1b]8;;\x1b\\. \n\n\x1b[35m▌ \x1b[0m\x1b[35mMarkdown uses email-style > characters for blockquoting.\x1b[0m\x1b[35m \x1b[0m\n\x1b[35m▌ \x1b[0m\x1b[35mLorem ipsum\x1b[0m\x1b[35m \x1b[0m\n\n🌆 \x1b]8;id=1358623393;https://github.com/willmcgugan/rich/raw/master/imgs/progress.gif\x1b\\progress\x1b]8;;\x1b\\ \n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34ma=1 \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;249;38;114;48;2;39;40;34mimport\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mthis\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mfoobar \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n" + expected = "╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n║ \x1b[1mHeading\x1b[0m ║\n╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n\n\n \x1b[1;4mSub-heading\x1b[0m \n\n \x1b[1mHeading\x1b[0m \n\n \x1b[1;2mH4 Heading\x1b[0m \n\n \x1b[4mH5 Heading\x1b[0m \n\n \x1b[3mH6 Heading\x1b[0m \n\nParagraphs are separated by a blank line. \n\nTwo spaces at the end of a line \nproduces a line break. \n\nText attributes \x1b[3mitalic\x1b[0m, \x1b[1mbold\x1b[0m, \x1b[38;5;15;40mmonospace\x1b[0m. \n\nHorizontal rule: \n\n\x1b[2m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\nBullet list: \n\n\x1b[1;33m • \x1b[0mapples \n\x1b[1;33m • \x1b[0moranges \n\x1b[1;33m • \x1b[0mpears \n\nNumbered list: \n\n\x1b[1;33m 1 \x1b[0mlather \n\x1b[1;33m 2 \x1b[0mrinse \n\x1b[1;33m 3 \x1b[0mrepeat \n\nAn \x1b]8;id=3607223980;http://example.com\x1b\\\x1b[38;5;12mexample\x1b[0m\x1b]8;;\x1b\\. \n\n\x1b[35m▌ \x1b[0m\x1b[35mMarkdown uses email-style > characters for blockquoting.\x1b[0m\x1b[35m \x1b[0m\n\x1b[35m▌ \x1b[0m\x1b[35mLorem ipsum\x1b[0m\x1b[35m \x1b[0m\n\n🌆 \x1b]8;id=1358623393;https://github.com/willmcgugan/rich/raw/master/imgs/progress.gif\x1b\\progress\x1b]8;;\x1b\\ \n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34ma=1 \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;249;38;114;48;2;39;40;34mimport\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mthis\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mfoobar \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n" assert rendered_markdown == expected