Skip to main content

A powerful terminal management library.

Project description

slate

Slate

A powerful terminal management library.

pip install sh40-slate

rule

Purpose

Slate handles most of the interactions one might be expected to do while writing a UI application for terminals (TUI). It does so in a simple to understand, Pythonic manner, and uses a simple synchronous API to allow for maximum compatibility with existing codebases.

rule

Feature highlights

Screen API

Under the hood, Slate's terminal objects each have a Screen. This screen is used for creating fast diffs of the display state between renders, allowing you to draw only the parts that changed.

Anyone who's worked with terminals before knows their rendering is almost always the primary bottleneck of any application. They are surprisingly awful at rendering a full-screen's result, especially when clearing the display beforehand. Many applications result to hand-crafted update systems that only redraw the widgets that change, but that can still be quite intensive based on the size and content of each.

Slate handles cell-level diffing without you ever having to think about it. You just draw content to the terminal, and we will keep track of all the changes to display on the next draw, with practically 0 performance hit.

import time
from random import randint

from slate import terminal, Span, Color

# Hide the terminal's cursor
terminal.show_cursor(False)

content = [
    Span(
        "X" * terminal.width,
        bold=True,
        foreground=Color.black().lighten(2),
        background=Color.black(is_background=True).lighten(1),
    )
    for _ in range(terminal.height)
]

# Use an alt buffer to keep the pre-run terminal state intact
with terminal.alt_buffer():
    for line in content:
        terminal.write(content)

    terminal.draw()

    # Draw a '0' to a random spot on the screen, 60 times a second
    #
    # This type of operation is really taxing on terminals (especially old ones,
    # like Terminal.app) as it generally is done by redrawing the entire screen.
    # That's no longer gonna be a problem for us.
    while True:
        cursor = (randint(0, terminal.width - 1), randint(0, terminal.height - 1))

        # Make sure no more than 1 cell changes; a sanity check for the most part,
        # as this setup guarantees it.
        assert terminal.write("0", cursor=cursor) <= 1

        terminal.draw()
        time.sleep(1 / 60)

Terminal's smart cursor

If you paid attention, you might have noticed that we never moved the terminal's cursor while drawing the initial lines of content. This is because the terminal will always track and move its own cursor when written to, so it knows when it ran out of space and needs to move to a new line.

from slate import terminal

print(terminal.cursor)  # (0, 0)

terminal.write("1")
print(terminal.cursor)  # (1, 0)

It will even wrap while writing!

from slate import terminal, getch

# It will even wrap while writing!
terminal.write("#" * (terminal.width - 5))
terminal.write("This is long, but it will wrap around to the next line.")
terminal.draw()

getch() # Wait for input so the shell prompt doesn't slide things out of view

Sophisticated color tools

You might have noticed the usage of the lighten and darken API for colors above. These are amongst the many tools we provide for working with colors, which also include blending, generating W3C guidelines compliant contrast colors (white for dark colors, black for light ones), generating entire 4-color palettes with multiple of the most commonly used strategies, and more.

Best part; it works everywhere. Colors get translated to the best approximation the current terminal can display, so you don't have to worry about things looking completely wonky without true color support. Most terminals (at least ones you should be using / targeting) have true color support nowadays, but our approximations will be good enough to use on older ones as well.

We also have advanced support for the NO_COLOR initiative, but instead of completely stripping the semantic information colors can convey, we translate each color to a greyscale value that matches with the perceived lightness, keeping the application informative.

Color grid example

Documentation

Once the library gets to a settled state (near 1.0), documentation will be hosted both online and as a celx application. Until then peep the examples folder, or check out some of the references by using python3 -m pydoc <name>.

See also

This library is mostly supposed to power some higher level tools, so using it raw might not be ideal. Thankfully, we have two projects that can help with that:

  • Zenith: A markup language with palette generation, built on Slate's Span and Color primitives.
  • Celadon: A TUI library that uses Slate's Terminal to handle all terminal-interfacing.
  • celx: A hypermedia-driven TUI framework built on top of Celadon.

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

sh40_slate-0.9.7.tar.gz (37.4 kB view details)

Uploaded Source

Built Distribution

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

sh40_slate-0.9.7-py3-none-any.whl (42.3 kB view details)

Uploaded Python 3

File details

Details for the file sh40_slate-0.9.7.tar.gz.

File metadata

  • Download URL: sh40_slate-0.9.7.tar.gz
  • Upload date:
  • Size: 37.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sh40_slate-0.9.7.tar.gz
Algorithm Hash digest
SHA256 a24967a92fbacc6eb11ca5ac269cfc805a065f73a1363843a27c6566595caa84
MD5 57adcdf8881e4c1c90295c3eb2be1270
BLAKE2b-256 44b506e8e1adc46c915f5d3be7c61e9baaa9d386cb053ea1f06e5de7d2a58679

See more details on using hashes here.

File details

Details for the file sh40_slate-0.9.7-py3-none-any.whl.

File metadata

  • Download URL: sh40_slate-0.9.7-py3-none-any.whl
  • Upload date:
  • Size: 42.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for sh40_slate-0.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 be8d010327fc584bb999e708fb613ed693d9d490fb0aa241ec682bbd91942077
MD5 877c0cbf6dc35d9c99f9846451284418
BLAKE2b-256 d5497e2a3bb0a00d93ad150362d9a6e66fc809125fcadab5ec47dfa5d15a9479

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