~clicompanion-devs/clicompanion/clicomp-glade

« back to all changes in this revision

Viewing changes to comps.drafts.builds/test.py

  • Committer: duanedesign
  • Date: 2010-04-04 05:50:48 UTC
  • Revision ID: duanedesign@gmail.com-20100404055048-hqptxm1na56lawe6
"added scroling, 56 command capacity"

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# psax.py; illustration of curses library
 
3
# runs the shell command ps ax and saves the last lines of its output,
 
4
# as many as the window will fit; allows the user to move up and down
 
5
# within the window, killing those processes
 
6
# run line:
 
7
#python psax.py
 
8
# user commands:
 
9
''''u':
 
10
move highlight up a line
 
11
'd':
 
12
move highlight down a line
 
13
'k':
 
14
kill process in currently highlighted line
 
15
'r':
 
16
re-run 'ps ax' for update
 
17
'q':
 
18
quit'''
 
19
# possible extensions: allowing scrolling, so that the user could go
 
20
# through all the ps ax output; allow wraparound for long lines; ask
 
21
# user to confirm before killing a process
 
22
 
 
23
import curses, os, sys, traceback
 
24
 
 
25
# global variables
 
26
class gb:
 
27
    scrn = None # will point to Curses window object
 
28
    cmdoutlines = [] # output of 'ps ax' (including the lines we don't
 
29
 
 
30
# use, for possible future extension)
 
31
 
 
32
winrow = None # current row position in screen
 
33
startrow = None # index of first row in cmdoutlines to be displayed
 
34
def runpsax():
 
35
    p = os.popen('ps ax','r')
 
36
    gb.cmdoutlines = []
 
37
    row = 0
 
38
 
 
39
    for ln in p:
 
40
    # don't allow line wraparound, so truncate long lines
 
41
        ln = ln[:curses.COLS]
 
42
 
 
43
# remove EOLN if it is still there
 
44
    if ln[-1] == '\n': ln = ln[:-1]
 
45
 
 
46
    gb.cmdoutlines.append(ln)
 
47
    p.close()
 
48
 
 
49
# display last part of command output (as much as fits in screen)
 
50
def showlastpart():
 
51
    # clear screen
 
52
    gb.scrn.clear()
 
53
    # prepare to paint the (last part of the) 'ps ax' output on the screen
 
54
    gb.winrow = 0
 
55
    ncmdlines = len(gb.cmdoutlines)
 
56
    # two cases, depending on whether there is more output than screen rows
 
57
    if ncmdlines <= curses.LINES:
 
58
        gb.startrow = 0
 
59
        nwinlines = ncmdlines
 
60
        gb.startrow = ncmdlines - curses.LINES - 1
 
61
        nwinlines = curses.LINES
 
62
        lastrow = gb.startrow + nwinlines - 1
 
63
 
 
64
# now paint the rows
 
65
    for ln in gb.cmdoutlines[gb.startrow:lastrow]:
 
66
        gb.scrn.addstr(gb.winrow,0,ln)
 
67
 
 
68
 
 
69
 
 
70
        gb.winrow += 1
 
71
        # last line highlighted
 
72
        gb.scrn.addstr(gb.winrow,0,gb.cmdoutlines[lastrow],curses.A_BOLD)
 
73
        gb.scrn.refresh()
 
74
 
 
75
# move highlight up/down one line
 
76
def updown(inc):
 
77
    tmp = gb.winrow + inc
 
78
# ignore attempts to go off the edge of the screen
 
79
    if tmp >= 0 and tmp < curses.LINES:
 
80
# unhighlight the current line by rewriting it in default attributes
 
81
        gb.scrn.addstr(gb.winrow,0,gb.cmdoutlines[gb.startrow+gb.winrow])
 
82
# highlight the previous/next line
 
83
        gb.winrow = tmp
 
84
        ln = gb.cmdoutlines[gb.startrow+gb.winrow]
 
85
        gb.scrn.addstr(gb.winrow,0,ln,curses.A_BOLD)
 
86
        gb.scrn.refresh()
 
87
 
 
88
# kill the highlighted process
 
89
def kill():
 
90
    ln = gb.cmdoutlines[gb.startrow+gb.winrow]
 
91
    pid = int(ln.split()[0])
 
92
    os.kill(pid,9)
 
93
 
 
94
# run/re-run 'ps ax'
 
95
    def rerun():
 
96
        runpsax()
 
97
        showlastpart()
 
98
 
 
99
def main():
 
100
# window setup
 
101
    gb.scrn = curses.initscr()
 
102
    curses.noecho()
 
103
    curses.cbreak()
 
104
    # rpdb.set_trace() (I used RPDB for debugging)
 
105
    # run 'ps ax' and process the output
 
106
    gb.psax = runpsax()
 
107
    # display in the window
 
108
    showlastpart()
 
109
    # user command loop
 
110
    while True:
 
111
        # get user command
 
112
        c = gb.scrn.getch()
 
113
        c = chr(c)
 
114
        if c == 'u': updown(-1)
 
115
        elif c == 'd': updown(1)
 
116
        elif c == 'r': rerun()
 
117
        elif c == 'k': kill()
 
118
        else: break
 
119
 
 
120
    restorescreen()
 
121
 
 
122
    def restorescreen():
 
123
        curses.nocbreak()
 
124
        curses.echo()
 
125
        curses.endwin()
 
126
 
 
127
if __name__ =='__main__':
 
128
    main()
 
129
    restorescreen()
 
130
 
 
131
    # print error message re exception
 
132
    traceback.print_exc()
 
133