pixy

Add colour and style to terminal text.

https://github.com/kiancross/pixy

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.2%) to scientific vocabulary

Keywords

ansii-escape color colour python terminal

Keywords from Contributors

interactive serializer packaging network-simulation shellcodes hacking autograding observability genomics embedded
Last synced: 6 months ago · JSON representation

Repository

Add colour and style to terminal text.

Basic Info
  • Host: GitHub
  • Owner: kiancross
  • License: mit
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 181 KB
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 5
  • Releases: 6
Topics
ansii-escape color colour python terminal
Created over 5 years ago · Last pushed over 2 years ago
Metadata Files
Readme License

README.md

pixy

PyPI version License Continuous Integration CodeQL codecov

pixy is a Python library for adding colour and style to terminal text.

Terminal output can be styled using ANSII escape code. pixy provides a simple to use yet comprehensive wrapper, abstracting away the complexities.

pixy supports: - 3-bit, 4-bit and 8-bit colour; - 24-bit true colour RGB; - changing the foreground and background colour; - text decorators e.g. blink, underline, bold, italic, strike-through; - custom fonts for compliant terminals; - custom ANSII codes for development on non-spec-compliant terminals.

Getting Started

Installation

pixy can be installed using pip. $ python3 -m pip install pixy

Usage

pixy lets you style text straight out of the box.

```python import pixy

Print "Hello World" in red.

print(pixy.red("Hello World")) ```

It provides the flexibility to format text as you wish.

```python import pixy

Create a string "Hello" with bold text and blue background.

s1 = pixy.pring("Hello", pixy.decorators.bold + pixy.background.blue)

Concatenate the two strings, giving everything a red background (the

red background won't be applied to "Hello" because we've already

given it a blue background!)

print(pixy.pring(s1 + " World. Lorem ipsum dolor...", pixy.background.red)) ```

Documentation

Not all terminals support all ANSII escape codes. Please check the terminal you are testing on supports the features you are using before opening an issue.

Helpers

You can create a string of a certain colour using the helper functions below:

  • pixy.black(text)
  • pixy.red(text)
  • pixy.green(text)
  • pixy.yellow(text)
  • pixy.blue(text)
  • pixy.magenta(text)
  • pixy.cyan(text)
  • pixy.white(text)

Example

```python import pixy

Print "Hello World" in red.

print(pixy.red("Hello World")) ```

pixy.pring(text, style)

pixy.pring allows you to generate a string with any of the styles documented below. You can use more than one style by adding them together.

  • text - the text to apply the style to.
  • style - the style to apply to the text.

Example

```python import pixy

Print "Hello World" with green text and white background.

print(pixy.pring( "Hello World", pixy.foreground.green + pixy.background.white )) ```

pixy.foreground, pixy.background

pixy.foreground and pixy.background contain the variables: black, red, green, yellow, blue, magenta, cyan, white.

Using the pixy.foreground variant of the colour will apply the colour to the foreground and the pixy.background variant will apply it to the background.

These colours are known as the 3-bit colours and almost all terminals support them. Most terminals also let you make them bright by adding the pixy.decorators.bold style.

Example

```python import pixy

Print "Hello World" with green text.

print(pixy.pring("Hello World", pixy.foreground.green)) ```

pixy.decorators

Decorators let you apply other styles to the text.

| Decorator | Description | Support | |--|--|--| | pixy.decorators.bold | Bold/bright | | pixy.decorators.faint | Faint/dimmed | | pixy.decorators.italic | Italicised | | pixy.decorators.underline | Underlined | | pixy.decorators.slow_blink | Slow blink (less than 150 per minute) | | pixy.decorators.rapid_blink | Rapid blink (150+ per minute) | Not widely supported | | pixy.decorators.invert | Swap foreground and background colours | | pixy.decorators.conceal | Hide | Not widely supported | | pixy.decorators.strike | Strike-through | | pixy.decorators.fraktur | Fraktur | Rarely supported | | pixy.decorators.double_underline | Double underline | | pixy.decorators.framed | | Not widely supported | | pixy.decorators.encircled | | Not widely supported | | pixy.decorators.overlined | Overlined |

(Descriptions adapted from Wikipedia)

Example

```python import pixy

"Hello World" underlined.

print(pixy.pring("Hello World", pixy.decorators.underline)) ```

pixy.ExtendedColour(code, background=False)

With pixy.ExtendedColour you can select from a pre-defined selection of 256 colours.

  • code - corresponds to a colour code found here.

  • background - boolean value indicating if this is a background or foreground colour.

Example

```python import pixy

Print "Hello World" in violet.

print(pixy.pring("Hello World", pixy.ExtendedColour(99)))

Print "Hello World" with a violet background.

print(pixy.pring("Hello World", pixy.ExtendedColour(99, True))) ```

pixy.TrueColour(red, green, blue, background=False)

With pixy.TrueColour you can create an RGB colour.

  • red - value between 0 and 255 indicating the intensity of the red component.

  • green - value between 0 and 255 indicating the intensity of the green component.

  • blue - value between 0 and 255 indicating the intensity of the blue component.

  • background - boolean value indicating if this is a background or foreground colour.

Example

