add --style validation

add Disabled class to style registry

patch bump
This commit is contained in:
onyx-and-iris 2025-06-22 12:35:21 +01:00
parent 55a7da67db
commit 4632260961
3 changed files with 46 additions and 42 deletions

View File

@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online> # SPDX-FileCopyrightText: 2025-present onyx-and-iris <code@onyxandiris.online>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
__version__ = "0.18.3" __version__ = "0.18.4"

View File

@ -50,6 +50,15 @@ def setup_logging(debug: bool):
) )
def validate_style(value: str):
"""Validate and return the style."""
if value not in styles.registry:
raise typer.BadParameter(
f'Invalid style: {value}. Available styles: {", ".join(styles.registry.keys())}'
)
return value
@app.callback() @app.callback()
def main( def main(
ctx: typer.Context, ctx: typer.Context,
@ -112,6 +121,7 @@ def main(
envvar='OBS_STYLE', envvar='OBS_STYLE',
help='Set the style for the CLI output', help='Set the style for the CLI output',
show_default='disabled', show_default='disabled',
callback=validate_style,
), ),
] = settings.get('style'), ] = settings.get('style'),
no_border: Annotated[ no_border: Annotated[

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,11 +19,10 @@ def register_style(cls):
class Style: class Style:
"""Base class for styles.""" """Base class for styles."""
name: str = 'no_colour' name: str
description: str = 'Style disabled' border: str
border: str = 'none' column: str
column: str = 'none' highlight: str
highlight: str = 'none'
no_border: bool = False no_border: bool = False
def __post_init__(self): def __post_init__(self):
@ -32,9 +31,21 @@ class Style:
if self.no_border: if self.no_border:
self.border = None self.border = None
def __str__(self):
"""Return a string representation of the style.""" @register_style
return f'{self.name} - {self.description}' @dataclass
class Disabled(Style):
"""Disabled style."""
name: str = 'disabled'
border: str = 'none'
column: str = 'none'
highlight: str = 'none'
def __post_init__(self):
"""Post-initialization to set default values."""
super().__post_init__()
os.environ['NO_COLOR'] = '1'
@register_style @register_style
@ -43,10 +54,9 @@ class Red(Style):
"""Red style.""" """Red style."""
name: str = 'red' name: str = 'red'
description: str = 'Red text color'
border: str = 'red3' border: str = 'red3'
highlight: str = 'red1'
column: str = 'red1' column: str = 'red1'
highlight: str = 'red1'
@register_style @register_style
@ -55,10 +65,9 @@ class Magenta(Style):
"""Magenta style.""" """Magenta style."""
name: str = 'magenta' name: str = 'magenta'
description: str = 'Magenta text color'
border: str = 'magenta3' border: str = 'magenta3'
highlight: str = 'orchid1'
column: str = 'orchid1' column: str = 'orchid1'
highlight: str = 'orchid1'
@register_style @register_style
@ -67,10 +76,9 @@ class Purple(Style):
"""Purple style.""" """Purple style."""
name: str = 'purple' name: str = 'purple'
description: str = 'Purple text color'
border: str = 'medium_purple4' border: str = 'medium_purple4'
highlight: str = 'medium_purple'
column: str = 'medium_purple' column: str = 'medium_purple'
highlight: str = 'medium_purple'
@register_style @register_style
@ -79,10 +87,9 @@ class Blue(Style):
"""Blue style.""" """Blue style."""
name: str = 'blue' name: str = 'blue'
description: str = 'Blue text color'
border: str = 'cornflower_blue' border: str = 'cornflower_blue'
highlight: str = 'sky_blue2'
column: str = 'sky_blue2' column: str = 'sky_blue2'
highlight: str = 'sky_blue2'
@register_style @register_style
@ -91,10 +98,9 @@ class Cyan(Style):
"""Cyan style.""" """Cyan style."""
name: str = 'cyan' name: str = 'cyan'
description: str = 'Cyan text color'
border: str = 'dark_cyan' border: str = 'dark_cyan'
highlight: str = 'cyan'
column: str = 'cyan' column: str = 'cyan'
highlight: str = 'cyan'
@register_style @register_style
@ -103,10 +109,9 @@ class Green(Style):
"""Green style.""" """Green style."""
name: str = 'green' name: str = 'green'
description: str = 'Green text color'
border: str = 'green4' border: str = 'green4'
highlight: str = 'spring_green3'
column: str = 'spring_green3' column: str = 'spring_green3'
highlight: str = 'spring_green3'
@register_style @register_style
@ -115,10 +120,9 @@ class Yellow(Style):
"""Yellow style.""" """Yellow style."""
name: str = 'yellow' name: str = 'yellow'
description: str = 'Yellow text color'
border: str = 'yellow3' border: str = 'yellow3'
highlight: str = 'wheat1'
column: str = 'wheat1' column: str = 'wheat1'
highlight: str = 'wheat1'
@register_style @register_style
@ -127,10 +131,9 @@ class Orange(Style):
"""Orange style.""" """Orange style."""
name: str = 'orange' name: str = 'orange'
description: str = 'Orange text color'
border: str = 'dark_orange' border: str = 'dark_orange'
highlight: str = 'orange1'
column: str = 'orange1' column: str = 'orange1'
highlight: str = 'orange1'
@register_style @register_style
@ -139,10 +142,9 @@ class White(Style):
"""White style.""" """White style."""
name: str = 'white' name: str = 'white'
description: str = 'White text color'
border: str = 'grey82' border: str = 'grey82'
highlight: str = 'grey100'
column: str = 'grey100' column: str = 'grey100'
highlight: str = 'grey100'
@register_style @register_style
@ -151,10 +153,9 @@ class Grey(Style):
"""Grey style.""" """Grey style."""
name: str = 'grey' name: str = 'grey'
description: str = 'Grey text color'
border: str = 'grey50' border: str = 'grey50'
highlight: str = 'grey70'
column: str = 'grey70' column: str = 'grey70'
highlight: str = 'grey70'
@register_style @register_style
@ -163,10 +164,9 @@ class Navy(Style):
"""Navy Blue style.""" """Navy Blue style."""
name: str = 'navyblue' name: str = 'navyblue'
description: str = 'Navy Blue text color'
border: str = 'deep_sky_blue4' border: str = 'deep_sky_blue4'
highlight: str = 'light_sky_blue3'
column: str = 'light_sky_blue3' column: str = 'light_sky_blue3'
highlight: str = 'light_sky_blue3'
@register_style @register_style
@ -175,17 +175,11 @@ class Black(Style):
"""Black style.""" """Black style."""
name: str = 'black' name: str = 'black'
description: str = 'Black text color'
border: str = 'grey19' border: str = 'grey19'
column: str = 'grey11' column: str = 'grey11'
highlight: str = 'grey11'
def request_style_obj(style_name: str, no_border: bool) -> Style: def request_style_obj(style_name: str, no_border: bool) -> Style:
"""Entry point for style objects. Returns a Style object based on the style name.""" """Entry point for style objects. Returns a Style object based on the style name."""
style_name = str(style_name).lower() # coerce the type to string and lowercase it return registry[style_name.lower()](no_border=no_border)
if style_name not in _registry:
os.environ['NO_COLOR'] = '1' # Disable colour output
return Style()
return _registry[style_name](no_border=no_border)