Skip to content

Commit

Permalink
Add ability to style option / command tables.
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
ewels committed May 17, 2022
1 parent bc674e8 commit 50d89b7
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 2 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ For example, to print the option flags in a different colour, you can use:
click.rich_click.STYLE_OPTION = "magenta"
```

To add a blank line between rows of options, you can use:

```python
click.rich_click.STYLE_OPTIONS_TABLE_LEADING = 1
click.rich_click.STYLE_OPTIONS_TABLE_BOX = "SIMPLE"
```

See the [_Configuration options_](#configuration-options) section below for the full list of available options.

## Groups and sorting
Expand Down Expand Up @@ -333,6 +340,29 @@ click.rich_click.COMMAND_GROUPS = {
}
```

### Table styling

Typically you would style the option / command tables using the global config options.
However, if you wish you may style tables on a per-group basis using the `table_styles` key:

```python
click.rich_click.COMMAND_GROUPS = {
"mytool": [
{
"commands": ["sync", "auth"],
"table_styles": {
"show_lines": True,
"row_styles": ["magenta", "yellow", "cyan", "green"],
"border_style": "red",
"box": "DOUBLE",
},
},
],
}
```

The available keys are: `show_lines`, `leading`, `box`, `border_style`, `row_styles`, `pad_edge`, `padding`.

## Configuration options

