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.

picker3picker4

First, get picker.py from github: https://github.com/pp19dd/picker

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/"
    ]
).getSelected()

if opts == False:
    print "Aborted!"
else:
    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-2017 Edin (Dino) Beslagic