refactor: Consolidate text color decorators and update related properties

This commit is contained in:
sha
2026-01-04 20:49:55 +00:00
parent 3f8b158135
commit 336b030a6b
11 changed files with 136 additions and 280 deletions
+8 -3
View File
@@ -1,6 +1,6 @@
class SizeFormatter:
"""Class for formatting file sizes"""
@staticmethod
def format_size(bytes_size: int) -> str:
"""Format bytes to human readable with unit"""
@@ -9,9 +9,14 @@ class SizeFormatter:
return f"{bytes_size:.1f} {unit}"
bytes_size /= 1024
return f"{bytes_size:.1f} TB"
@staticmethod
def format_size_full(bytes_size: int) -> str:
"""Format size with both human readable and bytes"""
size_formatted = SizeFormatter.format_size(bytes_size)
return f"{size_formatted} ({bytes_size:,} bytes)"
return f"{size_formatted} ({bytes_size:,} bytes)"
@staticmethod
def format_size_short(bytes_size: int) -> str:
"""Format size with only human readable"""
return SizeFormatter.format_size(bytes_size)
+3 -94
View File
@@ -42,106 +42,15 @@ class TextDecorators:
return decorator
@staticmethod
def green() -> Callable:
"""Decorator to color text green."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if result == "":
return ""
return TextFormatter.green(str(result))
return wrapper
return decorator
@staticmethod
def yellow() -> Callable:
"""Decorator to color text yellow."""
def colour(name) -> Callable:
"""Decorator to colour text."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.yellow(str(result))
return wrapper
return decorator
@staticmethod
def cyan() -> Callable:
"""Decorator to color text cyan."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.cyan(str(result))
return wrapper
return decorator
@staticmethod
def magenta() -> Callable:
"""Decorator to color text magenta."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.magenta(str(result))
return wrapper
return decorator
@staticmethod
def red() -> Callable:
"""Decorator to color text red."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.red(str(result))
return wrapper
return decorator
@staticmethod
def orange() -> Callable:
"""Decorator to color text orange."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.orange(str(result))
return wrapper
return decorator
@staticmethod
def blue() -> Callable:
"""Decorator to color text blue."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.blue(str(result))
return wrapper
return decorator
@staticmethod
def grey() -> Callable:
"""Decorator to color text grey."""
def decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs) -> str:
result = func(*args, **kwargs)
if not result:
return ""
return TextFormatter.grey(str(result))
return TextFormatter.colour(name, str(result))
return wrapper
return decorator
+14 -53
View File
@@ -27,84 +27,45 @@ class TextFormatter:
return ''.join(word.capitalize() for word in text.split())
@staticmethod
def bold_green(text: str) -> str:
"""Deprecated: Use [TextFormatter.bold, TextFormatter.green] instead"""
import warnings
warnings.warn(
"TextFormatter.bold_green is deprecated. Use [TextFormatter.bold, TextFormatter.green] instead.",
DeprecationWarning,
stacklevel=2
)
return f"[bold green]{text}[/bold green]"
@staticmethod
def bold_cyan(text: str) -> str:
"""Deprecated: Use [TextFormatter.bold, TextFormatter.cyan] instead"""
import warnings
warnings.warn(
"TextFormatter.bold_cyan is deprecated. Use [TextFormatter.bold, TextFormatter.cyan] instead.",
DeprecationWarning,
stacklevel=2
)
return f"[bold cyan]{text}[/bold cyan]"
@staticmethod
def bold_magenta(text: str) -> str:
"""Deprecated: Use [TextFormatter.bold, TextFormatter.magenta] instead"""
import warnings
warnings.warn(
"TextFormatter.bold_magenta is deprecated. Use [TextFormatter.bold, TextFormatter.magenta] instead.",
DeprecationWarning,
stacklevel=2
)
return f"[bold magenta]{text}[/bold magenta]"
@staticmethod
def bold_yellow(text: str) -> str:
"""Deprecated: Use [TextFormatter.bold, TextFormatter.yellow] instead"""
import warnings
warnings.warn(
"TextFormatter.bold_yellow is deprecated. Use [TextFormatter.bold, TextFormatter.yellow] instead.",
DeprecationWarning,
stacklevel=2
)
return f"[bold yellow]{text}[/bold yellow]"
def colour(colour_name: str, text: str) -> str:
"""Generic method to color text with given colour name."""
return f"[{colour_name}]{text}[/{colour_name}]"
@staticmethod
def green(text: str) -> str:
return f"[green]{text}[/green]"
return TextFormatter.colour("green", text)
@staticmethod
def yellow(text: str) -> str:
return f"[yellow]{text}[/yellow]"
return TextFormatter.colour("yellow", text)
@staticmethod
def orange(text: str) -> str:
return f"[orange]{text}[/orange]"
return TextFormatter.colour("orange", text)
@staticmethod
def magenta(text: str) -> str:
return f"[magenta]{text}[/magenta]"
return TextFormatter.colour("magenta", text)
@staticmethod
def cyan(text: str) -> str:
return f"[cyan]{text}[/cyan]"
return TextFormatter.colour("cyan", text)
@staticmethod
def red(text: str) -> str:
return f"[red]{text}[/red]"
return TextFormatter.colour("red", text)
@staticmethod
def blue(text: str) -> str:
return f"[blue]{text}[/blue]"
return TextFormatter.colour("blue", text)
@staticmethod
def grey(text: str) -> str:
return f"[grey]{text}[/grey]"
return TextFormatter.colour("grey", text)
@staticmethod
def dim(text: str) -> str:
return f"[dim]{text}[/dim]"
return TextFormatter.colour("dimgray", text)
@staticmethod
def link(url: str, text: str | None = None) -> str:
@@ -119,4 +80,4 @@ class TextFormatter:
if url and url != "<None>" and url.startswith("http"):
# Use OSC 8 hyperlink escape sequence for clickable links
return f"\x1b]8;;{url}\x1b\\Open in TMDB\x1b]8;;\x1b\\"
return url
return url