constyle


Nameconstyle JSON
Version 2.0.4 PyPI version JSON
download
home_pagehttps://github.com/abrahammurciano/python-constyle
SummaryA Python library to add style to your console.
upload_time2024-05-21 13:48:38
maintainerNone
docs_urlNone
authorAbraham Murciano
requires_python<4.0,>=3.7
licenseGPLv3
keywords terminal console style color colors colour colours ansi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # constyle
A Python library to add style to your console.

The name of the library comes from merging the words **CONSoLE** and **STYLE**. Also "con" means "with" in Spanish.

## Installation

You can install this package with pip or conda.
```sh
$ pip install constyle
```
```sh
$ conda install -c conda-forge constyle
```
```sh
$ conda install -c abrahammurciano constyle
```

## Links

[![Documentation](https://img.shields.io/badge/Documentation-C61C3E?style=for-the-badge&logo=Read+the+Docs&logoColor=%23FFFFFF)](https://abrahammurciano.github.io/python-constyle/constyle)

[![Source Code - GitHub](https://img.shields.io/badge/Source_Code-GitHub-181717?style=for-the-badge&logo=GitHub&logoColor=%23FFFFFF)](https://github.com/abrahammurciano/python-constyle.git)

[![PyPI - constyle](https://img.shields.io/badge/PyPI-constyle-006DAD?style=for-the-badge&logo=PyPI&logoColor=%23FFD242)](https://pypi.org/project/constyle/)

[![Anaconda - constyle](https://img.shields.io/badge/Anaconda-constyle-44A833?style=for-the-badge&logo=Anaconda&logoColor=%23FFFFFF)](https://anaconda.org/abrahammurciano/constyle)

[![Discord - Community](https://img.shields.io/badge/Discord-Community-5865F2?style=for-the-badge&logo=Discord&logoColor=FFFFFF)](https://discord.gg/nUmsrhNDSs)

## Usage

There are a couple of ways to use this library.

### The `style` function

The simplest way is with the `style` function.

```py
from constyle import style, Attributes

print(style('Hello World', Attributes.GREEN, Attributes.BOLD, Attributes.ON_BLUE))
```

### `Style` objects

You can also use `Style` objects to create a reusable style with any number of attributes.

#### Calling a `Style` object

`Style` objects are callable and take a string as input (or any other object, which will be converted to a string) and return a styled string.

```py
warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(warning('You shall not pass!'))
```

#### Adding `Style` objects

Adding together `Style` objects will also create `Style` objects.

```py
whisper = Attributes.GREY + Attributes.DIM + Attributes.SUPERSCRIPT
print(whisper('Fly you fools'))
```

#### Converting `Style` objects to strings

`Style` objects can be converted to strings to obtain the ANSI escape sequence for that style.

```py
warning = Style(Attributes.YELLOW, Attributes.BOLD)
print(f"{warning}You shall not pass!{Attributes.RESET}")
```

### Attributes

The `Attributes` enum contains all the available ANSI attributes. You can read more about them [here](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).

`Attributes` are also `Style` objects, and as such, as demonstrated above, they too can be called to style a string, added together and to other `Style` objects, and converted to strings to obtain their ANSI sequence.

You'll find there is limited support for all the ANSI attributes among some consoles.

If you find more attributes that aren't provided in this enum, you can create your own by constructing a `Style` with an integer.

### Nesting

In order to nest styles, you can use the `end=` keyword argument of the `style` function or the `Style` class. Usually when applying a style, the `RESET` attribute is appended to the end. This can be undesirable when nesting (see the example below).

```py
bold = Attributes.BOLD
yellow = Attributes.YELLOW
green = Attributes.GREEN

print(yellow(bold('This is bold and yellow')))
print(green(f"This is green. {yellow('This is yellow.')} This is no longer green"))
```

In order to achieve the desired result in the above example, you would have to use the `end=` keyword argument of the `style` function. You can pass any `Style` to `end`.

```py
print(green(f"This is green. {bold('This is green and bold.', end=Attributes.NO_BOLD)} This is still green but not bold anymore"))
print(green(f"This is green. {yellow('This is yellow.', end=green)} This is now green again"))
```

### Custom colours

The `constyle.custom_colours` module contains a few classes that can be used to create custom colours.

#### RGB colours

You can create a `Style` for a custom RGB colour by using the `RGB` class. This is not well supported by all consoles.

```py
from constyle.custom_colours import RGB

print(style('This is pink', RGB(255, 192, 203)))
```

#### 8-bit colours

Some consoles support 8-bit colours. You can create a `Style` for an 8-bit colour by using the `EightBit` class, passing a single integer to it, or you can use the `EightBitRGB` class to create an 8-bit colour style as close to the RGB values as possible.

## The command line interface

This package also provides a very basic command line interface to print styled strings.

You can pass it any number of strings and it will print them all together (like `echo`). You can pass `--attribute` (or `-a`) with the name of an attribute to apply to the other strings being printed. You can pass `--attribute` as many times as you like.

You can use `constyle --help` to see more specific details, as well as all available attributes.

For example you can use `constyle` from your shell to print some styled text.

```sh
$ constyle Hello World! -a green -a bold -a on_white
```

Or if you're writing a shell script you can make an alias or a function to reuse a certain style.

```sh
#!/bin/bash
alias error="constyle --attribute bold --attribute red" # With an alias
warn() { constyle $@ -a bold -a yellow } # With a function
error You shall not pass!
warn Fly you fools!
```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/abrahammurciano/python-constyle",
    "name": "constyle",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.7",
    "maintainer_email": null,
    "keywords": "terminal, console, style, color, colors, colour, colours, ansi",
    "author": "Abraham Murciano",
    "author_email": "abrahammurciano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a3/1f/324d3827139910b69555d2776d0bd20aaefc68993907d6e63f52663ece5c/constyle-2.0.4.tar.gz",
    "platform": null,
    "description": "# constyle\nA Python library to add style to your console.\n\nThe name of the library comes from merging the words **CONSoLE** and **STYLE**. Also \"con\" means \"with\" in Spanish.\n\n## Installation\n\nYou can install this package with pip or conda.\n```sh\n$ pip install constyle\n```\n```sh\n$ conda install -c conda-forge constyle\n```\n```sh\n$ conda install -c abrahammurciano constyle\n```\n\n## Links\n\n[![Documentation](https://img.shields.io/badge/Documentation-C61C3E?style=for-the-badge&logo=Read+the+Docs&logoColor=%23FFFFFF)](https://abrahammurciano.github.io/python-constyle/constyle)\n\n[![Source Code - GitHub](https://img.shields.io/badge/Source_Code-GitHub-181717?style=for-the-badge&logo=GitHub&logoColor=%23FFFFFF)](https://github.com/abrahammurciano/python-constyle.git)\n\n[![PyPI - constyle](https://img.shields.io/badge/PyPI-constyle-006DAD?style=for-the-badge&logo=PyPI&logoColor=%23FFD242)](https://pypi.org/project/constyle/)\n\n[![Anaconda - constyle](https://img.shields.io/badge/Anaconda-constyle-44A833?style=for-the-badge&logo=Anaconda&logoColor=%23FFFFFF)](https://anaconda.org/abrahammurciano/constyle)\n\n[![Discord - Community](https://img.shields.io/badge/Discord-Community-5865F2?style=for-the-badge&logo=Discord&logoColor=FFFFFF)](https://discord.gg/nUmsrhNDSs)\n\n## Usage\n\nThere are a couple of ways to use this library.\n\n### The `style` function\n\nThe simplest way is with the `style` function.\n\n```py\nfrom constyle import style, Attributes\n\nprint(style('Hello World', Attributes.GREEN, Attributes.BOLD, Attributes.ON_BLUE))\n```\n\n### `Style` objects\n\nYou can also use `Style` objects to create a reusable style with any number of attributes.\n\n#### Calling a `Style` object\n\n`Style` objects are callable and take a string as input (or any other object, which will be converted to a string) and return a styled string.\n\n```py\nwarning = Style(Attributes.YELLOW, Attributes.BOLD)\nprint(warning('You shall not pass!'))\n```\n\n#### Adding `Style` objects\n\nAdding together `Style` objects will also create `Style` objects.\n\n```py\nwhisper = Attributes.GREY + Attributes.DIM + Attributes.SUPERSCRIPT\nprint(whisper('Fly you fools'))\n```\n\n#### Converting `Style` objects to strings\n\n`Style` objects can be converted to strings to obtain the ANSI escape sequence for that style.\n\n```py\nwarning = Style(Attributes.YELLOW, Attributes.BOLD)\nprint(f\"{warning}You shall not pass!{Attributes.RESET}\")\n```\n\n### Attributes\n\nThe `Attributes` enum contains all the available ANSI attributes. You can read more about them [here](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).\n\n`Attributes` are also `Style` objects, and as such, as demonstrated above, they too can be called to style a string, added together and to other `Style` objects, and converted to strings to obtain their ANSI sequence.\n\nYou'll find there is limited support for all the ANSI attributes among some consoles.\n\nIf you find more attributes that aren't provided in this enum, you can create your own by constructing a `Style` with an integer.\n\n### Nesting\n\nIn order to nest styles, you can use the `end=` keyword argument of the `style` function or the `Style` class. Usually when applying a style, the `RESET` attribute is appended to the end. This can be undesirable when nesting (see the example below).\n\n```py\nbold = Attributes.BOLD\nyellow = Attributes.YELLOW\ngreen = Attributes.GREEN\n\nprint(yellow(bold('This is bold and yellow')))\nprint(green(f\"This is green. {yellow('This is yellow.')} This is no longer green\"))\n```\n\nIn order to achieve the desired result in the above example, you would have to use the `end=` keyword argument of the `style` function. You can pass any `Style` to `end`.\n\n```py\nprint(green(f\"This is green. {bold('This is green and bold.', end=Attributes.NO_BOLD)} This is still green but not bold anymore\"))\nprint(green(f\"This is green. {yellow('This is yellow.', end=green)} This is now green again\"))\n```\n\n### Custom colours\n\nThe `constyle.custom_colours` module contains a few classes that can be used to create custom colours.\n\n#### RGB colours\n\nYou can create a `Style` for a custom RGB colour by using the `RGB` class. This is not well supported by all consoles.\n\n```py\nfrom constyle.custom_colours import RGB\n\nprint(style('This is pink', RGB(255, 192, 203)))\n```\n\n#### 8-bit colours\n\nSome consoles support 8-bit colours. You can create a `Style` for an 8-bit colour by using the `EightBit` class, passing a single integer to it, or you can use the `EightBitRGB` class to create an 8-bit colour style as close to the RGB values as possible.\n\n## The command line interface\n\nThis package also provides a very basic command line interface to print styled strings.\n\nYou can pass it any number of strings and it will print them all together (like `echo`). You can pass `--attribute` (or `-a`) with the name of an attribute to apply to the other strings being printed. You can pass `--attribute` as many times as you like.\n\nYou can use `constyle --help` to see more specific details, as well as all available attributes.\n\nFor example you can use `constyle` from your shell to print some styled text.\n\n```sh\n$ constyle Hello World! -a green -a bold -a on_white\n```\n\nOr if you're writing a shell script you can make an alias or a function to reuse a certain style.\n\n```sh\n#!/bin/bash\nalias error=\"constyle --attribute bold --attribute red\" # With an alias\nwarn() { constyle $@ -a bold -a yellow } # With a function\nerror You shall not pass!\nwarn Fly you fools!\n```",
    "bugtrack_url": null,
    "license": "GPLv3",
    "summary": "A Python library to add style to your console.",
    "version": "2.0.4",
    "project_urls": {
        "Documentation": "https://abrahammurciano.github.io/python-constyle/constyle",
        "Homepage": "https://github.com/abrahammurciano/python-constyle",
        "Repository": "https://github.com/abrahammurciano/python-constyle"
    },
    "split_keywords": [
        "terminal",
        " console",
        " style",
        " color",
        " colors",
        " colour",
        " colours",
        " ansi"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "32ccdc95c56afeca82d72797f03f9907716b5b2386173d55a4e241b24719a3a5",
                "md5": "6a59b59fdabad7abc13190cafa9a4927",
                "sha256": "4bf4c25dbbcd5f493fcef93a66a652da23e291d31a9e46cff4ed31483eb6c783"
            },
            "downloads": -1,
            "filename": "constyle-2.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6a59b59fdabad7abc13190cafa9a4927",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 21555,
            "upload_time": "2024-05-21T13:48:37",
            "upload_time_iso_8601": "2024-05-21T13:48:37.098079Z",
            "url": "https://files.pythonhosted.org/packages/32/cc/dc95c56afeca82d72797f03f9907716b5b2386173d55a4e241b24719a3a5/constyle-2.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a31f324d3827139910b69555d2776d0bd20aaefc68993907d6e63f52663ece5c",
                "md5": "3caaf01f10afaacae8d4d6b375fe6729",
                "sha256": "482e0609e79cffd277f14962b1873f0f7ddaccd72bd17e2c653c31e898c0b451"
            },
            "downloads": -1,
            "filename": "constyle-2.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "3caaf01f10afaacae8d4d6b375fe6729",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 21598,
            "upload_time": "2024-05-21T13:48:38",
            "upload_time_iso_8601": "2024-05-21T13:48:38.217601Z",
            "url": "https://files.pythonhosted.org/packages/a3/1f/324d3827139910b69555d2776d0bd20aaefc68993907d6e63f52663ece5c/constyle-2.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-21 13:48:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "abrahammurciano",
    "github_project": "python-constyle",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "constyle"
}
        
Elapsed time: 0.24553s