You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This quickly becomes deeply nested, hard to read, and less obvious for newcomers – especially when 3+ styles are used.
Proposed solution
Introduce a new method (on the tint instance and optionally as a module‑level helper) that accepts keyword arguments for foreground color, background color, and text styles.
Example usage:
# Module-level functionfromraztintimportformat_textprint(format_text("Important message", color="red", bg="blue", styles=["bold", "underline"]))
# Or on the tint instancefromraztintimporttintprint(tint.format_text("Important message", color="red", bg="blue", styles=["bold", "underline"]))
The method internally builds the appropriate ANSI sequence by combining the chosen foreground, optional background, and any number of style codes (bold, dim, italic, underline, strikethrough). A reset parameter (default True) can append a reset code at the end.
Detailed design & improvements over a naive implementation
Method naming
While the issue originally used style(), consider format_text() or paint() to avoid confusion with the existing text style functions (bold, underline). We'll keep style() as an alias or choose the clearest name during implementation.
This makes short calls concise and is forward‑compatible.
Reset behaviour – must be explicit
Setting reset=True (default) appends \033[0m after the text, resetting all styles and colors. This is safe for a single call but may break chaining (e.g., two format_text calls on the same line). Better: Make reset=False the default, or clearly document that reset=True is a “hard reset”. Add a reset_only_applied=False option for advanced use (requires tracking which codes were added).
Accept ANSI numeric codes directly
To make the API future‑proof and friendly to power users, color and bg should accept:
Color names (e.g., "red", "bright_green")
Integer ANSI codes (e.g., 31 for red, 44 for blue background)
(Future) RGB tuples – for now raise NotImplementedError or ignore.
If an unknown name or out‑of‑range code is provided, raise ValueError.
Reuse internal low‑level methods
The implementation must not duplicate ANSI logic. Instead, it should call existing methods:
tint.color(text, fg_code) or a new _code_for_color(name)
tint.background(text, bg_code)
internal helpers for style codes (_code_for_style("bold"))
This ensures a single source of truth and consistent behavior with NO_COLOR / FORCE_COLOR.
Problem
Currently, combining multiple styles (e.g., bold + underline + red) requires nested function calls:
This quickly becomes deeply nested, hard to read, and less obvious for newcomers – especially when 3+ styles are used.
Proposed solution
Introduce a new method (on the
tintinstance and optionally as a module‑level helper) that accepts keyword arguments for foreground color, background color, and text styles.Example usage:
The method internally builds the appropriate ANSI sequence by combining the chosen foreground, optional background, and any number of style codes (bold, dim, italic, underline, strikethrough). A
resetparameter (defaultTrue) can append a reset code at the end.Detailed design & improvements over a naive implementation
Method naming
While the issue originally used
style(), considerformat_text()orpaint()to avoid confusion with the existing text style functions (bold,underline). We'll keepstyle()as an alias or choose the clearest name during implementation.Flexible
stylesargumentThe
stylesparameter should accept:styles="bold"styles="bold", "underline"(using*args)styles=["bold", "underline"]This makes short calls concise and is forward‑compatible.
Reset behaviour – must be explicit
Setting
reset=True(default) appends\033[0mafter the text, resetting all styles and colors. This is safe for a single call but may break chaining (e.g., twoformat_textcalls on the same line).Better: Make
reset=Falsethe default, or clearly document thatreset=Trueis a “hard reset”. Add areset_only_applied=Falseoption for advanced use (requires tracking which codes were added).Accept ANSI numeric codes directly
To make the API future‑proof and friendly to power users,
colorandbgshould accept:"red","bright_green")31for red,44for blue background)NotImplementedErroror ignore.If an unknown name or out‑of‑range code is provided, raise
ValueError.Reuse internal low‑level methods
The implementation must not duplicate ANSI logic. Instead, it should call existing methods:
tint.color(text, fg_code)or a new_code_for_color(name)tint.background(text, bg_code)_code_for_style("bold"))This ensures a single source of truth and consistent behavior with
NO_COLOR/FORCE_COLOR.Handling invalid styles and colors
VALID_STYLES = {"bold","dim","italic","underline","strikethrough"}.VALID_STYLES, raiseValueError.colororbgname is not recognised, raiseValueError.Interaction with environment / color toggles
tint.use_color(or the global detection state).NO_COLOR, not a TTY, etc.), return plain text without any ANSI codes (including no reset).RAZTINT_FORCE_COLORis set, always output ANSI codes even without a TTY.Documentation examples in README
The README should show:
format_text("Hello", color="red", styles="bold")format_text("Alert", color="white", bg="red", styles=["bold", "underline"])reset=Falsefor concatenation:9. Acceptance criteria (updated)
format_text()(orstyle()) is available on theRazTintclass and as a module‑level helper.text: str,color: str | int | None = None,bg: str | int | None = None,styles: str | list[str] | None = None,reset: bool = True.colorandbgaccept named ANSI colors (e.g.,"red","bright_green") and integer codes (31,44).stylesaccepts a single style string, a list, or multiple string arguments.red(),bold(),bg_blue(), …) remain unchanged and work alongside the new method.ValueError.black, passesruff check, and passestytype checking."",None(treated as empty string), mutually exclusive styles (bold+dim– documented as unpredictable), nested calls.Additional considerations
reset=Falseis used, the caller is responsible for resetting styles later to avoid “bleeding” into subsequent output.