|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import click |
| 6 | +from _pytask import __version__ as version |
| 7 | +from _pytask.console import console |
| 8 | +from click_default_group import DefaultGroup |
| 9 | +from rich.highlighter import RegexHighlighter |
| 10 | +from rich.panel import Panel |
| 11 | +from rich.table import Table |
| 12 | +from rich.text import Text |
| 13 | + |
| 14 | + |
| 15 | +_SWITCH_REGEX = r"(?P<switch>\-\w)\b" |
| 16 | +_OPTION_REGEX = r"(?P<option>\-\-[\w\-]+)" |
| 17 | +_METAVAR_REGEX = r"\-\-[\w\-]+(?P<metavar>[ |=][\w\.:]+)" |
| 18 | + |
| 19 | + |
| 20 | +class OptionHighlighter(RegexHighlighter): |
| 21 | + highlights = [_SWITCH_REGEX, _OPTION_REGEX, _METAVAR_REGEX] |
| 22 | + |
| 23 | + |
| 24 | +class ColoredGroup(DefaultGroup): |
| 25 | + def format_help(self: DefaultGroup, ctx: Any, formatter: Any) -> None: # noqa: U100 |
| 26 | + highlighter = OptionHighlighter() |
| 27 | + |
| 28 | + console.print( |
| 29 | + f"[b]pytask[/b] [dim]v{version}[/]\n", justify="center", highlight=False |
| 30 | + ) |
| 31 | + |
| 32 | + console.print( |
| 33 | + "Usage: [b]pytask[/b] [b][OPTIONS][/b] [b][COMMAND][/b] [b][PATHS][/b]\n" |
| 34 | + ) |
| 35 | + |
| 36 | + console.print(self.help, style="dim") |
| 37 | + console.print() |
| 38 | + |
| 39 | + commands_table = Table(highlight=True, box=None, show_header=False) |
| 40 | + |
| 41 | + for command_name in sorted(self.commands): |
| 42 | + command = self.commands[command_name] |
| 43 | + |
| 44 | + if command_name == self.default_cmd_name: |
| 45 | + formatted_name = Text(command_name + " *", style="command") |
| 46 | + else: |
| 47 | + formatted_name = Text(command_name, style="command") |
| 48 | + |
| 49 | + commands_table.add_row(formatted_name, highlighter(command.help)) |
| 50 | + |
| 51 | + console.print( |
| 52 | + Panel( |
| 53 | + commands_table, |
| 54 | + title="[bold #ffffff]Commands[/bold #ffffff]", |
| 55 | + title_align="left", |
| 56 | + border_style="grey37", |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + print_options(self, ctx) |
| 61 | + |
| 62 | + console.print( |
| 63 | + "[bold red]♥[/bold red] [white]https://pytask-dev.readthedocs.io[/]", |
| 64 | + justify="right", |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class ColoredCommand(click.Command): |
| 69 | + """Override Clicks help with a Richer version.""" |
| 70 | + |
| 71 | + def format_help( |
| 72 | + self: click.Command, ctx: Any, formatter: Any # noqa: U100 |
| 73 | + ) -> None: |
| 74 | + console.print( |
| 75 | + f"[b]pytask[/b] [dim]v{version}[/]\n", justify="center", highlight=False |
| 76 | + ) |
| 77 | + |
| 78 | + console.print( |
| 79 | + f"Usage: [b]pytask[/b] [b]{self.name}[/b] [b][OPTIONS][/b] [b][PATHS][/b]\n" |
| 80 | + ) |
| 81 | + |
| 82 | + console.print(self.help, style="dim") |
| 83 | + console.print() |
| 84 | + |
| 85 | + print_options(self, ctx) |
| 86 | + |
| 87 | + console.print( |
| 88 | + "[bold red]♥[/bold red] [white]https://pytask-dev.readthedocs.io[/]", |
| 89 | + justify="right", |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def print_options(group_or_command: click.Command | DefaultGroup, ctx: Any) -> None: |
| 94 | + highlighter = OptionHighlighter() |
| 95 | + |
| 96 | + options_table = Table(highlight=True, box=None, show_header=False) |
| 97 | + |
| 98 | + for param in group_or_command.get_params(ctx): |
| 99 | + |
| 100 | + if isinstance(param, click.Argument): |
| 101 | + continue |
| 102 | + |
| 103 | + # The ordering of -h and --help is not fixed. |
| 104 | + if param.name == "help": |
| 105 | + opt1 = highlighter("-h") |
| 106 | + opt2 = highlighter("--help") |
| 107 | + elif len(param.opts) == 2: |
| 108 | + opt1 = highlighter(param.opts[0]) |
| 109 | + opt2 = highlighter(param.opts[1]) |
| 110 | + else: |
| 111 | + opt1 = Text("") |
| 112 | + opt2 = highlighter(param.opts[0]) |
| 113 | + |
| 114 | + if param.metavar: |
| 115 | + opt2 += Text(f" {param.metavar}", style="metavar") |
| 116 | + |
| 117 | + help_record = param.get_help_record(ctx) |
| 118 | + if help_record is None: |
| 119 | + help_text = "" |
| 120 | + else: |
| 121 | + help_text = Text.from_markup(param.get_help_record(ctx)[-1], emoji=False) |
| 122 | + |
| 123 | + options_table.add_row(opt1, opt2, highlighter(help_text)) |
| 124 | + |
| 125 | + console.print( |
| 126 | + Panel( |
| 127 | + options_table, |
| 128 | + title="[bold #ffffff]Options[/bold #ffffff]", |
| 129 | + title_align="left", |
| 130 | + border_style="grey37", |
| 131 | + ) |
| 132 | + ) |
0 commit comments