Simple terminal grid rendering library
Project description
ttygrid
ttygrid is a grid-based framework for building terminal simulations, games, and visualizations.
Features
- Grid class and cell class
- Functions to get and set cells
- Inbuilt coloring for different symbols using
termcolor - Auto adjust to terminal size using
shutil
v0.1 is out!
Quick Start Guide
You can install ttygrid using pip using:
pip install ttygrid
Alternatively, you could use:
pip3 install ttygrid
Once installed, import with
from ttygrid import Grid, Cell
Example
import random
import time
from ttygrid import Grid
def main():
grid = Grid()
while True:
cells = grid.get_all_cells(empty=True)
for cell in cells:
cell.symb = random.choice(['0', '1'])
grid.draw_cells(cell)
grid.clear_term()
print(grid)
time.sleep(0.1)
if __name__ == "__main__":
main()
Documentation
Grid()
Grid(rows=80, cols=40, mode="fit", grid=None, color_map=COLORS)
Creates a new ttygrid grid instance that can be manipulated using the library.
Args
-
rows(int) - If mode is "custom", the number of rows in the grid. If mode is "fit", the default number of rows in the grid ifshutil.get_terminal_size()fails to determine terminal size. Default value 80. -
cols(int) - If mode is "custom", the number of cols in the grid. If mode is "fit", the default number of cols in the grid ifshutil.get_terminal_size()fails to determine terminal size. Default value 40. -
mode(str) - Can be either "fit" or "custom" (case-sensitive). If mode is "custom", the dimensions of the grid are given throughrowsandcols. If mode is "fit", the dimensions are given throughshutil.get_terminal_size(), withrowsandcolsas fallback. Default value "fit" -
color_map(dict[str, str]) - A dict with keys as cell symbols and values as colors recognized bytermcolor.colored(). The default value is:
COLORS = {
'0': "green",
'1': "black",
'2': "yellow",
'3': "blue",
'4': "light_yellow",
}
str() (Grid)
print(grid)
Prints the formatted grid to the terminal.
show_size()
show_size(grid)
Prints the number of Lines (rows) and Columns (cols) in the grid. To get them programmatically, use grid.rows and grid.cols instead.
clear_term()
clear_term()
Clears the terminal. Typically used before printing new frames.
get_cell()
get_cell(col, row)
Returns the Cell object located at the coordinates (col, row) Raises ValueError if cell does not exist.
validate_cell()
validate_cell(cell)
Raises ValueError if cell coordinates not found in grid
get_all_cells()
get_all_cells(empty=True)
Get a list of all cells in the grid.
If empty is True, empty cells are also included.
If empty is False, empty cells are not included.
The default value is True
draw_cells()
draw_cells(*cells)
Draws all valid cells passed as arguments onto the grid.
clear()
clear()
Clears the grid (resets it to all cells being None)
redraw_frame()
redraw_frame(cell_map)
The grid is redrawn using cell_map.
Effectively, the value of the grid is now cell_map.
A cell_map value not made of cells or having irregular sides compared to original grid.rows and grid.cols, results in undefined behaviour.
It is best to first call get_all_cells() and pass in modified values of the call to redraw_frame()
Cell Class
Cell()
Cell(x, y, symb=None)
Args:
- x - the x (col) position of the Cell
- y - the y (row) position of the Cell
- symb - the symbol represented by the cell. A value of None indicates an empty cell.
str()
print(cell)
Prints the cell position and symbol
Known Issues
- Extra newline after print (Scheduled to be fixed in v0.2)
Roadmap
- Drawing primitives
- Demos
- Customizable status bar for simulation
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ttygrid-0.1.1.tar.gz.
File metadata
- Download URL: ttygrid-0.1.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c8c84274a0b78ffe665eda874cbd581bea4519d5419af19003d2e3f115032c3
|
|
| MD5 |
9a62165b89965e90f7cd9e28e3a96671
|
|
| BLAKE2b-256 |
35981e8cb209cd956cfb9e9b2c019fa38308b437ecbc4ce1490987effb2df9c9
|
File details
Details for the file ttygrid-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ttygrid-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00b1993a070ef553911e880c295947ff172ae28a241c013143041b570d61c1ab
|
|
| MD5 |
bae3d57c3aa9bad531a641bf3510e699
|
|
| BLAKE2b-256 |
1979233c2e3f58fd3e80526a14787e8ca94b1596b85b9b1a51e3fed2ad4682e7
|