Skip to content

Commit

Permalink
rich.syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed May 8, 2020
1 parent a4aafa6 commit 748dea6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.0.3] - 2020-05-08

### Added

- Added `python -m rich.syntax` command

## [1.0.2] - 2020-05-08

### Fixed
Expand Down
11 changes: 10 additions & 1 deletion docs/source/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ To syntax highlight code, construct a :class:`~rich.syntax.Syntax` object and pr
syntax = Syntax.from_path("syntax.py", line_numbers=True)
console.print(syntax)

The Syntax constructor (and :meth:`~rich.syntax.Syntax.from_path`) accept a ``theme`` attribute which should be the name of a `Pygments theme <https://pygments.org/demo/>`_.
The Syntax constructor (and :meth:`~rich.syntax.Syntax.from_path`) accept a ``theme`` attribute which should be the name of a `Pygments theme <https://pygments.org/demo/>`_.

You can use this class from the command line. Here's how you would syntax highlight a file called "syntax.py"::

python -m rich.syntax syntax.py

For the full list of arguments, run the following::

python -m rich.syntax -h
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "1.0.2"
version = "1.0.3"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <[email protected]>"]
license = "MIT"
Expand Down
49 changes: 46 additions & 3 deletions rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,55 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult

if __name__ == "__main__": # pragma: no cover

import sys
import argparse

parser = argparse.ArgumentParser(
description="Render Markdown to the console with Rich"
)
parser.add_argument("path", metavar="PATH", help="path to file")
parser.add_argument(
"-c",
"--force-color",
dest="force_color",
action="store_true",
help="force color for non-terminals",
)
parser.add_argument(
"-l",
"--line-numbers",
dest="line_numbers",
action="store_true",
help="render line numbers",
)
parser.add_argument(
"-w",
"--width",
type=int,
dest="width",
default=None,
help="width of output (default will auto-detect)",
)
parser.add_argument(
"-r",
"--wrap",
dest="word_wrap",
action="store_true",
default=False,
help="word wrap long lines",
)
parser.add_argument(
"-t", "--theme", dest="theme", default="monokai", help="pygments theme"
)
args = parser.parse_args()

from rich.console import Console

console = Console()
console = Console(force_terminal=args.force_color, width=args.width)

syntax = Syntax.from_path(
sys.argv[1], line_numbers=True, word_wrap=False, theme="default"
args.path,
line_numbers=args.line_numbers,
word_wrap=args.word_wrap,
theme=args.theme,
)
console.print(syntax)

0 comments on commit 748dea6

Please sign in to comment.