-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce `FormattingConfig` and deprecate `DefaultFormats`
- Loading branch information
Showing
33 changed files
with
679 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
|
||
class bcolors: | ||
NC = "\033[0m" # No Color, reset all | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
from prettytable import PrettyTable | ||
|
||
from chispa.bcolors import bcolors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
from dataclasses import dataclass | ||
from __future__ import annotations | ||
|
||
import warnings | ||
from dataclasses import dataclass, field | ||
|
||
|
||
@dataclass | ||
class DefaultFormats: | ||
mismatched_rows = ["red"] | ||
matched_rows = ["blue"] | ||
mismatched_cells = ["red", "underline"] | ||
matched_cells = ["blue"] | ||
""" | ||
This class is now deprecated and should be removed in a future release. | ||
""" | ||
|
||
mismatched_rows: list[str] = field(default_factory=lambda: ["red"]) | ||
matched_rows: list[str] = field(default_factory=lambda: ["blue"]) | ||
mismatched_cells: list[str] = field(default_factory=lambda: ["red", "underline"]) | ||
matched_cells: list[str] = field(default_factory=lambda: ["blue"]) | ||
|
||
def __post_init__(self): | ||
warnings.warn( | ||
"DefaultFormats is deprecated. Use `chispa.formatting.FormattingConfig` instead.", DeprecationWarning | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from chispa.formatting.format_string import format_string | ||
from chispa.formatting.formats import RESET, Color, Format, Style | ||
from chispa.formatting.formatting_config import FormattingConfig | ||
|
||
__all__ = ("Style", "Color", "FormattingConfig", "Format", "format_string", "RESET") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
from chispa.formatting.formats import RESET, Format | ||
|
||
|
||
def format_string(input_string: str, format: Format) -> str: | ||
if not format.color and not format.style: | ||
return input_string | ||
|
||
formatted_string = input_string | ||
codes = [] | ||
|
||
if format.style: | ||
for style in format.style: | ||
codes.append(style.value) | ||
|
||
if format.color: | ||
codes.append(format.color.value) | ||
|
||
formatted_string = "".join(codes) + formatted_string + RESET | ||
return formatted_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from enum import Enum | ||
|
||
RESET = "\033[0m" | ||
|
||
|
||
class Color(str, Enum): | ||
""" | ||
Enum for terminal colors. | ||
Each color is represented by its corresponding ANSI escape code. | ||
""" | ||
|
||
BLACK = "\033[30m" | ||
RED = "\033[31m" | ||
GREEN = "\033[32m" | ||
YELLOW = "\033[33m" | ||
BLUE = "\033[34m" | ||
PURPLE = "\033[35m" | ||
CYAN = "\033[36m" | ||
LIGHT_GRAY = "\033[37m" | ||
DARK_GRAY = "\033[90m" | ||
LIGHT_RED = "\033[91m" | ||
LIGHT_GREEN = "\033[92m" | ||
LIGHT_YELLOW = "\033[93m" | ||
LIGHT_BLUE = "\033[94m" | ||
LIGHT_PURPLE = "\033[95m" | ||
LIGHT_CYAN = "\033[96m" | ||
WHITE = "\033[97m" | ||
|
||
|
||
class Style(str, Enum): | ||
""" | ||
Enum for text styles. | ||
Each style is represented by its corresponding ANSI escape code. | ||
""" | ||
|
||
BOLD = "\033[1m" | ||
UNDERLINE = "\033[4m" | ||
BLINK = "\033[5m" | ||
INVERT = "\033[7m" | ||
HIDE = "\033[8m" | ||
|
||
|
||
@dataclass | ||
class Format: | ||
""" | ||
Data class to represent text formatting with color and style. | ||
Attributes: | ||
color (Color | None): The color for the text. | ||
style (list[Style] | None): A list of styles for the text. | ||
""" | ||
|
||
color: Color | None = None | ||
style: list[Style] | None = None | ||
|
||
@classmethod | ||
def from_dict(cls, format_dict: dict) -> Format: | ||
""" | ||
Create a Format instance from a dictionary. | ||
Args: | ||
format_dict (dict): A dictionary with keys 'color' and/or 'style'. | ||
""" | ||
if not isinstance(format_dict, dict): | ||
raise ValueError("Input must be a dictionary") | ||
|
||
valid_keys = {"color", "style"} | ||
invalid_keys = set(format_dict) - valid_keys | ||
if invalid_keys: | ||
raise ValueError(f"Invalid keys in format dictionary: {invalid_keys}. Valid keys are {valid_keys}") | ||
|
||
color = cls._get_color_enum(format_dict.get("color")) | ||
style = format_dict.get("style") | ||
if isinstance(style, str): | ||
styles = [cls._get_style_enum(style)] | ||
elif isinstance(style, list): | ||
styles = [cls._get_style_enum(s) for s in style] | ||
else: | ||
styles = None | ||
|
||
return cls(color=color, style=styles) | ||
|
||
@classmethod | ||
def from_list(cls, values: list[str]) -> Format: | ||
""" | ||
Create a Format instance from a list of strings. | ||
Args: | ||
values (list[str]): A list of strings representing colors and styles. | ||
""" | ||
if not all(isinstance(value, str) for value in values): | ||
raise ValueError("All elements in the list must be strings") | ||
|
||
color = None | ||
styles = [] | ||
valid_colors = [c.name.lower() for c in Color] | ||
valid_styles = [s.name.lower() for s in Style] | ||
|
||
for value in values: | ||
if value in valid_colors: | ||
color = Color[value.upper()] | ||
elif value in valid_styles: | ||
styles.append(Style[value.upper()]) | ||
else: | ||
raise ValueError( | ||
f"Invalid value: {value}. Valid values are colors: {valid_colors} and styles: {valid_styles}" | ||
) | ||
|
||
return cls(color=color, style=styles if styles else None) | ||
|
||
@staticmethod | ||
def _get_color_enum(color: Color | str | None) -> Color | None: | ||
if isinstance(color, Color): | ||
return color | ||
elif isinstance(color, str): | ||
try: | ||
return Color[color.upper()] | ||
except KeyError: | ||
valid_colors = [c.name.lower() for c in Color] | ||
raise ValueError(f"Invalid color name: {color}. Valid color names are {valid_colors}") | ||
return None | ||
|
||
@staticmethod | ||
def _get_style_enum(style: Style | str | None) -> Style | None: | ||
if isinstance(style, Style): | ||
return style | ||
elif isinstance(style, str): | ||
try: | ||
return Style[style.upper()] | ||
except KeyError: | ||
valid_styles = [f.name.lower() for f in Style] | ||
raise ValueError(f"Invalid style name: {style}. Valid style names are {valid_styles}") | ||
return None |
Oops, something went wrong.