Code Monkey home page Code Monkey logo

curseless's Introduction

Curseless

Provides a simple framework for making interactive terminal apps.

The approach is based off of the Elm architecture.

The view is declared using provided elements:

TextNode(Location(),
         "Hello World",
         style=Style(
             x_rel=ALIGN_RIGHT,
             y_rel=ALIGN_RIGHT,
             style=HIGHLIGHT
         ))

This would render "Hello World" (highlighted) in the bottom-right corner of the terminal.

Each time the view updates, the library (naively) figures out what has changed and what needs to be deleted and renderered.

Requirements:

  • Uses asyncio (Python3, version 3.5+)
  • Curses (A Python library for buidling things on the command line)

Linting:

  • run flake8 within the project directory

Testing:

To run the tests, run nosetests from within the project directory

Running samples:

  • python -m samples.todo.main

See the sample folder to get an idea of the different kinds of things we can build with this:

Hangman:

Alt Text

Brainstorming:

Alt Text

Canonical Todo List:

Alt Text

Some Example Code:

Most files will provide 3 functions:

  1. Model - a global state (should be immutable)
class Todo(NamedTuple):
    task: str 
    completed: bool = False


class Model(NamedTuple):
    todos: List[Todo]

Note that using MyPy typing is optional but potentially helpful


2. Update - a function that is passed the current model state and a message Message constrution helper:

Toggle = Message[int]('TOGGLE')

Snippet of relevant code that updates the model:

index = Toggle.retrieve_payload(msg)
todos = [todo if i != index else Todo(task=todo.task,
                                      completed=not todo.completed)
         for i, todo in enumerate(model.todos)]
return model._replace(todos=todos)



3. View - Takes in the model and renders to the terminal

Portion that renders the items:

def render_todo(i, todo, prefix):
    if todo.completed:
        style = Style(style=[GREEN, DIM])
    else:
        style = Style(style=DEFAULT)
    return TextNode(Location(y=i), prefix + todo.task, style=style)


def render_todos(todos, prefix):
    return [render_todo(i, todo, prefix) for i, todo in enumerate(todos)]


def list_todos(location, model, prefix='- '):
    return CompositeNode(location, render_todos(model.todos, prefix))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.