# colortextpy
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
Colortextpy is a Python package for adding colors and styles to terminal output, allowing you to create more visually appealing and organized command-line applications. The full `colortextpy` documentation is available at [https://susuky.github.io/colortextpy/](https://susuky.github.io/colortextpy/)
## Install
Tested on python 3.6-3.11, win11, win11 WSL2, Ubuntu
It doesn’t support win32
``` sh
pip install colortextpy
```
## Usage
### [Color printer](https://susuky.github.io/colortextpy/printer.html)
``` python
from colortextpy import Printer, colorprint
Printer.blue_print('blue')
Printer.blue_print('blue', bold=True)
colorprint('default')
colorprint('#ff3567', color='#ff3567')
colorprint('#123456', color=Fore['#123456'])
colorprint(4, color=4)
colorprint(137, color=Fore['137'])
colorprint('(50, 234, 33)', color=(50, 234, 33))
colorprint('(50, 24, 133)', color='(50, 24, 133)')
colorprint('dark_green', color=Fore.dark_green)
colorprint('violet', background='violet', bold=False)
colorprint('violet', background='violet', bold=True)
colorprint('violet', color=Fore.green, background='violet', bold=True)
```
![](images/index-0.png)
Use `Printer.available` to see other color printers
### [`Fore`](https://susuky.github.io/colortextpy/ansicolor.html#fore), [`Back`](https://susuky.github.io/colortextpy/ansicolor.html#back), [`Style`](https://susuky.github.io/colortextpy/ansicolor.html#style)
``` python
from colortextpy import Fore, Back, Style, AnsiColor, RESET_ALL
Fore, Back, Style
```
(<AnsiColor: 'FORE'>, <AnsiColor: 'BACK'>, <AnsiColor: 'STYLE'>)
You could use `Style` to get style ansi escape code:
``` python
print(f'{Style["bold"]+Style.underline}bold + underline{Style.end}')
Style['bold'], Style.underline
```
![](images/index-1.png)
Other style see `Style.availble`, but **bold**, **underline** would be
the most used
You could use `Fore` and `Back` to get the text foreground and
background ansi escape code:
``` python
Fore['red'], Fore.black, Back.chocolate, Back['hotpink']
```
('\x1b[38;2;255;0;0m',
'\x1b[38;2;0;0;0m',
'\x1b[48;2;210;105;30m',
'\x1b[48;2;255;105;180m')
Both `Fore` and `Back` could also support **8-bits**, **hex**, **rgb**
color.
``` python
Fore[50], Fore['#ffffff'], Fore['123, 45, 67']
```
('\x1b[38;5;50m', '\x1b[38;2;255;255;255m', '\x1b[38;2;123;45;67m')
``` python
Back['144'], Back['#123456'], Back['(55, 244, 31)']
```
('\x1b[48;5;144m', '\x1b[48;2;18;52;86m', '\x1b[48;2;55;244;31m')
Here’s demo of supported 8-bits colors:
<details>
<summary>Code</summary>
``` python
for i in range(256):
end = '\n' if (i+1)%8 == 0 else ' '*2
print(f'{i:3}: {Back[i]} {Back.reset}', end=end)
```
</details>
![](images/index-3.png)
Other available colors are in `Fore.availble` and `Back.available`
You could combine `Fore`, `Back`, `Style` to colorize your output:
``` python
text = 'something123'
print(Fore[50] + text + Fore.reset)
print(Back['black'] + Fore.aliceblue + Style.underline + text)
```
![](images/index-2.png)
`colortextpy` also provides
[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)
api to combine `Fore`, `Back` and `Style` together.
------------------------------------------------------------------------
### [AnsiColor](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)
> AnsiColor (fore:str=None, back:str=None, style:str=None)
Integrate with `Fore`, `Back`, `Style`.
| | **Type** | **Default** | **Details** |
|-------|----------|-------------|---------------------------------------------------------------------------|
| fore | str | None | Foreground color. Could be hex, rgb string or tuple, `Fore`, 8-bits color |
| back | str | None | Background color, Could be hex, rgb string or tuple, `Back`, 8-bits color |
| style | str | None | Text style. Seee `Style.available`. |
``` python
ansi = AnsiColor(fore='#0c0caa', back='aliceblue', style='underline')
print(ansi.ansi_fmt + '123456789' + RESET_ALL)
ansi = AnsiColor(fore=Fore['123, 234, 56'], back=(20, 29, 12), style=('bold', 'underline'))
print(ansi.ansi_fmt + '123' + RESET_ALL)
ansi.fore, ansi.back, ansi.style, ansi.ansi_fmt
```
![](images/index-4.png)
Without setting any color,
[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)
would give emtpy string:
``` python
AnsiColor().ansi_fmt
```
''
### [`ColorStream`](https://susuky.github.io/colortextpy/colorizer.html#colorstream)
Enables context managers to work as decorators to colorize the
`sys.stdout` or `sys.stderr`
Some usage:
``` python
with ColorStream(fore='red'):
print('text')
@ColorStream(fore=Fore.dark_orange)
def foo():
print('FOO')
```
``` python
with ColorStream(fore=Fore.dark_violet, autoreset=False):
print('autoreset off, affect next text')
with ColorStream(back=Back.light_green, style=(Style.underline, Style.bold)):
print('add background, underline, bold and autoreset')
with ColorStream(fore='red'):
print('Due to autoreset above, It only have red color')
print('Already leave context, show default color')
```
![](images/index-5.png)
``` python
@ColorStream(fore=Fore.dark_cyan)
def foo():
print('dark_cyan')
print('colortextpy')
foo()
```
![](images/index-6.png)
### [`colorize`](https://susuky.github.io/colortextpy/colorizer.html#ansicolorizer)
you can add color tag. Start with \<tag\> end with \</tag\>.
Some usage:
``` python
text = 'something'
text_w_tag = f'{text}-<fg red><bg #f0ffff>{text}</fg></bg>-{text}'
print(colorize(text_w_tag))
```
![](images/index-7.png)
And some other \<tag\> complex uasge:
<details>
<summary>Code</summary>
``` python
test_strings = ('one', 'two', 'three', 'four', 'five')
test_templates = [
'{0}',
'<blue>{0}</fg>',
'<red>{0}</red>--<bg green>{1}</bg green>',
'{0}--<red>{1}</red>--<fg red><bg green>{2}</bg>--{3}</fg>',
'{0}--<50>{1}</fg>--<fg 155><bg 78>{2}</bg></fg>',
'<bold>{0}--<fg 180, 46, 78>{1}</fg></bold>--<bg 152, 167, 52>{2}</bg>',
'<underline>{0}--<180, 46, 78>{1}</fg>--<bold>{1}--<bg 152, 167, 52>{2}</underline>--{3}</bold>--{4}</bg>',
'<bg #59FFAE>{0}--<#AAAA00>{1}--</bg>{2}</fg>--{3}',
]
for template in test_templates:
print(colorize(template.format(*test_strings)))
```
</details>
![](images/index-8.png)
`colorize` also integrates with
[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1):
``` python
print(colorize('something1', fore=5, back='#ffeeaa', style='bold'))
print(colorize('something2', fore='r', back='y', style='underline'))
```
![](images/index-9.png)
### [`Color`](https://susuky.github.io/colortextpy/color.html#color)
Some Constant of color with **hex**, **rgb**, **bgr** format
``` python
Color.red.name, Color.red.hex, Color.red.rgb, Color['red']
```
('red', '#ff0000', (255, 0, 0), <Color.red>)
You could also pass the hex constant from
[`Color`](https://susuky.github.io/colortextpy/color.html#color) into
`matplotlib.pyplot`:
``` python
plt.plot(np.sin(np.linspace(-4, 4, 50)), color=Color.red.hex)
```
![](index_files/figure-commonmark/cell-1-output-1.png)
Here are other colors in `Color.available` :
![](images/index-10.png)
Raw data
{
"_id": null,
"home_page": "https://github.com/susuky/colortextpy",
"name": "colortextpy",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "console color terminal text formatting notebook python",
"author": "ping",
"author_email": "hpisj322@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/80/1b/7816f7fc6557cd4d55d580c62b124bff67f829603194ad37663b55d7aa86/colortextpy-0.0.3.2.tar.gz",
"platform": null,
"description": "# colortextpy\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\nColortextpy is a Python package for adding colors and styles to terminal output, allowing you to create more visually appealing and organized command-line applications. The full `colortextpy` documentation is available at [https://susuky.github.io/colortextpy/](https://susuky.github.io/colortextpy/)\n\n\n## Install\n\nTested on python 3.6-3.11, win11, win11 WSL2, Ubuntu\n\nIt doesn\u2019t support win32\n\n``` sh\npip install colortextpy\n```\n\n## Usage\n\n### [Color printer](https://susuky.github.io/colortextpy/printer.html)\n\n``` python\nfrom colortextpy import Printer, colorprint\n\nPrinter.blue_print('blue')\nPrinter.blue_print('blue', bold=True)\n\ncolorprint('default')\ncolorprint('#ff3567', color='#ff3567')\ncolorprint('#123456', color=Fore['#123456'])\ncolorprint(4, color=4)\ncolorprint(137, color=Fore['137'])\ncolorprint('(50, 234, 33)', color=(50, 234, 33))\ncolorprint('(50, 24, 133)', color='(50, 24, 133)')\ncolorprint('dark_green', color=Fore.dark_green)\n\ncolorprint('violet', background='violet', bold=False)\ncolorprint('violet', background='violet', bold=True)\ncolorprint('violet', color=Fore.green, background='violet', bold=True)\n```\n\n![](images/index-0.png)\n\nUse `Printer.available` to see other color printers\n\n### [`Fore`](https://susuky.github.io/colortextpy/ansicolor.html#fore), [`Back`](https://susuky.github.io/colortextpy/ansicolor.html#back), [`Style`](https://susuky.github.io/colortextpy/ansicolor.html#style)\n\n``` python\nfrom colortextpy import Fore, Back, Style, AnsiColor, RESET_ALL\n\nFore, Back, Style\n```\n\n (<AnsiColor: 'FORE'>, <AnsiColor: 'BACK'>, <AnsiColor: 'STYLE'>)\n\nYou could use `Style` to get style ansi escape code:\n\n``` python\nprint(f'{Style[\"bold\"]+Style.underline}bold + underline{Style.end}')\nStyle['bold'], Style.underline\n```\n\n![](images/index-1.png)\n\nOther style see `Style.availble`, but **bold**, **underline** would be\nthe most used\n\nYou could use `Fore` and `Back` to get the text foreground and\nbackground ansi escape code:\n\n``` python\nFore['red'], Fore.black, Back.chocolate, Back['hotpink']\n```\n\n ('\\x1b[38;2;255;0;0m',\n '\\x1b[38;2;0;0;0m',\n '\\x1b[48;2;210;105;30m',\n '\\x1b[48;2;255;105;180m')\n\nBoth `Fore` and `Back` could also support **8-bits**, **hex**, **rgb**\ncolor.\n\n``` python\nFore[50], Fore['#ffffff'], Fore['123, 45, 67']\n```\n\n ('\\x1b[38;5;50m', '\\x1b[38;2;255;255;255m', '\\x1b[38;2;123;45;67m')\n\n``` python\nBack['144'], Back['#123456'], Back['(55, 244, 31)']\n```\n\n ('\\x1b[48;5;144m', '\\x1b[48;2;18;52;86m', '\\x1b[48;2;55;244;31m')\n\nHere\u2019s demo of supported 8-bits colors:\n\n<details>\n<summary>Code</summary>\n\n``` python\nfor i in range(256):\n end = '\\n' if (i+1)%8 == 0 else ' '*2\n print(f'{i:3}: {Back[i]} {Back.reset}', end=end)\n```\n\n</details>\n\n![](images/index-3.png)\n\nOther available colors are in `Fore.availble` and `Back.available`\n\nYou could combine `Fore`, `Back`, `Style` to colorize your output:\n\n``` python\ntext = 'something123'\nprint(Fore[50] + text + Fore.reset)\nprint(Back['black'] + Fore.aliceblue + Style.underline + text)\n```\n\n![](images/index-2.png)\n\n`colortextpy` also provides\n[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)\napi to combine `Fore`, `Back` and `Style` together.\n\n------------------------------------------------------------------------\n\n### [AnsiColor](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)\n\n> AnsiColor (fore:str=None, back:str=None, style:str=None)\n\nIntegrate with `Fore`, `Back`, `Style`.\n\n| | **Type** | **Default** | **Details** |\n|-------|----------|-------------|---------------------------------------------------------------------------|\n| fore | str | None | Foreground color. Could be hex, rgb string or tuple, `Fore`, 8-bits color |\n| back | str | None | Background color, Could be hex, rgb string or tuple, `Back`, 8-bits color |\n| style | str | None | Text style. Seee `Style.available`. |\n\n``` python\nansi = AnsiColor(fore='#0c0caa', back='aliceblue', style='underline')\nprint(ansi.ansi_fmt + '123456789' + RESET_ALL)\n\n\nansi = AnsiColor(fore=Fore['123, 234, 56'], back=(20, 29, 12), style=('bold', 'underline'))\nprint(ansi.ansi_fmt + '123' + RESET_ALL)\nansi.fore, ansi.back, ansi.style, ansi.ansi_fmt\n```\n\n![](images/index-4.png)\n\nWithout setting any color,\n[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1)\nwould give emtpy string:\n\n``` python\nAnsiColor().ansi_fmt\n```\n\n ''\n\n### [`ColorStream`](https://susuky.github.io/colortextpy/colorizer.html#colorstream)\n\nEnables context managers to work as decorators to colorize the\n`sys.stdout` or `sys.stderr`\n\nSome usage:\n\n``` python\n with ColorStream(fore='red'):\n print('text') \n\n @ColorStream(fore=Fore.dark_orange)\n def foo():\n print('FOO')\n```\n\n``` python\nwith ColorStream(fore=Fore.dark_violet, autoreset=False):\n print('autoreset off, affect next text')\n with ColorStream(back=Back.light_green, style=(Style.underline, Style.bold)):\n print('add background, underline, bold and autoreset')\n with ColorStream(fore='red'):\n print('Due to autoreset above, It only have red color')\nprint('Already leave context, show default color')\n```\n\n![](images/index-5.png)\n\n``` python\n@ColorStream(fore=Fore.dark_cyan)\ndef foo():\n print('dark_cyan')\n print('colortextpy')\n\nfoo()\n```\n\n![](images/index-6.png)\n\n### [`colorize`](https://susuky.github.io/colortextpy/colorizer.html#ansicolorizer)\n\nyou can add color tag. Start with \\<tag\\> end with \\</tag\\>.\n\nSome usage:\n\n``` python\ntext = 'something'\ntext_w_tag = f'{text}-<fg red><bg #f0ffff>{text}</fg></bg>-{text}'\nprint(colorize(text_w_tag))\n```\n\n![](images/index-7.png)\n\nAnd some other \\<tag\\> complex uasge:\n\n<details>\n<summary>Code</summary>\n\n``` python\ntest_strings = ('one', 'two', 'three', 'four', 'five')\ntest_templates = [\n '{0}',\n '<blue>{0}</fg>',\n '<red>{0}</red>--<bg green>{1}</bg green>',\n '{0}--<red>{1}</red>--<fg red><bg green>{2}</bg>--{3}</fg>',\n '{0}--<50>{1}</fg>--<fg 155><bg 78>{2}</bg></fg>',\n '<bold>{0}--<fg 180, 46, 78>{1}</fg></bold>--<bg 152, 167, 52>{2}</bg>',\n '<underline>{0}--<180, 46, 78>{1}</fg>--<bold>{1}--<bg 152, 167, 52>{2}</underline>--{3}</bold>--{4}</bg>',\n '<bg #59FFAE>{0}--<#AAAA00>{1}--</bg>{2}</fg>--{3}',\n]\n\nfor template in test_templates:\n print(colorize(template.format(*test_strings)))\n```\n\n</details>\n\n![](images/index-8.png)\n\n`colorize` also integrates with\n[`AnsiColor`](https://susuky.github.io/colortextpy/ansicolor.html#ansicolor-1):\n\n``` python\nprint(colorize('something1', fore=5, back='#ffeeaa', style='bold'))\nprint(colorize('something2', fore='r', back='y', style='underline'))\n```\n\n![](images/index-9.png)\n\n### [`Color`](https://susuky.github.io/colortextpy/color.html#color)\n\nSome Constant of color with **hex**, **rgb**, **bgr** format\n\n``` python\nColor.red.name, Color.red.hex, Color.red.rgb, Color['red']\n```\n\n ('red', '#ff0000', (255, 0, 0), <Color.red>)\n\nYou could also pass the hex constant from\n[`Color`](https://susuky.github.io/colortextpy/color.html#color) into\n`matplotlib.pyplot`:\n\n``` python\nplt.plot(np.sin(np.linspace(-4, 4, 50)), color=Color.red.hex)\n```\n\n![](index_files/figure-commonmark/cell-1-output-1.png)\n\nHere are other colors in `Color.available` :\n\n![](images/index-10.png)\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "Colortextpy is a Python package for adding colors and styles to terminal output, allowing you to create more visually appealing and organized command-line applications.",
"version": "0.0.3.2",
"project_urls": {
"Homepage": "https://github.com/susuky/colortextpy"
},
"split_keywords": [
"console",
"color",
"terminal",
"text",
"formatting",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f672cd1c4c07e4872f91ecb6d238c27079a74a4dccc37a2aa8801eb07e1ca070",
"md5": "2550fc8fcec2f17458382eddd4b3c024",
"sha256": "b3b695e47518a7981c615ea3fd65b5fda0616ccf11541b567e013f95e38ea472"
},
"downloads": -1,
"filename": "colortextpy-0.0.3.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2550fc8fcec2f17458382eddd4b3c024",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 16041,
"upload_time": "2023-05-20T14:42:22",
"upload_time_iso_8601": "2023-05-20T14:42:22.760200Z",
"url": "https://files.pythonhosted.org/packages/f6/72/cd1c4c07e4872f91ecb6d238c27079a74a4dccc37a2aa8801eb07e1ca070/colortextpy-0.0.3.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "801b7816f7fc6557cd4d55d580c62b124bff67f829603194ad37663b55d7aa86",
"md5": "31af184ddcac185ddd147bccd1338cb0",
"sha256": "ad4672b43e8a48bcf7e8dd93a862395b03d877b3797e2670d83d579f4ca28bc4"
},
"downloads": -1,
"filename": "colortextpy-0.0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "31af184ddcac185ddd147bccd1338cb0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 17818,
"upload_time": "2023-05-20T14:42:25",
"upload_time_iso_8601": "2023-05-20T14:42:25.347986Z",
"url": "https://files.pythonhosted.org/packages/80/1b/7816f7fc6557cd4d55d580c62b124bff67f829603194ad37663b55d7aa86/colortextpy-0.0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-20 14:42:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "susuky",
"github_project": "colortextpy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "colortextpy"
}