From 45a9b95df8fa9f91871d7c98cface4e6c2586729 Mon Sep 17 00:00:00 2001 From: slack Date: Tue, 17 Nov 2009 14:31:59 +0100 Subject: [PATCH] Use curses.wrapper to clean up in case of exception. Also, colors :) --- melon-view.py | 138 ++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 73 deletions(-) diff --git a/melon-view.py b/melon-view.py index 6d0a143..58cd239 100755 --- a/melon-view.py +++ b/melon-view.py @@ -2,6 +2,7 @@ import sys import curses +import curses.wrapper import os import time @@ -20,7 +21,7 @@ class AppInfo: def get_app_info(filename): f = file(filename,"r") try: - f.seek(-200, 2) + f.seek(-2000, 2) except IOError: f.seek(0) @@ -42,76 +43,67 @@ def get_apps_info(): return apps - -# curses init -stdscr = curses.initscr() -rows, cols = stdscr.getmaxyx() -if cols < 80: - print "Terminal is too narrow" - # curses cleanup - stdscr.keypad(0) - curses.nocbreak() - curses.echo() - curses.endwin() - sys.exit(1) - -curses.noecho() -#curses.cbreak() -curses.halfdelay(100) -stdscr.keypad(1) -# app goes here - -numboxes = (rows-1)/3 -cur_pos = 0 -cur_x_pos = 0 -barsize = cols-68; -last_refresh = 0 - - -exit = False -while not exit: - if time.time() - last_refresh > refresh_interval: - last_refresh = time.time() - apps = get_apps_info() - - stdscr.erase() - for i in xrange(min(numboxes, len(apps)-cur_pos)): - app = apps[cur_pos + i] - attribs = curses.A_NORMAL - stdscr.addstr(3*i+1, 2, app.name, attribs) - stdscr.addstr(3*i+1, 65, (app.user+"@"+app.machine)[:barsize], attribs) - stdscr.addstr(3*i+2, 2, app.status[cur_x_pos:cur_x_pos+57], attribs) - - stdscr.hline(3*i+2, 65, "-", barsize) - stdscr.hline(3*i+2, 65, "+", int((app.progress/100.0)*barsize)) - stdscr.addstr(3*i+2, 60, str(app.progress)+"%", attribs) - stdscr.hline(3*(i+1),1,curses.ACS_HLINE, cols-2) - - stdscr.box() - try: - cmd = stdscr.getch() - except: - pass - if cmd == ord('q'): - exit = True - elif cmd == curses.KEY_DOWN: - if cur_pos < len(apps)-numboxes: - cur_pos += 1 - elif cmd == curses.KEY_UP: - if cur_pos > 0: - cur_pos -= 1 - elif cmd == curses.KEY_RIGHT: - cur_x_pos += 10 - elif cmd == curses.KEY_LEFT: - if cur_x_pos > 0: - cur_x_pos -= 10 - stdscr.refresh() - -# curses cleanup -stdscr.keypad(0) -curses.nocbreak() -curses.echo() -curses.endwin() - - +def main(stdscr): + curses.use_default_colors() + rows, cols = stdscr.getmaxyx() + if cols < 80: + print "Terminal is too narrow" + return + + numboxes = (rows-1)/3 + cur_pos = 0 + cur_x_pos = 0 + barsize = cols-68; + last_refresh = 0 + + curses.init_pair(1, curses.COLOR_WHITE, -1) + curses.init_pair(2, curses.COLOR_GREEN, -1) + curses.init_pair(3, curses.COLOR_BLUE, -1) + + exit = False + while not exit: + if time.time() - last_refresh > refresh_interval: + last_refresh = time.time() + apps = get_apps_info() + + stdscr.erase() + for i in xrange(min(numboxes, len(apps)-cur_pos)): + app = apps[cur_pos + i] + stdscr.attrset(curses.color_pair(1) | curses.A_NORMAL) + stdscr.addstr(3*i+1, 2, "..."+app.name[-57:], curses.A_BOLD) + stdscr.addstr(3*i+1, 65, (app.user+"@"+app.machine)[:barsize]) + stdscr.addstr(3*i+2, 2, app.status[cur_x_pos:cur_x_pos+57]) + + stdscr.addch(3*i+2, 65, "[", curses.A_BOLD) + stdscr.addch(3*i+2, cols-2, "]", curses.A_BOLD) + # PROGRESS BAR + stdscr.attrset(curses.color_pair(3) | curses.A_BOLD) + stdscr.hline(3*i+2, 66, "|", barsize) + stdscr.attrset(curses.color_pair(2) | curses.A_BOLD) + stdscr.hline(3*i+2, 66, "|", int((app.progress/100.0)*barsize)) + stdscr.attrset(curses.color_pair(1) | curses.A_BOLD) + stdscr.addstr(3*i+2, 60, str(app.progress)+"%", curses.A_BOLD) + stdscr.hline(3*(i+1),1,curses.ACS_HLINE, cols-2) + + stdscr.box() + try: + cmd = stdscr.getch() + except: + pass + if cmd == ord('q'): + exit = True + elif cmd == curses.KEY_DOWN: + if cur_pos < len(apps)-numboxes: + cur_pos += 1 + elif cmd == curses.KEY_UP: + if cur_pos > 0: + cur_pos -= 1 + elif cmd == curses.KEY_RIGHT: + cur_x_pos += 10 + elif cmd == curses.KEY_LEFT: + if cur_x_pos > 0: + cur_x_pos -= 10 + stdscr.refresh() + +curses.wrapper(main) -- 2.34.1