Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 26, 2022
2 parents 567f6c8 + 5c6ac2a commit b7236a1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
.DS_Store

# C extensions
*.so
Expand Down
51 changes: 43 additions & 8 deletions src/rich_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rich.console import ConsoleOptions, RenderResult
from rich.measure import Measurement

VERSION = "1.0"

BOXES = [
"none",
Expand Down Expand Up @@ -101,15 +102,14 @@ class OptionHighlighter(RegexHighlighter):
)

console.print(
"[b]Rich CLI[/b] 🤑\n\n[dim]Rich text and formatting in the terminal\n",
f"[b]Rich CLI[/b] [cyan]v{VERSION}[/] 🤑\n\n[dim]Rich text and formatting in the terminal\n",
justify="center",
)

console.print(
"Usage: [b]rich[/b] [b][OPTIONS][/] [b cyan]<PATH or TEXT or '-'>\n"
)

from rich import box
from rich.panel import Panel

options_table = Table(
Expand Down Expand Up @@ -142,10 +142,6 @@ class OptionHighlighter(RegexHighlighter):

options_table.add_row(opt1, opt2, highlighter(help))

# rv = param.get_help_record(ctx)
# if rv is not None:
# opts.append(rv)

console.print(
Panel(
options_table, border_style="dim", title="Options", title_align="left"
Expand Down Expand Up @@ -190,6 +186,21 @@ class OptionHighlighter(RegexHighlighter):
@click.option(
"--rule-style", metavar="STYLE", help="Rule style.", default="bright_green"
)
@click.option(
"--head",
"-h",
type=click.IntRange(min=1),
metavar="LINES",
default=None,
help="Display head of the file.",
)
@click.option(
"--tail",
type=click.IntRange(min=1),
metavar="LINES",
default=None,
help="Display tail of the file.",
)
@click.option(
"--rule-char",
metavar="CHARACTER(S)",
Expand Down Expand Up @@ -217,7 +228,7 @@ class OptionHighlighter(RegexHighlighter):
)
@click.option(
"--theme",
"-t",
"-m",
metavar="THEME",
help="Syntax theme. [dim]See https://pygments.org/styles/",
default="ansi_dark",
Expand Down Expand Up @@ -253,6 +264,8 @@ def main(
text_right: bool = False,
text_center: bool = False,
soft: bool = False,
head: Optional[int] = None,
tail: Optional[int] = None,
text_full: bool = False,
expand: bool = False,
width: int = -1,
Expand Down Expand Up @@ -349,21 +362,29 @@ def main(

try:
if resource == "-":
string = sys.stdin.read()
num_lines = len(string.splitlines())
line_range = _line_range(head, tail, num_lines)
renderable = Syntax(
sys.stdin.read(),
string,
lexer,
theme=theme,
line_numbers=line_numbers,
indent_guides=guides,
word_wrap=not no_wrap,
line_range=line_range,
)
else:
num_lines = len(read_resource(resource).splitlines())
line_range = _line_range(head, tail, num_lines)

renderable = Syntax.from_path(
resource,
theme=theme,
line_numbers=line_numbers,
indent_guides=guides,
word_wrap=not no_wrap,
line_range=line_range,
)
except Exception as error:
on_error("unable to read file", error)
Expand Down Expand Up @@ -428,6 +449,20 @@ def main(
on_error("failed to save HTML", error)


def _line_range(head, tail, num_lines):
if head and tail:
on_error("cannot specify both head and tail")
if head:
line_range = (1, head)
elif tail:
start_line = num_lines - tail + 2
finish_line = num_lines + 1
line_range = (start_line, finish_line)
else:
line_range = None
return line_range


def run():
main()

Expand Down

0 comments on commit b7236a1

Please sign in to comment.