Skip to main content

A high-level cross platform tty library

Project description

pypi licence code style black

teletype

teletype is a high-level cross platform tty library compatible with Python 2.7 and 3+. It provides a consistent interface between the terminal and cmd.exe by building on top of terminfo and msvcrt and has no dependancies.

Installation

$ pip install teletype

I/O Utilities (teletype.io)

Reading Key Strokes

You can read keystrokes from stdin using get_key. Regular keys get returned as a string with a single character, e.g. "a", "1", "&", etc., while special keys and key combinations are returned as a string description, e.g. "space", "f12", "page-up", "ctrl-c", etc. A listing of the supported key combinations are listed in the codes module.

from teletype.io import get_key

print("Delete C:/ Drive? [y/n]")
selection = ""
while selection.lower() not in ("y", "n"):
    selection = get_key()
    if selection in ("ctrl-c", "ctrl-z", "escape"):
        selection = "n"
if selection == "y":
    print("Deleting C:/ drive...")
    delete_c_drive()
else:
    print("Leaving C:/ drive alone")

Styling Output

You can style strings with COLOURS and effects using style_format. Styles can be passed in either as a space delimited string or in a collection (e.g. a tuple, set, list, etc.). The passed text string is then wrapped in the appropriate ASCII escape sequences and returned. When printed the appropriate styles will be applied.

Alternatively you can you just pass these same parameters to style_print and accomplish this in one fell swoop. style_print takes the same parameters as the regular print function and can be used in place. In python3 you can even import style_print as print and use it in place. In order to pull this compatibility off for python2, the style argument must be specified explitly when calling, however, e.g. style_print("yolo", style="yellow").

Lastly, you can use strip_format to clear a string of any escape sequences that have been previously applied.

from teletype.io import style_format, style_print, strip_format

# All of these will do the same thing, that is print the message in red and bold
print(style_format("I want to stand out!", "bold red"))
print(style_format("I want to stand out!", ("red", "bold")))
style_print("I want to stand out!", style=["red", "bold"])

# Styles are cleared afterwards so everything else gets printed normally
print("I want to be boring")

# If for whatever reason you want to unstyle text, thats a thing too
text = style_format("I don't actually want too be styled", ("red", "bold"))
print(strip_format(text))

Cursor manipulation

The package includes quite a few helper functions to move the CURSOR around the screen. These include erase_lines, erase_screen, hide_cursor, show_cursor, and move_cursor; all of which are fairly self explanitory. The only word of caution is to remember to reset CURSOR visibility as its state will persist after the python interpreter has exited.

Components (teletype.components)

The package also includes components, higher level UI classes that are composed from the I/O functions and can be easily incorporated to any CLI application.

SelectOne

from teletype.components import Select

picker = Select(
    header="Your Favourite Animal?",
    choices=["dog", "bird", "cat", "monkey", "gorilla"],
)
choice = picker.prompt()
print("Your choice: " + choice)

Output

SelectMany

from teletype.components import SelectMany

picker = SelectMany(
    header="Your Favourite Animals?",
    choices=["dog", "bird", "cat", "monkey", "gorilla"],
)
choices = picker.prompt()
print("Your choices: " + ", ".join(choices))

Output

ProgressBar

from time import sleep
from teletype.components import ProgressBar

iterations = 15

def iterable():
    for _ in range(iterations):
        sleep(0.2)
        yield

ProgressBar().process(iterable(), iterations)

Output

Styling Components (teletype.components.config)

You can set component primary and secondary styles using set_style.

from teletype.components import ProgressBar, SelectMany

config = {"style_primary": "yellow", "style_secondary": "magenta"}
iterable = range(25)
choices = (1, 2, 3)

ProgressBar(header="Progress Bar", **config).process(iterable)
SelectMany(choices, header="Select Many", **config).prompt()

Output

You can also change character sets using set_char(key, value) where value is the unicode character you want to use and key is one of:

  • selected
  • unselected
  • arrow
  • block
  • left-edge
  • right-edge

License

MIT. See license.txt for details.

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

teletype-0.3.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

teletype-0.3.1-py2.py3-none-any.whl (10.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file teletype-0.3.1.tar.gz.

File metadata

  • Download URL: teletype-0.3.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.16

File hashes

Hashes for teletype-0.3.1.tar.gz
Algorithm Hash digest
SHA256 75bdadcbe8d0b436b1b5dac84286db297e08a2b285855ec9e78de03e746ee4f9
MD5 4b89072732cf4d1a1b25f2a69532f7eb
BLAKE2b-256 936b33f55733266275012ced267e0ad3b53afb26c288ababde773bc8dc0a4b4c

See more details on using hashes here.

File details

Details for the file teletype-0.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: teletype-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.16

File hashes

Hashes for teletype-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 29b0329ed105b4c6e23206990cfba3c4346582ed33968e6c418c270e1b4216f6
MD5 aefdf016767e7023aac164ccd41f6a9e
BLAKE2b-256 98e106077ec0eedfc28023fea825022166549c44a90b8e6f456649962b2b8fd3

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