erikrose / blessings

A thin, practical wrapper around terminal capabilities in Python
http://pypi.python.org/pypi/blessings
MIT License
1.44k stars 136 forks source link

Should blessings have convenience functions? #70

Open ccorcos opened 9 years ago

ccorcos commented 9 years ago

Hey, I'm don't write command line applications terribly often, but when I do, I always find myself writing a variety of helper functions. For example here's what I have thus far for my current project:

from blessings import Terminal

term = Terminal()

def selectInput(options):
    for i in range(len(options)):
        print "%d) %s?" % (i+1, str(options[i]))

    select = -1
    while select == -1:
        value = raw_input("select [%d-%d]: " % (1, len(options)))
        try:
            i = int(value) - 1
            if i in range(len(options)):
                select = i
            else:
                print term.move_up, term.clear_eol, term.move_up
        except:
            print term.move_up, term.clear_eol, term.move_up

    print ""
    return options[select]

def validatedInput(question, validate):
    value = -1
    while True:
        value = raw_input(question)
        try:
            if validate(value):
                break
            else:
                print term.move_up, term.clear_eol, term.move_up
        except:
            print term.move_up, term.clear_eol, term.move_up
    return value

selectInput basically gives you a number of options and returns the option you selected. validatedInput is basically just a wrapper for "raw input" but makes sure you have entered in a valid input.

Its super simple, but I'm curious if you know of any resources that have these kinds of UI functions? Maybe it could be a good addition to this project?

jquast commented 9 years ago

(I am not the project leader, only a minor contributor so far, but you deserve some reply)

Probably not a good addition for this project: blessings is meant to be only a thin API above curses and related functions. Though it may be used directly to write applications, it may also be used by another library to provide more high-level functions such as user interfaces that you propose.

In some terms you can think of blessings as comparable to the OpenGL library, but for terminals -- what you are seeking is something more like what the GTK library is for terminals.

I would personally like to see some sort of UI library that uses blessings -- there are a number of people working on that. @thomasballinger for example has project https://github.com/thomasballinger/curtsies

https://github.com/wardi/urwid is also a popular choice. It does not use blessings (and in my opinion is very unpythonic, it could be said to be javaish if there such a term). But you may find what you need there if you don't particularly care about how you compose code.