```python import pixy

Print "Hello World" in violet.

print(pixy.pring("Hello World", pixy.TrueColour(238, 130, 238)))

Print "Hello World" with a violet background.

print(pixy.pring("Hello World", pixy.TrueColour(238, 130, 238, True))) ```

pixy.Font(code)

Allows you to change the font used. Most terminals do not support this.

  • code - a number between 0 and 8 corresponding to a font.

Example

```python import pixy

print(pixy.pring("Hello World", pixy.Font(3))) ```

pixy.EscapeSequence(...)

pixy.EscapeSequence can be used in instances that you want to defined your own ANSII escape sequence.

pixy.EscapeSequence takes a variable number of arguments - each argument should be an ANSII code.

Example

```python import pixy

Bright blue using ANSII code 94 supported by some terminals

print(pixy.pring("Hello World", pixy.EscapeSequence(94))) ```

Examples

Blinking Text

```python import pixy

print(pixy.pring("Hello World", pixy.decorators.slow_blink)) ```

Concatenation

```python import pixy

"Hello" in red.

s1 = pixy.pring("Hello", pixy.foreground.red)

" World" in blue.

s2 = pixy.pring(" World", pixy.foreground.blue)

Concatenate the strings, add a white background and make

them bold.

s3 = pixy.pring(s1 + s2, pixy.background.white + pixy.decorators.bold)

print(s3) ```

Colour shades

```python import pixy

for i in range(0, 0xFF, 3): colour = pixy.TrueColour(i, 0, 0, background=True) print(pixy.pring(" ", colour), end="")

print()

for i in range(0, 0xFF, 3): colour = pixy.TrueColour(0, i, 0, background=True) print(pixy.pring(" ", colour), end="")

print()

for i in range(0, 0xFF, 3): colour = pixy.TrueColour(0, 0, i, background=True) print(pixy.pring(" ", colour), end="")

print() ```

Colour gradient

```python import pixy

def gradient(coloura, colourb):

output = []

for i in range(0, 100, 2): 

    p = i / 100 

    output.append((
        int(colour_a[0] + p * (colour_b[0] - colour_a[0])),
        int(colour_a[1] + p * (colour_b[1] - colour_a[1])),
        int(colour_a[2] + p * (colour_b[2] - colour_a[2]))
    ))

return output

redtogreen = gradient((255, 0, 0), (0, 255, 0)) greentoblue = gradient((0, 255, 0), (0, 0, 255)) bluetored = gradient((0, 0, 255), (255, 0, 0))

for colour in redtogreen: print(pixy.pring(" ", pixy.TrueColour(*colour, background=True)), end="")

print()

for colour in greentoblue: print(pixy.pring(" ", pixy.TrueColour(*colour, background=True)), end="")

print()

for colour in bluetored: print(pixy.pring(" ", pixy.TrueColour(*colour, background=True)), end="")

print() ```

License

pixy is licensed under the MIT license.

Owner

  • Name: Kian Cross
  • Login: kiancross
  • Kind: user
  • Location: United Kingdom
  • Company: University of Cambridge

GitHub Events

Total
Last Year

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 70
  • Total Committers: 2
  • Avg Commits per committer: 35.0
  • Development Distribution Score (DDS): 0.386
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
dependabot[bot] 4****] 43
Kian Cross k****n@k****k 27
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 1
  • Total pull requests: 93
  • Average time to close issues: 8 minutes
  • Average time to close pull requests: 12 days
  • Total issue authors: 1
  • Total pull request authors: 2
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.25
  • Merged pull requests: 66
  • Bot issues: 0
  • Bot pull requests: 70
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • kiancross (1)
Pull Request Authors
  • dependabot[bot] (69)
  • kiancross (23)
Top Labels
Issue Labels
Pull Request Labels
dependencies (69) python (50) github_actions (19)

Dependencies

poetry.lock pypi
  • atomicwrites 1.4.0 develop
  • attrs 21.2.0 develop
  • black 22.6.0 develop
  • click 8.0.3 develop
  • colorama 0.4.4 develop
  • coverage 6.4.2 develop
  • importlib-metadata 4.10.0 develop
  • iniconfig 1.1.1 develop
  • mypy 0.971 develop
  • mypy-extensions 0.4.3 develop
  • packaging 21.3 develop
  • pathspec 0.9.0 develop
  • platformdirs 2.4.0 develop
  • pluggy 1.0.0 develop
  • py 1.11.0 develop
  • pyparsing 3.0.6 develop
  • pytest 7.1.2 develop
  • pytest-cov 3.0.0 develop
  • tomli 1.2.3 develop
  • typed-ast 1.4.3 develop
  • typing-extensions 4.0.1 develop
  • zipp 3.6.0 develop
pyproject.toml pypi
  • black ^22.6 develop
  • mypy ^0.971 develop
  • pytest ^7.1.2 develop
  • pytest-cov ^3.0.0 develop
  • python >=3.7
.github/workflows/codeql.yml actions
  • actions/checkout v3 composite
  • github/codeql-action/analyze v2 composite
  • github/codeql-action/init v2 composite
.github/workflows/publish.yml actions
  • Gr1N/setup-poetry v7 composite
  • actions/cache v3 composite
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/test.yml actions
  • Gr1N/setup-poetry v7 composite
  • actions/cache v3 composite
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • codecov/codecov-action v3.1.1 composite