Termicolor


NameTermicolor JSON
Version 1.0.3 PyPI version JSON
download
home_pageNone
SummarySimple, intuitive, chainable API for styling and coloring the terminal
upload_time2024-08-01 14:35:14
maintainerNone
docs_urlNone
authorNone
requires_python>=3.5
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Termicolor
==========

Termicolor is a package that offers an easy-to-use API for quickly applying colors to text. It is excellent for swiftly creating appropriate styles and allows for extensive reusability.

The only dependency is Colorama, to ensure that escape characters work with Windows.

Installation
------------

You can easily install Termicolor using pip

```
pip install Termicolor
```

Usage
-----

Once you have Termicolor installed, it is quite simple to use.

You can import `Color` from Termicolor:

```python
from Termicolor import Color
```

And start using it in your project. Here's a simple demo:

```python
styled_text = Color("Hello, World!").red.bold.underline
other_text = Color("Hello, World, Again.").bg_blue.bold.underline

print(styled_text, other_text)
```

Alternatively, you can import `ansi`, which is more hands-on with styling, but still removes the need to manually type in escape codes:

```python
from Termicolor import ansi

styled_text = f"{ansi('red', 'bold', 'underline')} Hello, World! {ansi('reset')}"
```

Even though Termicolor is very straightforward to get started with, it has many features which will be explained and demonstrated below.

### Supported Colors & Styles

Here is a list of what you can apply to your text using Termicolor:

| **Foreground**     | **Background**      | **Style**                
| ------------------ | ------------------- | ------------------------ 
| **black** `\033[30m`   | **bg_black** `\033[40m` | **reset** `\033[0m`
| **red** `\033[31m`     | **bg_red** `\033[41m`   | **bold** `\033[1m`
| **green** `\033[32m`   | **bg_green** `\033[42m` | **dim** `\033[2m`
| **yellow** `\033[33m`  | **bg_yellow** `\033[43m`| **underline** `\033[4m`
| **blue** `\033[34m`    | **bg_blue** `\033[44m`  | **blink** `\033[5m`
| **magenta** `\033[35m` | **bg_magenta** `\033[45m`| **rapid_blink** `\033[6m`
| **cyan** `\033[36m`    | **bg_cyan** `\033[46m`  | **inverse** `\033[7m`
|                    |                     | **hidden** `\033[8m`
|                    |                     | **strikethrough** `\033[9m`

### Reusing the same styles

Defining reusable styles with Termicolor is convenient and fast.

To do this, you can specify whatever styles you want as usual, omitting passing in a string as this will be used as a base. All you need to do is use the `freeze` method afterwords. This allows you to create new instances of `Color` with exactly the same specifications. Here's an example:

```python
warn = Color().yellow.underline.freeze()
danger = Color().red.bold.freeze()

print(warn("This is some serious text"))
print(danger("This is some even more serious text"))

# There's also a print method (it can take arguments):
warn("Using print method").print()
```

Of course, you can import these styles, allowing you to organize and define them in other files.

### Nesting & Adding new text

Termicolor offers support for nesting, as well as appending and prepending text to what you have already initialized.

There is a few ways you can nest text, but the simplest way to do it is to include it in the constructor of your dominant style:

```python
nested_text = Color("World!").blue
styled_text = Color("Hello,", nested_text).green.bold

print(styled_text)
```

Optionally, you can also nest using the `nest` method.

```python
nested_text = Color("World!").blue
styled_text = Color("Hello,").green.bold.nest(nested_text)
```

Either of these can take several arguments to nest several separate strings.

If you don't want to nest text, but would like to add it on, you can use either the `before` or `after` methods:

```python
after_demo = Color("Hello,").magenta.after("World!")
before_demo = Color("World!").magenta.before("Hello,")

print(after_demo, before_demo)
```

Both of these methods can also nest text, using the same approach.

As a side note, you can delay passing in the text until later, by calling the `Color` instance after your styling:

```python
styled_text = Color().green.bold("Hello, World!")

print(styled_text)
```

Other things to note:
* To add text right after any of these methods, pass in the keyword argument "after" when calling the instance.
* You will notice there's an automatic space added as a separator when nesting or adding additional text, you can override it by passing in false for the "sp" keyword argument to either the `after` or `before` method.

### Setting Seperators

To configure the separator space that is automatically placed between added text, the keyword argument "sp" can be set to whatever character or string you want. If no space is desired, set it to an empty string. Here's an example:

```python
styled_text = Color("Hello, ", "World", sp="")

print(styled_text) # Prints "Hello, World" instead of "Hello,  World" with two spaces
```

Additionally, you can remove or set a new separator by using these methods:
* `new_sp`, takes the new separator as an argument
* `remove_sp`, removes the separator

### Spacing & Padding

