Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan2357 committed Aug 13, 2022
0 parents commit 508e684
Show file tree
Hide file tree
Showing 6 changed files with 808 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
tests
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Colour Splash
A simple tool enabling easy colouring of terminal outputs

# Download
## Using PIP to use a imported Module
`pip install colour_splash`
## Manually to embed into a project
`git clone https://github.com/lachlan2357/python-colour-splash` into your project directory

# Usage
Include `import colour_splash` at the top of your file

## Functions
### `colour_splash.colour()`
Changes the foreground, background or both-grounds of text
#### Parameters:
- `text` the text you wish to be coloured
- `foreground` (optional): the string value of the name of the colour you wish to use as the foreground (text) colour
- `background` (optional): the string value of the name of the colour you wish to use as the background (highlighted) colour

### `colour_splash.style()`
Changes the style of text
#### Parameters:
- `text`: the text you wish to be styled
- `style` (optional): the string value of the name of the style you wish to use

### `colour_splash.help()`
Lists colours and styles and how they appear on your system
24 changes: 24 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "colour_splash"
version = "0.1.0"
authors = [
{ name="Lachlan Rehder", email="[email protected]" },
]
description = "A simple tool enabling easy colouring of terminal outputs"
readme = "README.md"
license = { file="LICENSE" }
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Development Status :: 4 - Beta",
]

[project.urls]
"Homepage" = "https://github.com/lachlan2357/python-colour-splash"
"Bug Tracker" = "https://github.com/lachlan2357/python-colour-splash/issues"
1 change: 1 addition & 0 deletions src/colour_splash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .colour_splash import *
79 changes: 79 additions & 0 deletions src/colour_splash/colour_splash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import platform

class settings:
force_colours = False

class __config:
escape_start = "\033[0;"
escape_end = "m"

colour_prefix = {
"foreground": 3,
"background": 4
}

colour_suffix = {
"black": 0,
"red": 1,
"green": 2,
"yellow": 3,
"blue": 4,
"magenta": 5,
"cyan": 6,
"white": 7,
"none": 9,
}

style_prefix = {
"none": 0,
"bright": 1,
"dim": 2,
"italic": 3,
"underline": 4,
"slow blink": 5,
"fast blink": 6,
"invert": 7,
"hide": 8,
"strikethrough": 9,
"double underline": 21,
"overline": 53,
}

def colour(text:str, foreground:str = "none", background:str = "none"):
if ("TERM" not in os.environ.keys() or platform.uname().system == "Windows") and not settings.force_colours:
return text

if foreground not in __config.colour_suffix:
raise TypeError(f"\"{foreground}\" is not a valid colour")

if background not in __config.colour_suffix:
raise TypeError(f"\"{background}\" is not a valid colour")

prefix = f"{__config.escape_start}3{__config.colour_suffix[foreground]};4{__config.colour_suffix[background]}{__config.escape_end}"
suffix = f"{__config.escape_start}39;49{__config.escape_end}"
return f"{prefix}{text}{suffix}"

def style(text:str, style:str = "none"):
if ("TERM" not in os.environ.keys() or (platform.uname().system == "Windows" and "TERM" not in os.environ.keys())) and not settings.force_colours:
return text

if style not in __config.style_prefix:
raise TypeError(f"\"{style}\" is not a valid style")

prefix = f"{__config.escape_start}{__config.style_prefix[style]}{__config.escape_end}"
suffix = suffix = f"{__config.escape_start}0{__config.escape_end}"
return f"{prefix}{text}{suffix}"

def help():
print("Foreground Colours")
for _colour in __config.colour_suffix:
print(" \u21B3", f"\"{_colour}\"", "foreground: ", colour(_colour, foreground = _colour))

print("Background Colours")
for _colour in __config.colour_suffix:
print(" \u21B3", f"\"{_colour}\"", "background: ", colour(_colour, background = _colour))

print("Styles")
for _style in __config.style_prefix:
print(" \u21B3", f"\"{_style}\"", "style: ", style(_style, _style))

0 comments on commit 508e684

Please sign in to comment.