Here is the full list of config options:
Expand All @@ -358,8 +388,22 @@ STYLE_REQUIRED_SHORT = "red"
STYLE_REQUIRED_LONG = "dim red"
STYLE_OPTIONS_PANEL_BORDER = "dim"
ALIGN_OPTIONS_PANEL = "left"
STYLE_OPTIONS_TABLE_SHOW_LINES = False
STYLE_OPTIONS_TABLE_LEADING = 0
STYLE_OPTIONS_TABLE_PAD_EDGE = False
STYLE_OPTIONS_TABLE_PADDING = (0, 1)
STYLE_OPTIONS_TABLE_BOX = ""
STYLE_OPTIONS_TABLE_ROW_STYLES = None
STYLE_OPTIONS_TABLE_BORDER_STYLE = None
STYLE_COMMANDS_PANEL_BORDER = "dim"
ALIGN_COMMANDS_PANEL = "left"
STYLE_COMMANDS_TABLE_SHOW_LINES = False
STYLE_COMMANDS_TABLE_LEADING = 0
STYLE_COMMANDS_TABLE_PAD_EDGE = False
STYLE_COMMANDS_TABLE_PADDING = (0, 1)
STYLE_COMMANDS_TABLE_BOX = ""
STYLE_COMMANDS_TABLE_ROW_STYLES = None
STYLE_COMMANDS_TABLE_BORDER_STYLE = None
STYLE_ERRORS_PANEL_BORDER = "red"
ALIGN_ERRORS_PANEL = "left"
STYLE_ERRORS_SUGGESTION = "dim"
Expand Down
6 changes: 6 additions & 0 deletions examples/click/03_groups_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
{
"name": "Advanced options",
"options": ["--help", "--version", "--debug"],
"table_styles": {
"show_lines": True,
"row_styles": ["magenta", "yellow", "cyan", "green"],
"border_style": "red",
"box": "DOUBLE",
},
},
],
"03_groups_sorting.py sync": [
Expand Down
73 changes: 73 additions & 0 deletions examples/click/10_table_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import rich_click as click

click.rich_click.STYLE_OPTIONS_TABLE_LEADING = 1
click.rich_click.STYLE_OPTIONS_TABLE_BOX = "SIMPLE"
click.rich_click.STYLE_OPTIONS_TABLE_ROW_STYLES = ["bold", ""]
click.rich_click.STYLE_COMMANDS_TABLE_SHOW_LINES = True
click.rich_click.STYLE_COMMANDS_TABLE_PAD_EDGE = True
click.rich_click.STYLE_COMMANDS_TABLE_PADDING = (1, 2)
click.rich_click.STYLE_COMMANDS_TABLE_BOX = "DOUBLE"
click.rich_click.STYLE_COMMANDS_TABLE_BORDER_STYLE = "red"
click.rich_click.STYLE_COMMANDS_TABLE_ROW_STYLES = ["magenta", "yellow", "cyan", "green"]


@click.group()
@click.option(
"--type",
default="files",
show_default=True,
required=True,
help="Type of file to sync",
)
@click.option(
"--debug/--no-debug",
"-d/-n",
default=False,
show_default=True,
help="Show the debug log messages",
)
@click.version_option("1.23", prog_name="mytool")
def cli(type, debug):
"""
My amazing tool does all the things.
This is a minimal example based on documentation
from the 'click' package.
You can try using --help at the top level and also for
specific group subcommands.
"""
print(f"Debug mode is {'on' if debug else 'off'}")


@cli.command()
@click.option("--input", "-i", required=True, help="Input path")
@click.option("--output", "-o", help="Output path")
@click.option("--all", is_flag=True, help="Sync all the things?")
@click.option("--overwrite", is_flag=True, help="Overwrite local files")
def sync(input, output, all, overwrite):
"""Synchronise all your files between two places."""
print("Syncing")


@cli.command()
@click.option("--all", is_flag=True, help="Get everything")
def download(all):
"""Pretend to download some files from somewhere."""
print("Downloading")


@cli.command()
def auth():
"""Authenticate the app."""
print("Downloading")


@cli.command()
def config():
"""Set up the configuration."""
print("Downloading")


if __name__ == "__main__":
cli()
55 changes: 53 additions & 2 deletions src/rich_click/rich_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import click
import rich.markdown
from rich import box
from rich.align import Align
from rich.columns import Columns
from rich.console import Console
Expand Down Expand Up @@ -42,8 +43,22 @@
STYLE_REQUIRED_LONG = "dim red"
STYLE_OPTIONS_PANEL_BORDER = "dim"
ALIGN_OPTIONS_PANEL = "left"
STYLE_OPTIONS_TABLE_SHOW_LINES = False
STYLE_OPTIONS_TABLE_LEADING = 0
STYLE_OPTIONS_TABLE_PAD_EDGE = False
STYLE_OPTIONS_TABLE_PADDING = (0, 1)
STYLE_OPTIONS_TABLE_BOX = ""
STYLE_OPTIONS_TABLE_ROW_STYLES = None
STYLE_OPTIONS_TABLE_BORDER_STYLE = None
STYLE_COMMANDS_PANEL_BORDER = "dim"
ALIGN_COMMANDS_PANEL = "left"
STYLE_COMMANDS_TABLE_SHOW_LINES = False
STYLE_COMMANDS_TABLE_LEADING = 0
STYLE_COMMANDS_TABLE_PAD_EDGE = False
STYLE_COMMANDS_TABLE_PADDING = (0, 1)
STYLE_COMMANDS_TABLE_BOX = ""
STYLE_COMMANDS_TABLE_ROW_STYLES = None
STYLE_COMMANDS_TABLE_BORDER_STYLE = None
STYLE_ERRORS_PANEL_BORDER = "red"
ALIGN_ERRORS_PANEL = "left"
STYLE_ERRORS_SUGGESTION = "dim"
Expand Down Expand Up @@ -470,7 +485,25 @@ class MetavarHighlighter(RegexHighlighter):
options_rows.append(rows)

if len(options_rows) > 0:
options_table = Table(highlight=True, box=None, show_header=False)
t_styles = {
"show_lines": STYLE_OPTIONS_TABLE_SHOW_LINES,
"leading": STYLE_OPTIONS_TABLE_LEADING,
"box": STYLE_OPTIONS_TABLE_BOX,
"border_style": STYLE_OPTIONS_TABLE_BORDER_STYLE,
"row_styles": STYLE_OPTIONS_TABLE_ROW_STYLES,
"pad_edge": STYLE_OPTIONS_TABLE_PAD_EDGE,
"padding": STYLE_OPTIONS_TABLE_PADDING,
}
t_styles.update(option_group.get("table_styles", {})) # type: ignore
box_style = getattr(box, t_styles.pop("box"), None) # type: ignore

options_table = Table(
highlight=True,
show_header=False,
expand=True,
box=box_style,
**t_styles,
)
# Strip the required column if none are required
if all([x[0] == "" for x in options_rows]):
options_rows = [x[1:] for x in options_rows]
Expand Down Expand Up @@ -505,7 +538,25 @@ class MetavarHighlighter(RegexHighlighter):

# Print each command group panel
for cmd_group in cmd_groups:
commands_table = Table(highlight=False, box=None, show_header=False)
t_styles = {
"show_lines": STYLE_COMMANDS_TABLE_SHOW_LINES,
"leading": STYLE_COMMANDS_TABLE_LEADING,
"box": STYLE_COMMANDS_TABLE_BOX,
"border_style": STYLE_COMMANDS_TABLE_BORDER_STYLE,
"row_styles": STYLE_COMMANDS_TABLE_ROW_STYLES,
"pad_edge": STYLE_COMMANDS_TABLE_PAD_EDGE,
"padding": STYLE_COMMANDS_TABLE_PADDING,
}
t_styles.update(cmd_group.get("table_styles", {})) # type: ignore
box_style = getattr(box, t_styles.pop("box"), None) # type: ignore

commands_table = Table(
highlight=False,
show_header=False,
expand=True,
box=box_style,
**t_styles,
)
# Define formatting in first column, as commands don't match highlighter regex
commands_table.add_column(style="bold cyan", no_wrap=True)
for command in cmd_group.get("commands", []):
Expand Down

0 comments on commit 50d89b7

Please sign in to comment.