With Termicolor, you can set spacing which is automatically added before and after but is a part of the styled string, or padding which adds spacing without it being styled. This is a simple demo:

```python
styled_text = Color("Hello, World!").red.padding(4).space(4)

print(styled_text)
```

If you want to only add padding or spacing after/left or before/right using the `padding_l` or `padding_r` method, and `space_b` or `space_a` method.

Note: padding or spacing **does** carry over when using the `freeze` method to reuse a style.

### Clear styling

To clear whatever attributes have been set, you can use the `reset` method to clear everything, or one of these other methods to clear a specific attribute:

* `clear_fore`, `clear_back`, or `clear_style` to clear foreground, background, or text formatting
* `clear_pad`, or `clear_space` to clear padding or spacing
* `clear_text` to clear any text that is set
* `remove_sp` to remove the separator

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "Termicolor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "border-l <border-l@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/06/00/17618360419f498a87aca0f645be03045f17014bc7eb116fef70f34f1b1a/termicolor-1.0.3.tar.gz",
    "platform": null,
    "description": "Termicolor\r\n==========\r\n\r\nTermicolor is a package that offers an easy-to-use API for quickly applying colors to text. It is excellent for swiftly creating appropriate styles and allows for extensive reusability.\r\n\r\nThe only dependency is Colorama, to ensure that escape characters work with Windows.\r\n\r\nInstallation\r\n------------\r\n\r\nYou can easily install Termicolor using pip\r\n\r\n```\r\npip install Termicolor\r\n```\r\n\r\nUsage\r\n-----\r\n\r\nOnce you have Termicolor installed, it is quite simple to use.\r\n\r\nYou can import `Color` from Termicolor:\r\n\r\n```python\r\nfrom Termicolor import Color\r\n```\r\n\r\nAnd start using it in your project. Here's a simple demo:\r\n\r\n```python\r\nstyled_text = Color(\"Hello, World!\").red.bold.underline\r\nother_text = Color(\"Hello, World, Again.\").bg_blue.bold.underline\r\n\r\nprint(styled_text, other_text)\r\n```\r\n\r\nAlternatively, you can import `ansi`, which is more hands-on with styling, but still removes the need to manually type in escape codes:\r\n\r\n```python\r\nfrom Termicolor import ansi\r\n\r\nstyled_text = f\"{ansi('red', 'bold', 'underline')} Hello, World! {ansi('reset')}\"\r\n```\r\n\r\nEven though Termicolor is very straightforward to get started with, it has many features which will be explained and demonstrated below.\r\n\r\n### Supported Colors & Styles\r\n\r\nHere is a list of what you can apply to your text using Termicolor:\r\n\r\n| **Foreground**     | **Background**      | **Style**                \r\n| ------------------ | ------------------- | ------------------------ \r\n| **black** `\\033[30m`   | **bg_black** `\\033[40m` | **reset** `\\033[0m`\r\n| **red** `\\033[31m`     | **bg_red** `\\033[41m`   | **bold** `\\033[1m`\r\n| **green** `\\033[32m`   | **bg_green** `\\033[42m` | **dim** `\\033[2m`\r\n| **yellow** `\\033[33m`  | **bg_yellow** `\\033[43m`| **underline** `\\033[4m`\r\n| **blue** `\\033[34m`    | **bg_blue** `\\033[44m`  | **blink** `\\033[5m`\r\n| **magenta** `\\033[35m` | **bg_magenta** `\\033[45m`| **rapid_blink** `\\033[6m`\r\n| **cyan** `\\033[36m`    | **bg_cyan** `\\033[46m`  | **inverse** `\\033[7m`\r\n|                    |                     | **hidden** `\\033[8m`\r\n|                    |                     | **strikethrough** `\\033[9m`\r\n\r\n### Reusing the same styles\r\n\r\nDefining reusable styles with Termicolor is convenient and fast.\r\n\r\nTo do this, you can specify whatever styles you want as usual, omitting passing in a string as this will be used as a base. All you need to do is use the `freeze` method afterwords. This allows you to create new instances of `Color` with exactly the same specifications. Here's an example:\r\n\r\n```python\r\nwarn = Color().yellow.underline.freeze()\r\ndanger = Color().red.bold.freeze()\r\n\r\nprint(warn(\"This is some serious text\"))\r\nprint(danger(\"This is some even more serious text\"))\r\n\r\n# There's also a print method (it can take arguments):\r\nwarn(\"Using print method\").print()\r\n```\r\n\r\nOf course, you can import these styles, allowing you to organize and define them in other files.\r\n\r\n### Nesting & Adding new text\r\n\r\nTermicolor offers support for nesting, as well as appending and prepending text to what you have already initialized.\r\n\r\nThere is a few ways you can nest text, but the simplest way to do it is to include it in the constructor of your dominant style:\r\n\r\n```python\r\nnested_text = Color(\"World!\").blue\r\nstyled_text = Color(\"Hello,\", nested_text).green.bold\r\n\r\nprint(styled_text)\r\n```\r\n\r\nOptionally, you can also nest using the `nest` method.\r\n\r\n```python\r\nnested_text = Color(\"World!\").blue\r\nstyled_text = Color(\"Hello,\").green.bold.nest(nested_text)\r\n```\r\n\r\nEither of these can take several arguments to nest several separate strings.\r\n\r\nIf you don't want to nest text, but would like to add it on, you can use either the `before` or `after` methods:\r\n\r\n```python\r\nafter_demo = Color(\"Hello,\").magenta.after(\"World!\")\r\nbefore_demo = Color(\"World!\").magenta.before(\"Hello,\")\r\n\r\nprint(after_demo, before_demo)\r\n```\r\n\r\nBoth of these methods can also nest text, using the same approach.\r\n\r\nAs a side note, you can delay passing in the text until later, by calling the `Color` instance after your styling:\r\n\r\n```python\r\nstyled_text = Color().green.bold(\"Hello, World!\")\r\n\r\nprint(styled_text)\r\n```\r\n\r\nOther things to note:\r\n* To add text right after any of these methods, pass in the keyword argument \"after\" when calling the instance.\r\n* You will notice there's an automatic space added as a separator when nesting or adding additional text, you can override it by passing in false for the \"sp\" keyword argument to either the `after` or `before` method.\r\n\r\n### Setting Seperators\r\n\r\nTo configure the separator space that is automatically placed between added text, the keyword argument \"sp\" can be set to whatever character or string you want. If no space is desired, set it to an empty string. Here's an example:\r\n\r\n```python\r\nstyled_text = Color(\"Hello, \", \"World\", sp=\"\")\r\n\r\nprint(styled_text) # Prints \"Hello, World\" instead of \"Hello,  World\" with two spaces\r\n```\r\n\r\nAdditionally, you can remove or set a new separator by using these methods:\r\n* `new_sp`, takes the new separator as an argument\r\n* `remove_sp`, removes the separator\r\n\r\n### Spacing & Padding\r\n\r\nWith Termicolor, you can set spacing which is automatically added before and after but is a part of the styled string, or padding which adds spacing without it being styled. This is a simple demo:\r\n\r\n```python\r\nstyled_text = Color(\"Hello, World!\").red.padding(4).space(4)\r\n\r\nprint(styled_text)\r\n```\r\n\r\nIf you want to only add padding or spacing after/left or before/right using the `padding_l` or `padding_r` method, and `space_b` or `space_a` method.\r\n\r\nNote: padding or spacing **does** carry over when using the `freeze` method to reuse a style.\r\n\r\n### Clear styling\r\n\r\nTo clear whatever attributes have been set, you can use the `reset` method to clear everything, or one of these other methods to clear a specific attribute:\r\n\r\n* `clear_fore`, `clear_back`, or `clear_style` to clear foreground, background, or text formatting\r\n* `clear_pad`, or `clear_space` to clear padding or spacing\r\n* `clear_text` to clear any text that is set\r\n* `remove_sp` to remove the separator\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Simple, intuitive, chainable API for styling and coloring the terminal",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/border-l/Termicolor"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d82056554cb8448c5700b4940bb33c978704fb255e444f2e6f6e295222816ff4",
                "md5": "16713cbc80027f3035acaa807630e1a0",
                "sha256": "37679a789242ff66ff1834c7e44443c34da41f280a8c052cee1c5cbbdcf06a7c"
            },
            "downloads": -1,
            "filename": "Termicolor-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "16713cbc80027f3035acaa807630e1a0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 8565,
            "upload_time": "2024-08-01T14:35:13",
            "upload_time_iso_8601": "2024-08-01T14:35:13.727155Z",
            "url": "https://files.pythonhosted.org/packages/d8/20/56554cb8448c5700b4940bb33c978704fb255e444f2e6f6e295222816ff4/Termicolor-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "060017618360419f498a87aca0f645be03045f17014bc7eb116fef70f34f1b1a",
                "md5": "98839e5fa179156fd9027cd5abd64555",
                "sha256": "293a434073941b2cb5f1432644f0218fe6523cf7108fa6e1f3f086144433d353"
            },
            "downloads": -1,
            "filename": "termicolor-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "98839e5fa179156fd9027cd5abd64555",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 9817,
            "upload_time": "2024-08-01T14:35:14",
            "upload_time_iso_8601": "2024-08-01T14:35:14.709096Z",
            "url": "https://files.pythonhosted.org/packages/06/00/17618360419f498a87aca0f645be03045f17014bc7eb116fef70f34f1b1a/termicolor-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-01 14:35:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "border-l",
    "github_project": "Termicolor",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "termicolor"
}
        
Elapsed time: 0.50035s