(should arguably be in extras) Eases production of colors in the terminal, mostly for a few command line debug tools.
Tries to only produce color escapes when the terminal supports it: (whether we detect we are in a tty, and TERM suggests we are color-capable). You can override that in manual checks with the arguments on guess_color_support(), You can override that in the automatic checks (necessary for the convenience function) by setting the globals default_forceifnoterm and/or default_forceifnotty)
Currently provides some convenience functions for a single foreground color, meant to be used like: :
import helpers.shellcolor as sc print( sc.red('shown as red') )
Or without checking it is suported: :
print( sc.BRIGHT+sc.BLUE+sc.UNDERLINE+'shown bright blue'+sc.RESET )
Tries to only add the control codes when the context seems capable of it. (TODO: this needs some tuning) check whether we are in a tty, and whether the TERM suggests we are color-capable. You can override that in manual checks with the arguments on guess_color_support(), You can override that in the automatic checks (necessary for the convenience function) by setting the globals default_forceifnoterm and/or default_forceifnotty
Function | _add |
Undocumented |
Function | _format |
Helper for cformat() (NOW REDUNDANT?) e.g. : |
Function | _percent |
Will rewrite any format strings with a width to have more width by the amount specified. |
Function | _strip |
Undocumented |
Function | bgblack |
add color on string if supported |
Function | bgblue |
add color on string if supported |
Function | bgcyan |
add color on string if supported |
Function | bggreen |
add color on string if supported |
Function | bgmagenta |
add color on string if supported |
Function | bgorange |
add color on string if supported |
Function | bgred |
add color on string if supported |
Function | bgyellow |
add color on string if supported |
Function | black |
add color on string if supported |
Function | blend |
add an RGB color around a string that is some fraction between two colors Note this only really makes sense on true-color terminals. |
Function | blue |
add color on string if supported |
Function | brightblack |
add color on string if supported |
Function | brightblue |
add color on string if supported |
Function | brightcyan |
add color on string if supported |
Function | brightgray |
add color on string if supported |
Function | brightgreen |
add color on string if supported |
Function | brightgrey |
add color on string if supported |
Function | brightmagenta |
add color on string if supported |
Function | brightred |
add color on string if supported |
Function | brightyellow |
add color on string if supported |
Function | cformat |
EXPERIMENT: A percent formatter that is aware that color escapes have zero display width, so you can feed it strings with color escapes and avoid some magic reindenting weirdness. |
Function | clearscreen |
output clear-screen code, if supported |
Function | closest |
Given a r,g,b color (0..255 scale), pick the closest shell color (returns the function) |
Function | color |
color string s according to where v lies between fromv and tov, ...from a discrete set of colors (if you wanted something more gradual, perhaps use redgreen (if you like the fixed colors), or do it yourself with e... |
Function | cyan |
add color on string if supported |
Function | darkgray |
add color on string if supported |
Function | darkgrey |
add color on string if supported |
Function | default |
add "default colors" code before string if supported |
Function | gray |
add color on string if supported |
Function | green |
add color on string if supported |
Function | grey |
add color on string if supported |
Function | guess |
Tries to guess whether we can use color code output. |
Function | hash |
return string wrapped in a (non-black basic shell) color (and RESET after) based on (a hash of) the string |
Function | magenta |
add color on string if supported |
Function | orange |
add color on string if supported |
Function | real |
Returns 2-tuple: |
Function | red |
add color on string if supported |
Function | redgreen |
turns a fraction in 0..1 to red..green |
Function | reset |
add color reset code before string if supported |
Function | supported |
return what guess_color_support() estimated. (if that was not run yet, do that before returning) |
Function | true |
true-color escape from 0..255 r,g,b |
Function | truncate |
Truncate a string after so-many real characters. Written for "truncate colored text according to the terminal width" functionality. |
Function | tty |
fetches current terminal size |
Function | white |
add color on string if supported |
Function | yellow |
add color on string if supported |
Constant | BGBLACK |
Undocumented |
Constant | BGBLUE |
Undocumented |
Constant | BGBRIGHTGRAY |
Undocumented |
Constant | BGCYAN |
Undocumented |
Constant | BGGREEN |
Undocumented |
Constant | BGMAGENTA |
Undocumented |
Constant | BGRED |
Undocumented |
Constant | BGYELLOW |
Undocumented |
Constant | BLACK |
Undocumented |
Constant | BLUE |
Undocumented |
Constant | BRIGHT |
Undocumented |
Constant | BRIGHTBLACK |
Undocumented |
Constant | BRIGHTBLUE |
Undocumented |
Constant | BRIGHTCYAN |
Undocumented |
Constant | BRIGHTGREEN |
Undocumented |
Constant | BRIGHTGREY |
Undocumented |
Constant | BRIGHTMAGENTA |
Undocumented |
Constant | BRIGHTRED |
Undocumented |
Constant | BRIGHTYELLOW |
Undocumented |
Constant | CLEARSCREEN |
Undocumented |
Constant | CYAN |
Undocumented |
Constant | DEFAULT |
Undocumented |
Constant | DEFAULT |
Undocumented |
Constant | DEFAULT |
Undocumented |
Constant | ERASEDISP |
Undocumented |
Constant | ERASELINE |
Undocumented |
Constant | GOTO00 |
Undocumented |
Constant | GREEN |
Undocumented |
Constant | GREY |
Undocumented |
Constant | MAGENTA |
Undocumented |
Constant | NOCOLOR |
Undocumented |
Constant | RED |
Undocumented |
Constant | RESET |
Undocumented |
Constant | UNDERLINE |
Undocumented |
Constant | YELLOW |
Undocumented |
Variable | _guess |
Undocumented |
Variable | ere |
Undocumented |
Variable | pre |
Undocumented |
Helper for cformat() (NOW REDUNDANT?) e.g. :
' \x1b[33mfork\x1b[0m\x1b[39m'
becomes :
[' ', '\x1b[33m', 'fork', '\x1b[0m', '\x1b[39m']
The numbers are the bytestring length and the print length
Will rewrite any format strings with a width to have more width by the amount specified.
The add sequence must have as many items as there are format strings.
TODO: deal with %r sensibly?
add an RGB color around a string that is some fraction between two colors Note this only really makes sense on true-color terminals.
Parameters | |
s | Undocumented |
frac:float | Undocumented |
rgb1 | Undocumented |
rgb2 | Undocumented |
EXPERIMENT: A percent formatter that is aware that color escapes have zero display width, so you can feed it strings with color escapes and avoid some magic reindenting weirdness.
cformat(arg1,arg2) acts like arg1%arg2
e.g. cformat('%20s', (WHITE+'fork'+RESET,) ) == ' \x1b[1;37mfork\x1b[0m\x1b[39m' instead of '\x1b[1;37mfork\x1b[0m\x1b[39m'
Assumption is that escapes have zero width, which is true for colors but not for weirder things.
Given a r,g,b color (0..255 scale), pick the closest shell color (returns the function)
The brightness that color is rendered as depends on the terminal, see e.g. https://en.wikipedia.org/wiki/ANSI_escape_code#3.2F4_bit
To get some semblance of control, you can control the brightness the non-bright color is assumed to have. (hint: you can cheat your way to less of the garish bright colors by upping mid, or more by lowering it)
You can completely avoid the bright colors by specifying nobright=True
color string s according to where v lies between fromv and tov, ...from a discrete set of colors (if you wanted something more gradual, perhaps use redgreen (if you like the fixed colors), or do it yourself with e.g. true_colf)
Parameters | |
s:str | Undocumented |
v:float | Undocumented |
fromv | Undocumented |
tov | Undocumented |
colors | Undocumented |
Tries to guess whether we can use color code output.
If we are not on a tty/pty (usually a pipe), return False If the TERM mentions we can, returns True, if it mensions we cannot, return False If we do not know, return the fallback (by default is True)
TODO: fix the logic for this, it does not do exactly what was intended.
User code should probably always prefer supported(), and only use guess_color_support if it wants a different test or fallback later.
return string wrapped in a (non-black basic shell) color (and RESET after) based on (a hash of) the string
If you want to color an entire string based on just a part of it, hand the part into hash_instead
TODO: variant that gives just the escapes, so we can avoid calling this a lot
Parameters | |
s | Undocumented |
rgb |
|
append | Undocumented |
prepend | Undocumented |
hash | if you want to color a verbose string based on just part of it, hand the color-determining part into hash_instead, and the whole into s |
on |
by default we don't care, which might be good for area fills |
Returns 2-tuple:
- amount of printed characters,
- amount spent in control codes (which is calculated as the byte length minus the first part)
Truncate a string after so-many real characters. Written for "truncate colored text according to the terminal width" functionality.
Parameters | |
s | Undocumented |
targetlen | Undocumented |
append | By default, it appends a reset to default colors, so that it doesn't leave things as the last-used color. To avoid that, say append='' |