Nov. 17, 2013

Simple python list picker using curses

Are you writing a workhorse shell script in python?  Why not curses?

This class, using curses, lets a user select from a simple list of options. The return is a simple list of selected options, or False (for cancel). And, for the OCD in all of us, it preserves terminal window contents after exiting.


First, get from github:

Usage example:

from picker import *
opts = Picker(
    title = 'Select files to delete',
    options = [
        ".autofsck", ".autorelabel", "bin/", "boot/", 
        "cgroup/", "dev/", "etc/", "home/", "installimage.conf",
        "installimage.debug", "lib/", "lib64/", "lost+found/",
        "media/", "mnt/", "opt/", "proc/", "root/",
        "sbin/", "selinux/", "srv/", "sys/",
        "tmp/", "usr/", "var/"

if opts == False:
    print "Aborted!"
    print opts

If the user hits cancel, the routine returns a False - otherwise, you get a simple list:

['.autofsck', '.autorelabel', 'bin/', 'boot/', 'cgroup/']

The return, being a simple list, allows for a fairly readable logic:

if "cgroup/" in opts:
    print "cgroup is done for!"

Now, mind the possible False return, and put it to good use!

Find this interesting, or useful? Consider sharing the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Posts on this blog solely represent my personal opinions and technical experience.

© 2009-2019 Edin (Dino) Beslagic