Usage

To use Hexes in a project:

#!/usr/bin/env python

# The basic imports:
from hexes import (
    Application,
    Box,
    Style,
)
from hexes.behaviors import quit
import logging

logging.basicConfig(
    filename='hexes.log',
    level=logging.DEBUG,
)

# We're going to use this in the logic below; not part of Hexes.
import asyncio

# Layout
#
# You can nest boxes indefinitely, though some layouts may fail on some screen
# sizes. You can specify text for boxes, whether that text should be flowed or
# treated as fixed, whether child boxes should be laid out horizontally or
# vertically, height and width for boxes, etc.
ls_box = Box(
    style=Style(
        flow=False,
    ),
)
input_box = Box(
    editable=True,
    style=Style(
        height=3,
    ),
)

root = Box(
    style=Style(
        layout=Style.Layout.Horizontal,
    ),
    children=(
        Box(
            children=(
                ls_box,
                input_box,
            ),
        ),
        Box(
            style=Style(
                width=20,
            ),
        ),
    ),
)

# Logic
#
# Instantiate the application with the layout attached.
# Register any pre-defined behaviors you want (right now, that's only `quit`)
# using the same mechanism as custom behaviors, `app.on`.
app = Application(root=root)
app.on('q', quit)


# Define custom behavior with the `@app.on` decorator. This decorator
# requires an event identifier, which is either 'ready' or a key identifier
# as returned by `curses.window.getkey`
@app.on('ready')
def input_text(app):
    app.edit(input_box, callback=handle_edit)


@asyncio.coroutine
def handle_edit(app, textbox, characters):
    if ls_box.text is None:
        ls_box.text = ""
    ls_box.text += characters + "\n"
    app.schedule(input_text)


@app.on('j')
def scroll_down(app):
    ls_box.scroll(1)


@app.on('k')
def scroll_up(app):
    ls_box.scroll(-1)

# Run
#
# The context manager helps us clean up no matter what exceptional exit
# conditions we have.
with app:
    app.run()

The text input area is still larval. Give me bug reports!

Musings on the future

What sorts of widgets are important in a terminal app?

  • Text areas
  • Scrollable text areas
  • Auto-scrolling text areas (as for chat or Twitter feed)
  • Text input areas

These widgets should be relatively smart, knowing their own dimensions, when to resize, how to listen to things (some sort of data-binding model here?), how to style themselves, etc.