Skip to main content

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 if shutil.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 if shutil.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 through rows and cols. If mode is "fit", the dimensions are given through shutil.get_terminal_size(), with rows and cols as fallback. Default value "fit"

  • color_map (dict[str, str]) - A dict with keys as cell symbols and values as colors recognized by termcolor.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ttygrid-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

ttygrid-0.1.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file ttygrid-0.1.2.tar.gz.

File metadata

  • Download URL: ttygrid-0.1.2.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

Hashes for ttygrid-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f038bf2a93d3bd9fe4aed26b3096f817c873f9c840553c3f305a8a212380fd44
MD5 e9aef6b3f8d304af5728347ae25c9207
BLAKE2b-256 ef67477cd67a8a3ac84303d29572cf8bb5f7d4e53e0d00c5ac20148dc4a6fac5

See more details on using hashes here.

File details

Details for the file ttygrid-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ttygrid-0.1.2-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

Hashes for ttygrid-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a5e243639636ac1aeb7b1eaca0ae8cb8514dd3e79ff92f2e09b849b0db15e77
MD5 ce1696ec973003cd315b428f90060187
BLAKE2b-256 f2d854ad1985daffbff4aa377af95e3c311460be28f444c76e77057e62cf78db

See more details on using hashes here.

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