]>
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; version 2 of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 from Blinkenlights
import BlinkenBase
19 from UIBase
import UIBase
20 from threading
import *
21 import thread
, time
, sys
, os
22 from offlineimap
import version
, threadutil
23 from offlineimap
.threadutil
import MultiLock
25 import curses
, curses
.panel
, curses
.textpad
, curses
.wrapper
27 acctkeys
= '1234567890abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=;/.,'
31 self
.pairlock
= Lock()
32 self
.iolock
= MultiLock()
36 self
.pairlock
.acquire()
38 self
.pairs
= {self
._getpairindex
(curses
.COLOR_WHITE
,
39 curses
.COLOR_BLACK
): 0}
42 self
.pairlock
.release()
50 def locked(self
, target
, *args
, **kwargs
):
51 """Perform an operation with full locking."""
54 apply(target
, args
, kwargs
)
60 curses
.panel
.update_panels()
62 self
.locked(lockedstuff
)
65 return hasattr(self
, 'stdscr')
67 def _getpairindex(self
, fg
, bg
):
68 return '%d/%d' % (fg
,bg
)
70 def getpair(self
, fg
, bg
):
71 if not self
.has_color
:
73 pindex
= self
._getpairindex
(fg
, bg
)
74 self
.pairlock
.acquire()
76 if self
.pairs
.has_key(pindex
):
77 return curses
.color_pair(self
.pairs
[pindex
])
79 self
.pairs
[pindex
] = self
.nextpair
80 curses
.init_pair(self
.nextpair
, fg
, bg
)
82 return curses
.color_pair(self
.nextpair
- 1)
84 self
.pairlock
.release()
87 self
.stdscr
= curses
.initscr()
93 self
.has_color
= curses
.has_colors()
99 self
.oldcursor
= curses
.curs_set(0)
104 self
.stdscr
.refresh()
105 (self
.height
, self
.width
) = self
.stdscr
.getmaxyx()
109 if not hasattr(self
, 'stdscr'):
111 #self.stdscr.addstr(self.height - 1, 0, "\n",
112 # self.getpair(curses.COLOR_WHITE,
113 # curses.COLOR_BLACK))
114 if self
.oldcursor
!= None:
115 curses
.curs_set(self
.oldcursor
)
116 self
.stdscr
.refresh()
117 self
.stdscr
.keypad(0)
127 class CursesAccountFrame
:
128 def __init__(s
, master
, accountname
):
131 s
.accountname
= accountname
133 def drawleadstr(s
, secs
= None):
135 acctstr
= '%s: [active] %13.13s: ' % (s
.key
, s
.accountname
)
137 acctstr
= '%s: [%3d:%02d] %13.13s: ' % (s
.key
,
138 secs
/ 60, secs
% 60,
140 s
.c
.locked(s
.window
.addstr
, 0, 0, acctstr
)
141 s
.location
= len(acctstr
)
143 def setwindow(s
, window
, key
):
147 for child
in s
.children
:
148 child
.update(window
, 0, s
.location
)
151 def getnewthreadframe(s
):
152 tf
= CursesThreadFrame(s
.c
, s
.window
, 0, s
.location
)
154 s
.children
.append(tf
)
157 def startsleep(s
, sleepsecs
):
160 def sleeping(s
, sleepsecs
, remainingsecs
):
164 s
.drawleadstr(remainingsecs
)
168 time
.sleep(sleepsecs
)
176 return s
.sleeping_abort
181 class CursesThreadFrame
:
182 def __init__(s
, master
, window
, y
, x
):
183 """master should be a CursesUtil object."""
189 bg
= curses
.COLOR_BLACK
190 s
.colormap
= {'black': s
.c
.getpair(curses
.COLOR_BLACK
, bg
),
191 'gray': s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
192 'white': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
193 'blue': s
.c
.getpair(curses
.COLOR_BLUE
, bg
),
194 'red': s
.c
.getpair(curses
.COLOR_RED
, bg
),
195 'purple': s
.c
.getpair(curses
.COLOR_MAGENTA
, bg
),
196 'cyan': s
.c
.getpair(curses
.COLOR_CYAN
, bg
),
197 'green': s
.c
.getpair(curses
.COLOR_GREEN
, bg
),
198 'orange': s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
199 'yellow': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
200 'pink': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_RED
, bg
)}
204 def setcolor(self
, color
):
205 self
.color
= self
.colormap
[color
]
206 self
.colorname
= color
211 if self
.getcolor() == 'black':
212 self
.window
.addstr(self
.y
, self
.x
, ' ', self
.color
)
214 self
.window
.addstr(self
.y
, self
.x
, '.', self
.color
)
215 self
.c
.stdscr
.move(self
.c
.height
- 1, self
.c
.width
- 1)
216 self
.window
.refresh()
217 self
.c
.locked(lockedstuff
)
220 return self
.colorname
222 def getcolorpair(self
):
225 def update(self
, window
, y
, x
):
231 def setthread(self
, newthread
):
232 self
.setcolor('black')
234 # self.setcolor('gray')
236 # self.setcolor('black')
239 def __init__(s
, util
):
244 s
.statuslock
= Lock()
249 s
.thread
= threadutil
.ExitNotifyThread(target
= s
.bgreaderloop
,
250 name
= "InputHandler loop")
251 s
.thread
.setDaemon(1)
256 s
.statuslock
.acquire()
257 if s
.lockheld
or s
.bgchar
== None:
258 s
.statuslock
.release()
261 s
.statuslock
.release()
262 ch
= s
.c
.stdscr
.getch()
263 s
.statuslock
.acquire()
265 if s
.lockheld
or s
.bgchar
== None:
270 s
.statuslock
.release()
272 def set_bgchar(s
, callback
):
273 """Sets a "background" character handler. If a key is pressed
274 while not doing anything else, it will be passed to this handler.
276 callback is a function taking a single arg -- the char pressed.
278 If callback is None, clears the request."""
279 s
.statuslock
.acquire()
280 oldhandler
= s
.bgchar
281 newhandler
= callback
284 if oldhandler
and not newhandler
:
286 if newhandler
and not oldhandler
:
289 s
.statuslock
.release()
291 def input_acquire(s
):
292 """Call this method when you want exclusive input control.
293 Make sure to call input_release afterwards!
296 s
.inputlock
.acquire()
297 s
.statuslock
.acquire()
299 s
.statuslock
.release()
301 def input_release(s
):
302 """Call this method when you are done getting input."""
303 s
.statuslock
.acquire()
305 s
.statuslock
.release()
306 s
.inputlock
.release()
309 class Blinkenlights(BlinkenBase
, UIBase
):
315 BlinkenBase
.init_banner(s
)
317 s
.inputhandler
= InputHandler(s
.c
)
318 s
.gettf().setcolor('red')
319 s
._msg
(version
.banner
)
320 s
.inputhandler
.set_bgchar(s
.keypress
)
323 # Not a terminal? Can't use curses.
324 if not sys
.stdout
.isatty() and sys
.stdin
.isatty():
327 # No TERM specified? Can't use curses.
329 if not len(os
.environ
['TERM']):
333 # ncurses doesn't want to start? Can't use curses.
334 # This test is nasty because initscr() actually EXITS on error.
340 return not os
.WEXITSTATUS(os
.waitpid(pid
, 0)[1])
345 # If we didn't die by here, indicate success.
348 def keypress(s
, key
):
357 index
= acctkeys
.index(chr(key
))
359 # Key not a valid one: exit.
362 if index
> len(s
.hotkeys
):
363 # Not in our list of valid hotkeys.
366 # Trying to end sleep somewhere.
368 s
.getaccountframe(s
.hotkeys
[index
]).syncnow()
370 def getpass(s
, accountname
, config
, errmsg
= None):
371 s
.inputhandler
.input_acquire()
373 # See comment on _msg for info on why both locks are obtained.
378 s
.gettf().setcolor('white')
379 s
._addline
(" *** Input Required", s
.gettf().getcolorpair())
380 s
._addline
(" *** Please enter password for account %s: " % accountname
,
381 s
.gettf().getcolorpair())
382 s
.logwindow
.refresh()
383 password
= s
.logwindow
.getstr()
387 s
.inputhandler
.input_release()
393 s
.bannerwindow
= curses
.newwin(1, s
.c
.width
, 0, 0)
394 s
.setupwindow_drawbanner()
395 s
.logheight
= s
.c
.height
- 1 - len(s
.af
.keys())
396 s
.logwindow
= curses
.newwin(s
.logheight
, s
.c
.width
, 1, 0)
398 s
.logwindow
.scrollok(1)
399 s
.logwindow
.move(s
.logheight
- 1, 0)
400 s
.setupwindow_drawlog()
401 accounts
= s
.af
.keys()
408 for account
in accounts
:
409 accountwindow
= curses
.newwin(1, s
.c
.width
, pos
, 0)
410 s
.af
[account
].setwindow(accountwindow
, acctkeys
[index
])
411 s
.hotkeys
.append(account
)
419 def setupwindow_drawbanner(s
):
421 color
= s
.c
.getpair(curses
.COLOR_WHITE
, curses
.COLOR_BLUE
) | \
424 color
= curses
.A_REVERSE
425 s
.bannerwindow
.bkgd(' ', color
) # Fill background with that color
426 s
.bannerwindow
.addstr("%s %s" % (version
.productname
,
428 s
.bannerwindow
.addstr(0, s
.bannerwindow
.getmaxyx()[1] - len(version
.copyright
) - 1,
431 s
.bannerwindow
.noutrefresh()
433 def setupwindow_drawlog(s
):
435 color
= s
.c
.getpair(curses
.COLOR_WHITE
, curses
.COLOR_BLACK
)
437 color
= curses
.A_NORMAL
438 s
.logwindow
.bkgd(' ', color
)
439 for line
, color
in s
.text
:
440 s
.logwindow
.addstr("\n" + line
, color
)
441 s
.logwindow
.noutrefresh()
443 def getaccountframe(s
, accountname
= None):
444 if accountname
== None:
445 accountname
= s
.getthreadaccount()
448 if accountname
in s
.af
:
449 return s
.af
[accountname
]
452 s
.af
[accountname
] = CursesAccountFrame(s
.c
, accountname
)
461 return s
.af
[accountname
]
464 def _msg(s
, msg
, color
= None):
466 for thisline
in msg
.split("\n"):
470 # We must acquire both locks. Otherwise, deadlock can result.
471 # This can happen if one thread calls _msg (locking curses, then
472 # tf) and another tries to set the color (locking tf, then curses)
474 # By locking both up-front here, in this order, we prevent deadlock.
479 if not s
.c
.isactive():
480 # For dumping out exceptions and stuff.
484 s
.gettf().setcolor(color
)
485 s
._addline
(msg
, s
.gettf().getcolorpair())
486 s
.logwindow
.refresh()
491 def _addline(s
, msg
, color
):
494 s
.logwindow
.addstr("\n" + msg
, color
)
495 s
.text
.append((msg
, color
))
496 while len(s
.text
) > s
.logheight
:
501 def terminate(s
, exitstatus
= 0):
503 UIBase
.terminate(s
, exitstatus
)
505 def threadException(s
, thread
):
507 UIBase
.threadException(s
, thread
)
509 def mainException(s
):
511 UIBase
.mainException(s
)
513 def sleep(s
, sleepsecs
):
514 s
.gettf().setcolor('red')
515 s
._msg
("Next sync in %d:%02d" % (sleepsecs
/ 60, sleepsecs
% 60))
516 BlinkenBase
.sleep(s
, sleepsecs
)
518 if __name__
== '__main__':
519 x
= Blinkenlights(None)
524 fgs
= {'black': curses
.COLOR_BLACK
, 'red': curses
.COLOR_RED
,
525 'green': curses
.COLOR_GREEN
, 'yellow': curses
.COLOR_YELLOW
,
526 'blue': curses
.COLOR_BLUE
, 'magenta': curses
.COLOR_MAGENTA
,
527 'cyan': curses
.COLOR_CYAN
, 'white': curses
.COLOR_WHITE
}
530 win1
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, 0)
531 win1
.addstr("Black/normal\n")
532 for name
, fg
in fgs
.items():
533 win1
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
))
534 win2
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1])
535 win2
.addstr("Blue/normal\n")
536 for name
, fg
in fgs
.items():
537 win2
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
))
538 win3
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] +
540 win3
.addstr("Black/bright\n")
541 for name
, fg
in fgs
.items():
542 win3
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
) | \
544 win4
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] * 3)
545 win4
.addstr("Blue/bright\n")
546 for name
, fg
in fgs
.items():
547 win4
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
) | \