]>
code.delx.au - offlineimap/blob - offlineimap/head/offlineimap/ui/Curses.py
1 # Curses-based interfaces
2 # Copyright (C) 2003 John Goerzen
3 # <jgoerzen@complete.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from Blinkenlights
import BlinkenBase
20 from UIBase
import UIBase
21 from threading
import *
23 from offlineimap
import version
, threadutil
24 from offlineimap
.threadutil
import MultiLock
26 import curses
, curses
.panel
, curses
.textpad
, curses
.wrapper
27 from debuglock
import DebuggingLock
31 self
.pairs
= {self
._getpairindex
(curses
.COLOR_WHITE
,
32 curses
.COLOR_BLACK
): 0}
35 self
.pairlock
= Lock()
36 self
.iolock
= MultiLock()
44 def locked(self
, target
, *args
, **kwargs
):
45 """Perform an operation with full locking."""
48 apply(target
, args
, kwargs
)
54 curses
.panel
.update_panels()
56 self
.locked(lockedstuff
)
59 return hasattr(self
, 'stdscr')
61 def _getpairindex(self
, fg
, bg
):
62 return '%d/%d' % (fg
,bg
)
64 def getpair(self
, fg
, bg
):
65 pindex
= self
._getpairindex
(fg
, bg
)
66 self
.pairlock
.acquire()
68 if self
.pairs
.has_key(pindex
):
69 return curses
.color_pair(self
.pairs
[pindex
])
71 self
.pairs
[pindex
] = self
.nextpair
72 curses
.init_pair(self
.nextpair
, fg
, bg
)
74 return curses
.color_pair(self
.nextpair
- 1)
76 self
.pairlock
.release()
79 self
.stdscr
= curses
.initscr()
85 self
.has_color
= curses
.has_colors()
91 (self
.height
, self
.width
) = self
.stdscr
.getmaxyx()
94 if not hasattr(self
, 'stdscr'):
96 #self.stdscr.addstr(self.height - 1, 0, "\n",
97 # self.getpair(curses.COLOR_WHITE,
98 # curses.COLOR_BLACK))
100 self
.stdscr
.keypad(0)
110 class CursesAccountFrame
:
111 def __init__(s
, master
, accountname
):
114 s
.accountname
= accountname
116 def setwindow(s
, window
):
118 acctstr
= '%15.15s: ' % s
.accountname
119 s
.window
.addstr(0, 0, acctstr
)
120 s
.location
= len(acctstr
)
121 for child
in s
.children
:
122 child
.update(window
, 0, s
.location
)
125 def getnewthreadframe(s
):
126 tf
= CursesThreadFrame(s
.c
, s
.window
, 0, s
.location
)
128 s
.children
.append(tf
)
131 class CursesThreadFrame
:
132 def __init__(s
, master
, window
, y
, x
):
133 """master should be a CursesUtil object."""
139 bg
= curses
.COLOR_BLACK
140 s
.colormap
= {'black': s
.c
.getpair(curses
.COLOR_BLACK
, bg
),
141 'gray': s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
142 'white': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
143 'blue': s
.c
.getpair(curses
.COLOR_BLUE
, bg
),
144 'red': s
.c
.getpair(curses
.COLOR_RED
, bg
),
145 'purple': s
.c
.getpair(curses
.COLOR_MAGENTA
, bg
),
146 'cyan': s
.c
.getpair(curses
.COLOR_CYAN
, bg
),
147 'green': s
.c
.getpair(curses
.COLOR_GREEN
, bg
),
148 'orange': s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
149 'yellow': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
150 'pink': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_RED
, bg
)}
154 def setcolor(self
, color
):
155 self
.color
= self
.colormap
[color
]
160 self
.window
.addstr(self
.y
, self
.x
, '.', self
.color
)
161 self
.c
.stdscr
.move(self
.c
.height
- 1, self
.c
.width
- 1)
162 self
.window
.refresh()
163 self
.c
.locked(lockedstuff
)
168 def update(self
, window
, y
, x
):
174 def setthread(self
, newthread
):
175 self
.setcolor('black')
177 # self.setcolor('gray')
179 # self.setcolor('black')
182 def __init__(s
, util
):
185 s
.inputlock
= DebuggingLock('inputlock')
187 s
.statuslock
= DebuggingLock('statuslock')
192 s
.thread
= threadutil
.ExitNotifyThread(target
= s
.bgreaderloop
,
193 name
= "InputHandler loop")
194 s
.thread
.setDaemon(1)
199 s
.statuslock
.acquire()
200 if s
.lockheld
or s
.bgchar
== None:
201 s
.statuslock
.release()
204 s
.statuslock
.release()
205 ch
= s
.c
.stdscr
.getch()
206 s
.statuslock
.acquire()
208 if s
.lockheld
or s
.bgchar
== None:
213 s
.statuslock
.release()
215 def set_bgchar(s
, callback
):
216 """Sets a "background" character handler. If a key is pressed
217 while not doing anything else, it will be passed to this handler.
219 callback is a function taking a single arg -- the char pressed.
221 If callback is None, clears the request."""
222 s
.statuslock
.acquire()
223 oldhandler
= s
.bgchar
224 newhandler
= callback
227 if oldhandler
and not newhandler
:
229 if newhandler
and not oldhandler
:
232 s
.statuslock
.release()
234 def input_acquire(s
):
235 """Call this method when you want exclusive input control.
236 Make sure to call input_release afterwards!
239 s
.inputlock
.acquire()
240 s
.statuslock
.acquire()
242 s
.statuslock
.release()
244 def input_release(s
):
245 """Call this method when you are done getting input."""
246 s
.statuslock
.acquire()
248 s
.statuslock
.release()
249 s
.inputlock
.release()
252 class Blinkenlights(BlinkenBase
, UIBase
):
255 s
.aflock
= DebuggingLock('aflock')
258 BlinkenBase
.init_banner(s
)
260 s
.inputhandler
= InputHandler(s
.c
)
261 s
.gettf().setcolor('red')
262 s
._msg
(version
.banner
)
263 s
.inputhandler
.set_bgchar(s
.keypress
)
265 def keypress(s
, key
):
266 s
._msg
("Key pressed: " + str(key
))
268 def getpass(s
, accountname
, config
, errmsg
= None):
269 s
.inputhandler
.input_acquire()
271 # See comment on _msg for info on why both locks are obtained.
276 s
.gettf().setcolor('white')
277 s
._addline
(" *** Input Required", s
.gettf().getcolor())
278 s
._addline
(" *** Please enter password for account %s: " % accountname
,
279 s
.gettf().getcolor())
280 s
.logwindow
.refresh()
281 password
= s
.logwindow
.getstr()
285 s
.inputhandler
.input_release()
291 s
.bannerwindow
= curses
.newwin(1, s
.c
.width
, 0, 0)
292 s
.setupwindow_drawbanner()
293 s
.logheight
= s
.c
.height
- 1 - len(s
.af
.keys())
294 s
.logwindow
= curses
.newwin(s
.logheight
, s
.c
.width
, 1, 0)
296 s
.logwindow
.scrollok(1)
297 s
.setupwindow_drawlog()
298 accounts
= s
.af
.keys()
303 for account
in accounts
:
304 accountwindow
= curses
.newwin(1, s
.c
.width
, pos
, 0)
305 s
.af
[account
].setwindow(accountwindow
)
312 def setupwindow_drawbanner(s
):
313 s
.bannerwindow
.bkgd(' ', curses
.A_BOLD | \
314 s
.c
.getpair(curses
.COLOR_WHITE
,
316 s
.bannerwindow
.addstr("%s %s" % (version
.productname
,
318 s
.bannerwindow
.addstr(0, s
.bannerwindow
.getmaxyx()[1] - len(version
.copyright
) - 1,
321 s
.bannerwindow
.noutrefresh()
323 def setupwindow_drawlog(s
):
324 s
.logwindow
.bkgd(' ', s
.c
.getpair(curses
.COLOR_WHITE
, curses
.COLOR_BLACK
))
325 for line
, color
in s
.text
:
326 s
.logwindow
.addstr(line
+ "\n", color
)
327 s
.logwindow
.noutrefresh()
329 def getaccountframe(s
):
330 accountname
= s
.getthreadaccount()
333 if accountname
in s
.af
:
334 return s
.af
[accountname
]
337 s
.af
[accountname
] = CursesAccountFrame(s
.c
, accountname
)
346 return s
.af
[accountname
]
349 def _msg(s
, msg
, color
= None):
351 for thisline
in msg
.split("\n"):
355 # We must acquire both locks. Otherwise, deadlock can result.
356 # This can happen if one thread calls _msg (locking curses, then
357 # tf) and another tries to set the color (locking tf, then curses)
359 # By locking both up-front here, in this order, we prevent deadlock.
364 if not s
.c
.isactive():
365 # For dumping out exceptions and stuff.
369 s
.gettf().setcolor(color
)
370 s
._addline
(msg
, s
.gettf().getcolor())
371 s
.logwindow
.refresh()
376 def _addline(s
, msg
, color
):
379 s
.logwindow
.addstr(msg
+ "\n", color
)
380 s
.text
.append((msg
, color
))
381 while len(s
.text
) > s
.logheight
:
386 def terminate(s
, exitstatus
= 0):
388 UIBase
.terminate(s
, exitstatus
)
390 def threadException(s
, thread
):
392 UIBase
.threadException(s
, thread
)
394 def mainException(s
):
396 UIBase
.mainException(s
)
398 if __name__
== '__main__':
399 x
= Blinkenlights(None)
404 fgs
= {'black': curses
.COLOR_BLACK
, 'red': curses
.COLOR_RED
,
405 'green': curses
.COLOR_GREEN
, 'yellow': curses
.COLOR_YELLOW
,
406 'blue': curses
.COLOR_BLUE
, 'magenta': curses
.COLOR_MAGENTA
,
407 'cyan': curses
.COLOR_CYAN
, 'white': curses
.COLOR_WHITE
}
410 win1
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, 0)
411 win1
.addstr("Black/normal\n")
412 for name
, fg
in fgs
.items():
413 win1
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
))
414 win2
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1])
415 win2
.addstr("Blue/normal\n")
416 for name
, fg
in fgs
.items():
417 win2
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
))
418 win3
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] +
420 win3
.addstr("Black/bright\n")
421 for name
, fg
in fgs
.items():
422 win3
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
) | \
424 win4
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] * 3)
425 win4
.addstr("Blue/bright\n")
426 for name
, fg
in fgs
.items():
427 win4
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
) | \