@@ -222,10 +222,17 @@ def print(
222222 end : str = "\n " ,
223223 flush : bool = True ,
224224 ) -> None :
225- """A print function, whose print values can be formatted using formatting codes.\n
226- -----------------------------------------------------------------------------------
227- For exact information about how to use special formatting codes, see the
228- `format_codes` module documentation."""
225+ """A print function, whose print `values` can be formatted using formatting codes.\n
226+ --------------------------------------------------------------------------------------------------
227+ - `values` -⠀the values to print
228+ - `default_color` -⠀the default text color to use if no other text color was applied
229+ - `brightness_steps` -⠀the amount to increase/decrease default-color brightness per modifier code
230+ - `sep` -⠀the separator to use between multiple values
231+ - `end` -⠀the string to append at the end of the printed values
232+ - `flush` -⠀whether to flush the output buffer after printing\n
233+ --------------------------------------------------------------------------------------------------
234+ For exact information about how to use special formatting codes,
235+ see the `format_codes` module documentation."""
229236 FormatCodes .__config_console ()
230237 _sys .stdout .write (FormatCodes .to_ansi (sep .join (map (str , values )) + end , default_color , brightness_steps ))
231238 if flush :
@@ -238,11 +245,14 @@ def input(
238245 brightness_steps : int = 20 ,
239246 reset_ansi : bool = False ,
240247 ) -> str :
241- """An input, whose prompt can be formatted using formatting codes.\n
242- -------------------------------------------------------------------------
243- If `reset_ansi` is true, all ANSI formatting will be reset, after the
244- user confirms the input and the program continues to run.\n
245- -------------------------------------------------------------------------
248+ """An input, whose `prompt` can be formatted using formatting codes.\n
249+ --------------------------------------------------------------------------------------------------
250+ - `prompt` -⠀the prompt to show to the user
251+ - `default_color` -⠀the default text color to use if no other text color was applied
252+ - `brightness_steps` -⠀the amount to increase/decrease default-color brightness per modifier code
253+ - `reset_ansi` -⠀if true, all ANSI formatting will be reset, after the user confirmed the input
254+ and the program continues to run\n
255+ --------------------------------------------------------------------------------------------------
246256 For exact information about how to use special formatting codes, see the
247257 `format_codes` module documentation."""
248258 FormatCodes .__config_console ()
@@ -260,9 +270,15 @@ def to_ansi(
260270 _validate_default : bool = True ,
261271 ) -> str :
262272 """Convert the formatting codes inside a string to ANSI formatting.\n
263- -------------------------------------------------------------------------
264- For exact information about how to use special formatting codes, see the
265- `format_codes` module documentation."""
273+ --------------------------------------------------------------------------------------------------
274+ - `string` -⠀the string that contains the formatting codes to convert
275+ - `default_color` -⠀the default text color to use if no other text color was applied
276+ - `brightness_steps` -⠀the amount to increase/decrease default-color brightness per modifier code
277+ - `_default_start` -⠀whether to start the string with the `default_color` ANSI code, if set
278+ - `_validate_default` -⠀whether to validate the `default_color` before use\n
279+ --------------------------------------------------------------------------------------------------
280+ For exact information about how to use special formatting codes,
281+ see the `format_codes` module documentation."""
266282 if not isinstance (string , str ):
267283 string = str (string )
268284 if _validate_default :
@@ -353,20 +369,21 @@ def replace_keys(match: Match) -> str:
353369 return (((FormatCodes .__get_default_ansi (default_color ) or "" ) if _default_start else "" )
354370 + string ) if default_color is not None else string
355371
356- @staticmethod
357- def escape_ansi (ansi_string : str ) -> str :
358- """Escapes all ANSI codes in the string, so they are visible when output to the console."""
359- return ansi_string .replace (ANSI .CHAR , ANSI .ESCAPED_CHAR )
360-
361372 @staticmethod
362373 def escape (string : str , default_color : Optional [Rgba | Hexa ] = None , _escape_char : Literal ["/" , "\\ " ] = "/" ) -> str :
363374 """Escapes all valid formatting codes in the string, so they are visible when output
364375 to the console using `FormatCodes.print()`. Invalid formatting codes remain unchanged.\n
365376 -----------------------------------------------------------------------------------------
366- For exact information about how to use special formatting codes, see the
367- `format_codes` module documentation."""
377+ - `string` -⠀the string that contains the formatting codes to escape
378+ - `default_color` -⠀the default text color to use if no other text color was applied
379+ - `_escape_char` -⠀the character to use to escape formatting codes (`/` or `\\ `)\n
380+ -----------------------------------------------------------------------------------------
381+ For exact information about how to use special formatting codes,
382+ see the `format_codes` module documentation."""
368383 if not isinstance (string , str ):
369384 string = str (string )
385+ if not _escape_char in {"/" , "\\ " }:
386+ raise ValueError ("'_escape_char' must be either '/' or '\\ '" )
370387
371388 use_default , default_color = FormatCodes .__validate_default_color (default_color )
372389
@@ -404,17 +421,45 @@ def escape_format_code(match: Match) -> str:
404421
405422 return "\n " .join (_COMPILED ["formatting" ].sub (escape_format_code , l ) for l in string .split ("\n " ))
406423
424+ @staticmethod
425+ def escape_ansi (ansi_string : str ) -> str :
426+ """Escapes all ANSI codes in the string, so they are visible when output to the console.\n
427+ -------------------------------------------------------------------------------------------
428+ - `ansi_string` -⠀the string that contains the ANSI codes to escape"""
429+ return ansi_string .replace (ANSI .CHAR , ANSI .ESCAPED_CHAR )
430+
431+ @staticmethod
432+ def remove (
433+ string : str ,
434+ default_color : Optional [Rgba | Hexa ] = None ,
435+ get_removals : bool = False ,
436+ _ignore_linebreaks : bool = False ,
437+ ) -> str | tuple [str , tuple [tuple [int , str ], ...]]:
438+ """Removes all formatting codes from the string with optional tracking of removed codes.\n
439+ --------------------------------------------------------------------------------------------------------
440+ - `string` -⠀the string that contains the formatting codes to remove
441+ - `default_color` -⠀the default text color to use if no other text color was applied
442+ - `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
443+ where each tuple contains the position of the removed formatting code and the removed formatting code
444+ - `_ignore_linebreaks` -⠀whether to ignore line breaks for the removal positions"""
445+ return FormatCodes .remove_ansi (
446+ FormatCodes .to_ansi (string , default_color = default_color ),
447+ get_removals = get_removals ,
448+ _ignore_linebreaks = _ignore_linebreaks ,
449+ )
450+
407451 @staticmethod
408452 def remove_ansi (
409453 ansi_string : str ,
410454 get_removals : bool = False ,
411455 _ignore_linebreaks : bool = False ,
412456 ) -> str | tuple [str , tuple [tuple [int , str ], ...]]:
413- """Removes all ANSI codes from the string.\n
414- --------------------------------------------------------------------------------------------------
415- If `get_removals` is true, additionally to the cleaned string, a list of tuples will be returned.
416- Each tuple contains the position of the removed ansi code and the removed ansi code.\n
417- If `_ignore_linebreaks` is true, linebreaks will be ignored for the removal positions."""
457+ """Removes all ANSI codes from the string with optional tracking of removed codes.\n
458+ ---------------------------------------------------------------------------------------------------
459+ - `ansi_string` -⠀the string that contains the ANSI codes to remove
460+ - `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
461+ where each tuple contains the position of the removed ansi code and the removed ansi code
462+ - `_ignore_linebreaks` -⠀whether to ignore line breaks for the removal positions"""
418463 if get_removals :
419464 removals = []
420465
@@ -433,24 +478,6 @@ def replacement(match: Match) -> str:
433478 else :
434479 return _COMPILED ["ansi_seq" ].sub ("" , ansi_string )
435480
436- @staticmethod
437- def remove (
438- string : str ,
439- default_color : Optional [Rgba | Hexa ] = None ,
440- get_removals : bool = False ,
441- _ignore_linebreaks : bool = False ,
442- ) -> str | tuple [str , tuple [tuple [int , str ], ...]]:
443- """Removes all formatting codes from the string.\n
444- ---------------------------------------------------------------------------------------------------
445- If `get_removals` is true, additionally to the cleaned string, a list of tuples will be returned.
446- Each tuple contains the position of the removed formatting code and the removed formatting code.\n
447- If `_ignore_linebreaks` is true, linebreaks will be ignored for the removal positions."""
448- return FormatCodes .remove_ansi (
449- FormatCodes .to_ansi (string , default_color = default_color ),
450- get_removals = get_removals ,
451- _ignore_linebreaks = _ignore_linebreaks ,
452- )
453-
454481 @staticmethod
455482 def __config_console () -> None :
456483 """Configure the console to be able to interpret ANSI formatting."""
0 commit comments