Skip to content

Commit 748dea6

Browse files
committed
rich.syntax
1 parent a4aafa6 commit 748dea6

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.3] - 2020-05-08
9+
10+
### Added
11+
12+
- Added `python -m rich.syntax` command
13+
814
## [1.0.2] - 2020-05-08
915

1016
### Fixed

docs/source/syntax.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ To syntax highlight code, construct a :class:`~rich.syntax.Syntax` object and pr
1313
syntax = Syntax.from_path("syntax.py", line_numbers=True)
1414
console.print(syntax)
1515

16-
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/>`_.
16+
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/>`_.
17+
18+
You can use this class from the command line. Here's how you would syntax highlight a file called "syntax.py"::
19+
20+
python -m rich.syntax syntax.py
21+
22+
For the full list of arguments, run the following::
23+
24+
python -m rich.syntax -h
25+

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "1.0.2"
5+
version = "1.0.3"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <willmcgugan@gmail.com>"]
88
license = "MIT"

rich/syntax.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,55 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
289289

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

292-
import sys
292+
import argparse
293+
294+
parser = argparse.ArgumentParser(
295+
description="Render Markdown to the console with Rich"
296+
)
297+
parser.add_argument("path", metavar="PATH", help="path to file")
298+
parser.add_argument(
299+
"-c",
300+
"--force-color",
301+
dest="force_color",
302+
action="store_true",
303+
help="force color for non-terminals",
304+
)
305+
parser.add_argument(
306+
"-l",
307+
"--line-numbers",
308+
dest="line_numbers",
309+
action="store_true",
310+
help="render line numbers",
311+
)
312+
parser.add_argument(
313+
"-w",
314+
"--width",
315+
type=int,
316+
dest="width",
317+
default=None,
318+
help="width of output (default will auto-detect)",
319+
)
320+
parser.add_argument(
321+
"-r",
322+
"--wrap",
323+
dest="word_wrap",
324+
action="store_true",
325+
default=False,
326+
help="word wrap long lines",
327+
)
328+
parser.add_argument(
329+
"-t", "--theme", dest="theme", default="monokai", help="pygments theme"
330+
)
331+
args = parser.parse_args()
332+
293333
from rich.console import Console
294334

295-
console = Console()
335+
console = Console(force_terminal=args.force_color, width=args.width)
296336

297337
syntax = Syntax.from_path(
298-
sys.argv[1], line_numbers=True, word_wrap=False, theme="default"
338+
args.path,
339+
line_numbers=args.line_numbers,
340+
word_wrap=args.word_wrap,
341+
theme=args.theme,
299342
)
300343
console.print(syntax)

0 commit comments

Comments
 (0)