curses_app.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 #  Copyright (c) 2012, UC Regents
00004 #  All rights reserved.
00005 #
00006 #  Redistribution and use in source and binary forms, with or without
00007 #  modification, are permitted provided that the following conditions
00008 #  are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the University of California nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 #  POSSIBILITY OF SUCH DAMAGE.
00032 import curses
00033 
00034 class Bag:
00035     pass
00036 
00037 class CursesApp:
00038     def init_curses(self):
00039         self.colors = Bag()
00040         try:
00041             self.stdscr = curses.initscr()
00042             curses.start_color()
00043             curses.use_default_colors()
00044             curses.init_pair(1, curses.COLOR_GREEN, -1)
00045             self.colors.green = curses.color_pair(1)
00046             curses.init_pair(2, curses.COLOR_YELLOW, -1)
00047             self.colors.yellow = curses.color_pair(2)
00048             curses.init_pair(3, curses.COLOR_RED, -1)
00049             self.colors.red = curses.color_pair(3)
00050             curses.init_pair(4, curses.COLOR_BLUE, -1)
00051             self.colors.blue = curses.color_pair(4)
00052             curses.init_pair(5, curses.COLOR_BLACK, -1)
00053             self.colors.black = curses.color_pair(5)
00054             curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_WHITE)
00055             self.colors.black_on_white = curses.color_pair(6)
00056             curses.init_pair(7, curses.COLOR_WHITE, -1)
00057             self.colors.white = curses.color_pair(7)
00058             curses.init_pair(8, curses.COLOR_CYAN, -1)
00059             self.colors.cyan = curses.color_pair(8)
00060             curses.noecho()
00061             curses.cbreak()
00062             curses.curs_set(0)
00063             self.stdscr.nodelay(True)
00064             self.stdscr.clear()
00065             self.stdscr.keypad(True)
00066         except curses.error:
00067             pass
00068         
00069     def restore_screen(self):
00070         curses.curs_set(1)
00071         curses.nocbreak()
00072         curses.echo()
00073         curses.endwin()
00074         
00075         
00076     def curses_msg(self, msg, line=25):
00077         try:
00078             self.stdscr.addstr(line, 0, " "*80)
00079             self.stdscr.addstr(line, 0, msg)
00080             self.stdscr.refresh()
00081         except curses.error:
00082             pass
00083         
00084     def add_field(self, label, text, row=None, col=None, label_color=None):
00085         if label_color is None:
00086             label_color = self.colors.cyan
00087         if row is None and col is None:
00088             self.stdscr.addstr(label, label_color)
00089         else:
00090             self.stdscr.addstr(row, col, label, label_color)
00091         self.stdscr.addstr(text)
00092         self.stdscr.clrtoeol()


starmac_tui
Author(s): bouffard
autogenerated on Sun Jan 5 2014 11:38:30