-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 508e684
Showing
6 changed files
with
808 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
tests |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .colour_splash import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |