ubelt.util_colors module¶
-
ubelt.util_colors.
highlight_code
(text, lexer_name='python', **kwargs)[source]¶ Highlights a block of text using ansii tags based on language syntax.
Parameters: - text (str) – plain text to highlight
- lexer_name (str) – name of language
- **kwargs – passed to pygments.lexers.get_lexer_by_name
Returns: - text : highlighted text
If pygments is not installed, the plain text is returned.
Return type: str
- CommandLine:
- python -c “import pygments.formatters; print(list(pygments.formatters.get_all_formatters()))”
Example
>>> import ubelt as ub >>> text = 'import ubelt as ub; print(ub)' >>> new_text = ub.highlight_code(text) >>> print(new_text)
-
ubelt.util_colors.
color_text
(text, color)[source]¶ Colorizes text a single color using ansii tags.
Parameters: - text (str) – text to colorize
- color (str) – may be one of the following: yellow, blink, lightgray, underline, darkyellow, blue, darkblue, faint, fuchsia, black, white, red, brown, turquoise, bold, darkred, darkgreen, reset, standout, darkteal, darkgray, overline, purple, green, teal, fuscia
Returns: - text : colorized text.
If pygments is not installed plain text is returned.
Return type: str
- CommandLine:
- python -c “import pygments.console; print(sorted(pygments.console.codes.keys()))” python -m ubelt.util_colors color_text
Example
>>> from ubelt.util_colors import * # NOQA >>> text = 'raw text' >>> assert color_text(text, 'red') == '\x1b[31;01mraw text\x1b[39;49;00m' >>> assert color_text(text, None) == 'raw text'