Display text in various foreground/background colors.
usage: color.py [-h] [--test] [-r] [-b BACKGROUND_COLOR] [-p] [-v] [color] [text ...]
Print text in the specified color
positional arguments:
color Choose a foreground color: default, black, darkred, darkgreen, darkyellow, darkblue, darkmagenta, darkcyan, lightgray, darkgray, red, green,
orange, blue, magenta, cyan, white
text Text to display
options:
-h, --help show this help message and exit
--test Test all colors
-r, --reset Reset a console to default color only
-b BACKGROUND_COLOR, --background-color BACKGROUND_COLOR
Choose a background color: default, black, darkred, darkgreen, darkyellow, darkblue, darkmagenta, darkcyan, lightgray, darkgray, red, green,
orange, yellow, blue, magenta, purple, cyan, white
-p, --persist Set a console to the specified color
-v, --verbose Enable debugging
Argument | Description | Default |
---|---|---|
color |
The foreground color. Choices:
|
None, the foreground color is required |
text |
Text to display | If not specified, the script will read text from stdin. Either text must be specified or stdin must be directed - the script will not read text directly from the terminal |
Option | Description | Default |
---|---|---|
--test |
Displays sample colors | The default is to print custom text in the specified color |
--reset |
Reset the foreground/background colors o the default | The default is to print custom text in the specified color |
--background-color |
Specifies the background color | The default is use the current background color |
--presist |
Sets foreground/background colors permanently until changed or reset | The default is to print custom text in the specified color |
-v |
Enable verbose debugging | Debugging is not enabled |
Note: The background is white so the white text doesn't appear!
Note: I'm not crazy about the lingering background color when the prompt returns. It doesn't seem to linger long... at least you don't have to use --reset
!
The class could be used by another script - in fact, that's how the --color
option of the banner
does just that!
Note: It helps when color.py
is in the same directory as the script using the class. I often do that as a symbolic link:
$ ln -s ~/bin/color.py .
$
- I typically use the script without the
.py
extension through the use of a very simple alias in my~/.bashrc
bash login script:alias color=color.py
- The color names are pretty flexible:
- A color such as
lightgrey
can be expressed aslight-grey
,light gray
(that's not a typo!), or even its shortest abbreviation such aslightg
- Ambiguous colors result in an error:
$ date | color b usage: color.py [-h] [--test] [-r] [-b BACKGROUND_COLOR] [-p] [-v] [color] [text ...] color.py: error: argument color: 'b' is ambiguous: black, blue $
- A color such as
- yellow and purple are technically not supported and that pisses me off. To me, orange and magenta are close enough (respectively) so I'm making them equivalent.