Skip to main content

A Python package designed to enhance code readability and CLI experience.

Project description

ansiplus

Pepy Total Downlods PyPI - Python Version GitHub repo size PyPI - License

A Python package designed to enhance code readability and CLI experience.

ansiplus-preview

Usage

[!WARNING] Not all terminals work perfectly with ANSI codes. Ideally you should use xterm-256color.

[!NOTE] Terminal themes can change colors, e.g. magenta might show up as yellow, but it is still using the magenta ANSI code.

Prerequisites

  • Python version >=3.10
  • A terminal emulator that accepts ANSI codes
  • A terminal emulator that uses xterm-256color

[!NOTE] ANSI codes aren't just limited to xterm-256color, but they may still be limited to all features this package includes.

Tested Terminals on Linux

Terminal ($TERM)

  • Konsole (xterm-256color) - no missing features
  • Xterm (xterm) - no missing features
  • Alacritty (alacritty) - blinking text does not work
  • Kitty (xterm-kitty) - blinking text does not work

Colors

Printing colored text can be used from a single function:

>>> from ansiplus import print_color
>>> print_color(text="hello world", color="red")
hello world
>>>

Optionally, you can also use the bgcolor parameter to change the background color.

Manually putting the color ANSI codes could be used at well:

>>> from ansiplus.ansi.colors import Fore, Back
>>> Fore.RED
'\x1b[31m'
>>> Back.RED
'\x1b[41m'
>>> print(f"{Fore.RED}hello world{Fore.RESET}") #output will appear in red
hello world
>>>

RGB colors are also an option:

>>> from ansiplus import print_rgb
>>> print_rgb(text="custom rgb text", rgb=(200, 108, 0))
custom rgb text
>>>

User Input

User input during a prompt can be colored:

>>> from ansiplus import input_color
>>> foo = input_color(text="name: ", color="blue") #users input will appear in blue
name:
>>>

Classes can be used for input, which will allow the ability to have user input history:

>>> from ansiplus import NewPrompt
>>> myprompt = NewPrompt()
>>> myprompt.set_color("blue")
>>> myprompt.prompt("say something: ")
say something: this text is blue
'this text is blue'
>>> myprompt.prompt("say something: ", 'red')
say something: this text is red, default is blue
'this text is red, default is blue'
>>> myprompt.latest
'this text is red, default is blue'
>>> myprompt.history
['this text is blue', 'this text is red, default is blue']
>>>

Text Styles

Text can be stylized and printed:

>>> from ansiplus import print_style
>>> print_style("bold text", 'bold')
bold text
>>>

Like colors, this can also be done manually:

>>> from ansiplus.ansi.styles import BOLD, BOLD_RESET
>>> BOLD
'\x1b[1m'
>>> BOLD_RESET
'\x1b[22m'
>>> print(f"{BOLD}bold text{BOLD_RESET}")
bold text
>>>

More Functions

There are more ANSI code features beyond colors and styles, these could be viewed by using help(ansiplus) inside the python interpreter.

Developers

Wheels

When building the package for testing, it is recommended to use python3 -m build.

PIP Virtual Environments

Virtual environments should be named ".venv" or ".env", as this is used in the ".gitignore" file.

Contributing

Contributions must not include:

  • breaking code
  • major changes
  • changes to the version number
  • wheel files or egg-info files
  • spaghetti code

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansiplus-1.1.0.tar.gz (10.3 kB view hashes)

Uploaded Source

Built Distribution

ansiplus-1.1.0-py3-none-any.whl (11.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page