Compare commits

..

No commits in common. "1c2d1abb2a6be65f577db96a46f8bb7590e65799" and "10cb9777faf21bdd312aa86ecd25e4ffdd2909be" have entirely different histories.

5 changed files with 20 additions and 57 deletions

View File

@ -1,3 +1,3 @@
"""module for package metadata.""" """module for package metadata."""
__version__ = '0.11.3' __version__ = '0.11.0'

View File

@ -8,15 +8,6 @@ from . import styles
from .__about__ import __version__ as version from .__about__ import __version__ as version
def validate_style(ctx: click.Context, param: click.Parameter, value: str) -> str:
"""Validate the style option."""
if value not in styles.registry:
raise click.BadParameter(
f"Invalid style '{value}'. Available styles: {', '.join(styles.registry.keys())}"
)
return value
@click.group() @click.group()
@click.option( @click.option(
'-d', '-d',
@ -52,7 +43,6 @@ def validate_style(ctx: click.Context, param: click.Parameter, value: str) -> st
show_default=True, show_default=True,
show_envvar=True, show_envvar=True,
help='The style to use for output.', help='The style to use for output.',
callback=validate_style,
) )
@click.option( @click.option(
'-b', '-b',

View File

@ -9,9 +9,13 @@ err = Console(stderr=True, style='bold red')
def highlight(ctx: click.Context, text: str) -> str: def highlight(ctx: click.Context, text: str) -> str:
"""Highlight text for console output.""" """Highlight text for console output."""
if ctx.obj['style'].name == 'no_colour':
return text
return f'[{ctx.obj["style"].highlight}]{text}[/{ctx.obj["style"].highlight}]' return f'[{ctx.obj["style"].highlight}]{text}[/{ctx.obj["style"].highlight}]'
def warning(ctx: click.Context, text: str) -> str: def warning(ctx: click.Context, text: str) -> str:
"""Format warning text for console output.""" """Format warning text for console output."""
return f'[{ctx.obj["style"].warning}]{text}[/{ctx.obj["style"].warning}]' if ctx.obj['style'].name == 'no_colour':
return text
return f'[magenta]{text}[/magenta]'

View File

@ -3,15 +3,15 @@
import os import os
from dataclasses import dataclass from dataclasses import dataclass
registry = {} _registry = {}
def register_style(cls): def register_style(cls):
"""Register a style class.""" """Register a style class."""
key = cls.__name__.lower() key = cls.__name__.lower()
if key in registry: if key in _registry:
raise ValueError(f'Style {key} is already registered.') raise ValueError(f'Style {key} is already registered.')
registry[key] = cls _registry[key] = cls
return cls return cls
@ -19,12 +19,11 @@ def register_style(cls):
class Style: class Style:
"""Base class for styles.""" """Base class for styles."""
name: str name: str = 'no_colour'
border: str border: str | None = None
header: str header: str | None = None
cell: str cell: str | None = None
highlight: str highlight: str | None = None
warning: str
no_border: bool = False no_border: bool = False
def __post_init__(self): def __post_init__(self):
@ -34,24 +33,6 @@ class Style:
self.border = None self.border = None
@register_style
@dataclass
class Disabled(Style):
"""Disabled style."""
name: str = 'disabled'
header: str = ''
border: str = 'none'
cell: str = 'none'
highlight: str = 'none'
warning: str = 'none'
def __post__init__(self):
"""Post-initialization to set default values."""
super().__post_init__()
os.environ['NO_COLOR'] = '1'
@register_style @register_style
@dataclass @dataclass
class Red(Style): class Red(Style):
@ -62,7 +43,6 @@ class Red(Style):
border: str = 'dark_red' border: str = 'dark_red'
cell: str = 'red' cell: str = 'red'
highlight: str = 'red3' highlight: str = 'red3'
warning: str = 'magenta'
@register_style @register_style
@ -75,7 +55,6 @@ class Magenta(Style):
border: str = 'dark_magenta' border: str = 'dark_magenta'
cell: str = 'magenta' cell: str = 'magenta'
highlight: str = 'magenta3' highlight: str = 'magenta3'
warning: str = 'magenta'
@register_style @register_style
@ -88,7 +67,6 @@ class Purple(Style):
border: str = 'purple' border: str = 'purple'
cell: str = 'medium_orchid' cell: str = 'medium_orchid'
highlight: str = 'medium_orchid' highlight: str = 'medium_orchid'
warning: str = 'magenta'
@register_style @register_style
@ -101,7 +79,6 @@ class Blue(Style):
border: str = 'dark_blue' border: str = 'dark_blue'
cell: str = 'blue' cell: str = 'blue'
highlight: str = 'blue3' highlight: str = 'blue3'
warning: str = 'magenta'
@register_style @register_style
@ -114,7 +91,6 @@ class Cyan(Style):
border: str = 'dark_cyan' border: str = 'dark_cyan'
cell: str = 'cyan' cell: str = 'cyan'
highlight: str = 'cyan3' highlight: str = 'cyan3'
warning: str = 'magenta'
@register_style @register_style
@ -127,7 +103,6 @@ class Green(Style):
border: str = 'dark_green' border: str = 'dark_green'
cell: str = 'green' cell: str = 'green'
highlight: str = 'green3' highlight: str = 'green3'
warning: str = 'magenta'
@register_style @register_style
@ -140,7 +115,6 @@ class Yellow(Style):
border: str = 'yellow3' border: str = 'yellow3'
cell: str = 'wheat1' cell: str = 'wheat1'
highlight: str = 'yellow3' highlight: str = 'yellow3'
warning: str = 'magenta'
@register_style @register_style
@ -153,7 +127,6 @@ class Orange(Style):
border: str = 'dark_orange' border: str = 'dark_orange'
cell: str = 'orange' cell: str = 'orange'
highlight: str = 'orange3' highlight: str = 'orange3'
warning: str = 'magenta'
@register_style @register_style
@ -166,7 +139,6 @@ class White(Style):
border: str = 'white' border: str = 'white'
cell: str = 'white' cell: str = 'white'
highlight: str = 'white' highlight: str = 'white'
warning: str = 'magenta'
@register_style @register_style
@ -179,7 +151,6 @@ class Grey(Style):
border: str = 'grey50' border: str = 'grey50'
cell: str = 'grey70' cell: str = 'grey70'
highlight: str = 'grey90' highlight: str = 'grey90'
warning: str = 'magenta'
@register_style @register_style
@ -192,7 +163,6 @@ class Navy(Style):
border: str = 'deep_sky_blue4' border: str = 'deep_sky_blue4'
cell: str = 'light_sky_blue3' cell: str = 'light_sky_blue3'
highlight: str = 'light_sky_blue3' highlight: str = 'light_sky_blue3'
warning: str = 'magenta'
@register_style @register_style
@ -205,9 +175,12 @@ class Black(Style):
border: str = 'black' border: str = 'black'
cell: str = 'grey30' cell: str = 'grey30'
highlight: str = 'grey30' highlight: str = 'grey30'
warning: str = 'magenta'
def request_style_obj(style_name: str, no_border: bool) -> Style: def request_style_obj(style_name: str, no_border: bool) -> Style:
"""Request a style object by name.""" """Request a style object by name."""
return registry[style_name.lower()](no_border=no_border) key = style_name.lower()
if key not in _registry:
os.environ['NO_COLOR'] = '1' # Disable colour output
return Style(no_border=no_border)
return _registry[key](no_border=no_border)

View File

@ -10,10 +10,6 @@ def check_mark(ctx: click.Context, value: bool, empty_if_false: bool = False) ->
if empty_if_false and not value: if empty_if_false and not value:
return '' return ''
# rich gracefully handles the absence of colour throughout the rest of the application, if os.getenv('NO_COLOR', '') != '' or ctx.obj['style'].name == 'no_colour':
# but here we must handle it manually.
# If NO_COLOR is set, we return plain text symbols.
# Otherwise, we return coloured symbols.
if os.getenv('NO_COLOR', '') != '':
return '' if value else '' return '' if value else ''
return '' if value else '' return '' if value else ''