Skip to main content

Terminal manipulation utilities for Python applications, including ANSI color codes, cursor control, text styling, and interactive output formatting

Project description

Termite: Terminal Utilities

Terminal manipulation utilities for Python: colors, styles, emojis, cursor-control and string-based formatting.

Features

  • text-colors
    • basic named colors
    • arbitrary hex, rgb, or rgba(which gets blended back to rgb)
    • sum to merge colors, e.g. BLUE + RED
  • highlight-colors
    • basic named colors
    • arbitrary hex, rgb, or rgba(which gets blended back to rgb)
    • sum to merge colors, e.g. BLUE + RED
  • styles
    • bold
    • italic
    • underline
    • strikethrough
  • cursor actions
    • write_ahead/save/restore
    • clear/erase
    • up/down/left/right
    • pos/row/col
  • emojis
    • :fire => 🔥
  • special fonts and ASCII art
    • Big Text
    • Boxed Text
  • Markdown Rendering using termite-md

How to use

  1. in Python
  2. in the command-line

Install

pip install modularizer-termite

Quick Start

from termite import subprint, full_demo

subprint("GREEN[BOX[BIG[hello, world]]]\nBGYELLOW[Termite can do a lot :fire:]")
full_demo()

Tools

  • from termite import t: access pretty much any const or builder
    • print(t.GRAY + "some text" + t.RST) or print(t.GRAY("some text"))
  • from termite import sub: do string substitutions to get the fancy string
  • from termite import subprint: do string substitutions then print the fancy string
  • from termite import cprint: each statement overwrites the last, print with black text and gray completions, built for an autocomplete solution
  • termite.raw: JUST the strings, none of our fancy classes or getters
  • termite.chars.CTRL: object which gets the key equivalents of Ctrl combos, e.g. CTRL + 'c', CTRL["c"], CTRL.C

String Substitution with sub()

The sub() function provides a declarative string-based API for terminal formatting.

Basic Usage

from termite import sub, subprint

a = sub("RED[text] GREEN[text] BLUE[text]")
b = sub("BOLD[bold] ITALIC[italic] UNDERLINE[underline]")
subprint("BOLDRED[Error message]")

Stacked Keys

Stack multiple keys together. Keys are matched longest-first (e.g., RED before R):

from termite import sub, subprint

x = sub("BOLDITALICREDBLU[bold italic merged red+blue]")
subprint("BOLDUNDERLINEGREENYELLOW[bold underline green+yellow]")

Merged Colors

Multiple colors are automatically merged by averaging RGB values:

from termite import subprint

subprint("BLUERED[merged blue+red]")
subprint("GREENYELLOW[green+yellow]")
subprint("REDBLUEGREEN[three colors merged]")

Short Aliases

  • B = BOLD, I = ITALIC, U = UNDERLINE, D = DIM, S = STRIKETHROUGH, R = REVERSE, H = HIDDEN
from termite import subprint

subprint("B(bold) I(italic) U(underline)")
subprint("BOLD+ITALIC+UNDERLINE[bold italic underline]")

Cursor Control

from termite import subprint

subprint("LEFT RIGHT UP DOWN")              # Default: 1
subprint("LEFT[3] RIGHT[5] UP[2] DOWN[4]")  # With args
subprint("POS[10,20] ROW[5] COL[10]")       # Positioning
subprint("CLEAR ERASE BACK")                # Line control
subprint("WA[GRAY[completion]]")            # Write ahead

RGB Colors

from termite import subprint

subprint("rgb[#FF5733][custom orange]")
subprint("rgb[#258][shorthand]")
subprint("rgb[255,100,50][RGB tuple]")
subprint("bgrgb[#0000FF][background]")

Nested Patterns

from termite import subprint

subprint("GREEN[BOLD[bold green]]")
subprint("RED[BOLD[UNDERLINE[nested]]]")
subprint("GREEN[BOLD[FGRGB[#FF0000][red inside]]]")

Examples

from termite import subprint

# Status messages
subprint("GREEN[✓ Success] RED[✗ Error]")

# Progress
for i in range(10):
    subprint(f"BOLD[Progress: ]GREEN[{i+1}/10]", end="")
    subprint("CLEAR")

# Interactive completion
pre="prefix"
completion="test"
subprint(f"{pre}WA[GRAY[{completion}]]")

Colors API

from termite import colors

# Foreground colors
colors.red("text")
colors.green("text")
colors.bright_red("text")

# Background colors
colors.bg.blue("text")

# Styles
colors.bold("text")
colors.italic("text")

# RGB colors
colors("#FF5733")("text")
colors((255, 87, 51))("text")

# Combining
colors.bold + colors.red("text")
colors(foreground="red", background="blue", style="bold")

Cursor Control

from termite import cursor

cursor.up(n=1)        # Move up n lines
cursor.down(n=1)      # Move down n lines
cursor.left(n=1)     # Move left n chars
cursor.right(n=1)     # Move right n chars
cursor.pos(row, col)  # Move to position
cursor.col(n=0)       # Move to column
cursor.row(n)         # Move to row
cursor.clear_line()   # Clear to end of line
cursor.erase_line()   # Erase entire line
cursor.write_ahead(text)  # Write without moving cursor
cursor.backspace()    # Backspace

Print with Suggestions

import time
from termite import cprint

cprint("User typed: ", completion="completion")
time.sleep(1)
cprint("User typed: abc", completion="def")  # Updates previous

Module Structure

  • colors.py - Color and styling API
  • cursor.py - Cursor movement and positioning
  • sub.py - String substitution for formatting
  • terminal.py - High-level printing utilities
  • raw - Low-level ANSI codes
  • chars.py - Control character mappings
  • clie.py - Command Line Interface which calls sub

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

modularizer_termite-0.1.0.tar.gz (51.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modularizer_termite-0.1.0-py3-none-any.whl (62.5 kB view details)

Uploaded Python 3

File details

Details for the file modularizer_termite-0.1.0.tar.gz.

File metadata

  • Download URL: modularizer_termite-0.1.0.tar.gz
  • Upload date:
  • Size: 51.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for modularizer_termite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6e44b76eaa702d68e9f1c1d632081e433d0c6ceeaae71008b17f91ba987e741e
MD5 2f90d8b76b5f62d78b179091963f91d9
BLAKE2b-256 731c735cf7a7927ec1e587cb5cf2e636a2ee48afbd84a2370e1f6331e6a9323f

See more details on using hashes here.

Provenance

The following attestation bundles were made for modularizer_termite-0.1.0.tar.gz:

Publisher: publish.yml on modularizer/termite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modularizer_termite-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for modularizer_termite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f561ccaff3017546f25ee60e7c80b1c05bc952c6c8cd69440c4ee2c71f5524d
MD5 1072180c91b9472eaeb00698ace1f841
BLAKE2b-256 51f57b6dddcc26429547969a815b7726f091f4a7f0a660f9037022b6e9673e9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for modularizer_termite-0.1.0-py3-none-any.whl:

Publisher: publish.yml on modularizer/termite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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