curses_menu.py
Go to the documentation of this file.
00001 
00002 # Simple menu using curses. Inherit this class and add your own
00003 # functionality.
00004 ## author: Advait Jain
00005 
00006 import sys
00007 import curses
00008 
00009 class CursesMenu():
00010     def __init__(self):
00011         self.stdscr = curses.initscr()
00012         curses.start_color()
00013         curses.noecho()
00014         curses.cbreak()
00015         self.stdscr.keypad(1)
00016         self.menu_dict = {}
00017         self.key_list = [] # to esnure that menu is displayed in the same order as it is created.
00018 
00019     def begin_menu(self, title):
00020         self.menu_title = title
00021 
00022     def add_item(self, key, text, function=None):
00023         self.menu_dict[key] = (text, function)
00024         self.key_list.append(key)
00025 
00026     def finish_menu(self, text):
00027         self.menu_finish = text
00028 
00029     def display_menu(self):
00030         self.row, self.col = 0, 0
00031         self.stdscr.addstr(self.row, self.col, self.menu_title)
00032 
00033         for k in self.key_list:
00034             self.row += 1
00035             self.col = 2
00036             self.stdscr.addstr(self.row, self.col, str(k)+': '+self.menu_dict[k][0])
00037 
00038         self.row += 2
00039         self.col = 0
00040         self.stdscr.addstr(self.row, self.col, self.menu_finish)
00041         self.col = len(self.menu_finish) + 2
00042         self.stdscr.move(self.row, self.col)
00043 
00044     def run(self):
00045         self.display_menu()
00046         while 1:
00047             c = chr(self.stdscr.getch())
00048             self.stdscr.clear()
00049             self.display_menu()
00050             self.stdscr.addstr(self.row, self.col, c)
00051 
00052             notification_row = self.row+1
00053             notification_col = 5
00054             self.stdscr.move(notification_row, notification_col)
00055             if c not in self.menu_dict:
00056                 self.stdscr.addstr('Incorrect option')
00057             else:
00058                 t,f = self.menu_dict[c]
00059                 if f == None:
00060                     self.stdscr.addstr('Unimplemented option')
00061                 else:
00062                     # call the function.
00063                     self.stdscr.addstr('Going to '+ t)
00064                     f(c)
00065             self.stdscr.move(self.row, self.col)
00066 
00067     def exit(self, key=''):
00068         curses.nocbreak();
00069         self.stdscr.keypad(0);
00070         curses.echo()
00071         curses.endwin()
00072         sys.exit()
00073 
00074     def ros_service_call(self, option):
00075         # make a ros service call to perform appropriate function.
00076         pass
00077 
00078 
00079 if __name__ == '__main__':
00080 
00081     cm = CursesMenu()
00082     cm.begin_menu('Here is what I can do for you today:')
00083     cm.add_item('f', 'Fly')
00084     cm.add_item('g', 'Make a ROS service call', cm.ros_service_call)
00085     cm.add_item('q', 'Quit', cm.exit)
00086     cm.finish_menu('Select you option')
00087     cm.run()
00088 
00089 
00090 
00091 
00092 
00093 
00094 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06