Terminal I/O with a pixel-like abstraction
Project description
termpixels
the terminal as a character-cell matrix
Get it
This project is on PyPI.
Alternatively, just pip install -e .
in the root directory.
Purpose
Creating programs that run inside of terminals seems convoluted. The goal of termpixels is to abstract the terminal into a 2D array of "pixels", or character cells, which each contain a single text character, a foreground color, and a background color. termpixels allows you to modify the screen contents anywhere, at any time, and then handles updating the terminal automatically.
Limitations
There are lots of great libraries for coloring terminal output. This one is designed for full-screen applications that completely control the contents of the screen. That means that it automatically saves and clears the screen, resets the cursor position, and accepts input in cbreak mode.
Demo
from termpixels.app import App
from termpixels.color import Color
from time import time
from math import sin
class FunTextApp(App):
def __init__(self):
super().__init__()
def on_frame(self):
self.screen.clear() # remove everything from the screen
text = "Hello world, from termpixels!"
for i, c in enumerate(text):
f = i / len(text)
color = Color.hsl(f + time(), 1, 0.5) # create a color from a hue value
x = self.screen.w // 2 - len(text) // 2 # horizontally center the text
offset = sin(time() * 3 + f * 5) * 2 # some arbitrary math
y = round(self.screen.h / 2 + offset) # vertical center with an offset
self.screen.print(c, x + i, y, fg=color) # draw the text to the screen buffer
self.screen.update() # commit the changes to the screen
if __name__ == "__main__":
FunTextApp().start()
Features
- Unix terminal feature detection with terminfo (via Python curses)
- Windows support through Win32 Console API
- Terminal (re)size detection
- Asynchronous input
- Keyboard input with support for special keys like arrows, function keys, escape, etc.
- Mouse click and move input in terminals supporting xterm mouse
- 16, 256, and true color output (with detection for best supported mode)
- No reliance on ncurses except for terminfo lookup
- 100% Python
- and more
Inspiration
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 Distributions
Built Distribution
Hashes for termpixels-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 615210aa028eddf1434ead6d5cc96d5e4a29a2135a82896712e03a315aa32ff9 |
|
MD5 | 010c7e148d112fe0bcb0383486b95b27 |
|
BLAKE2b-256 | cfbb8f2dd17e6b6e819abb677fb6ec479925c035cec7ca4f9c87412551f6b9a2 |