Print 'text color' and 'text format' on Term with Python
Project description
term-printer
Print 'text color' and 'text format' on Term with Python
※ It may not work depending on the OS and shell used.
PIP
$ pip install term-printer
import
from term_printer import Color, Color256, ColorRGB, StdText, cprint
If you want to override bultin print
function
from term_printer import Color, Color256, ColorRGB, StdText, cprint as print
Usage
1. Attrs print
Applies to all characters.
You can specify Format
, Color
, Color256
, and ColorRGB
.
Able to specify more than one.
source
from term_printer import Color, Color256, ColorRGB, Format, cprint
# default
cprint("this is a default pen")
# bold
cprint("this is a bold pen", attrs=[Format.BOLD])
# italic
cprint("this is a italic pen", attrs=[Format.ITALIC])
# red color
cprint("this is a red pen", attrs=[Color.RED])
# bright red color
cprint("this is a bright red pen", attrs=[Color.BRIGHT_RED])
# background magenta color
cprint("this is a bg magenta pen", attrs=[Color.BG_MAGENTA])
# background bright magenta color
cprint("this is a bg bright magenta pen", attrs=[Color.BG_BRIGHT_MAGENTA])
# magenta color & italic
cprint("this is a magenta italic pen", attrs=[Color.MAGENTA, Format.ITALIC])
# bold & italic
cprint("this is a bold italic pen", attrs=[Format.BOLD, Format.ITALIC])
# cyan color & bold & italic
cprint("this is a cyan bold italic pen", attrs=[Color.CYAN, Format.BOLD, Format.ITALIC])
# 8-bit color 154
cprint("this is a 8-bit 154 pen", attrs=[Color256(154)])
# 8-bit bg color 154 and magenta color
cprint("this is a bg 8-bit 154 pen", attrs=[Color256(154, is_bg=True), Color.MAGENTA])
# rgb(100, 255, 255) color
cprint("this is a rgb(100, 255, 255) pen", attrs=[ColorRGB(100, 255, 255)])
# bg rgb(100, 255, 255) color and black color
cprint(
"this is a bg rgb(100, 255, 255) pen",
attrs=[ColorRGB(100, 255, 255, is_bg=True), Color.BLACK],
)
result
2. StdText print
source
from term_printer import Color, Format, StdText, cprint
# default
cprint("this is a default pen")
# bold
cprint(f"this is a {StdText('bold', Format.BOLD)} pen")
# italic
cprint(f"this is a {StdText('italic', Format.ITALIC)} pen")
# reverse
cprint(f"this is a {StdText('reverse', Format.REVERSE)} pen")
# red color
cprint(f"this is a {StdText('red', Color.RED)} pen")
# background magenta color
cprint(f"this is a {StdText('bg magenta', Color.BG_MAGENTA)} pen")
# magenta color & italic
cprint(f"this is a {StdText('magenta', Color.MAGENTA)} {StdText('italic', Format.ITALIC)} pen")
# bold & italic
cprint(f"this is a {StdText('bold', Format.BOLD)} {StdText('italic', Format.ITALIC)} pen")
# cyan color & bold & italic
cprint(f"this is a {StdText('cyan', Color.CYAN)} {StdText('bold', Format.BOLD)} {StdText('italic', Format.ITALIC)} pen")
result
Color
class Color(Enum)
Enum class.
Example
from term_printer import Color
Color.RED # RED foreground color
Color.BG_RED # RED background color
Color.BLUE # BLUE foreground color
Color.BG_BLUE # BLUE background color
source
Definition is 3-bit and 4-bit colors
Color256
class Color256(n: int, is_bg: bool = False)
-
First argument takes
int
(0 - 255). -
Second argument takes
bool
(default: False). False: change foreground color True: change background color
Example
from term_printer import Color256
Color256(9) # RED foreground color
Color256(9, True) # RED background color
Color256(12) # BLUE foreground color
Color256(12, True) # BLUE background color
Definition is 8-bit 256 colors
ColorRGB
class ColorRGB(r: int, g: int, b: int, is_bg: bool = False)
-
Three arguments takes
int
(0 - 255). -
Fourth argument takes
bool
(default: False). False: change foreground color True: change background color
Example
from term_printer import ColorRGB
ColorRGB(255, 0, 0) # RED foreground color
ColorRGB(255, 0, 0, True) # RED background color
ColorRGB(0, 0, 255) # BLUE foreground color
ColorRGB(0, 0, 255, True) # BLUE background color
Format
class Format(Enum):
BOLD = 1
FAINT = 2
ITALIC = 3
UNDERLINE = 4
BLINK = 5
FAST_BLINK = 6
REVERSE = 7
CONCEAL = 8
STRIKE = 9
Enum class.
Example
from term_printer import Format
Format.BOLD # BOLD font
Format.FAINT # FAINT font
Format.ITALIC # ITALIC font
Format.UNDERLINE # UNDERLINE font
Definition is SGR
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file term-printer-1.1.tar.gz
.
File metadata
- Download URL: term-printer-1.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abdf796eea6b1e5a4d6f591e6f5458d9f470fd26e648c523c21c328bae586800 |
|
MD5 | 97a6175a0f11572ed3f033e1efbb254d |
|
BLAKE2b-256 | b14465c249afcec483f32cc2c9c1ec55d25a4a699447057398be60ffcb040a16 |
File details
Details for the file term_printer-1.1-py3-none-any.whl
.
File metadata
- Download URL: term_printer-1.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcc706451e947157dd264ec98dfa425dd52a22c0f063a8cd22b30c6980ca7cb1 |
|
MD5 | 8859bd6f2a8c6980357807dd645d7e53 |
|
BLAKE2b-256 | 831b5b9a8883fa5eaf90a940c3433fe28b01bfe2548375458b586c462134641d |