]>
code.delx.au - offlineimap/blob - 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 from Blinkenlights
import BlinkenBase
20 from UIBase
import UIBase
21 from threading
import *
22 import thread
, time
, sys
, os
, signal
, time
23 from offlineimap
import version
, threadutil
24 from offlineimap
.threadutil
import MultiLock
26 import curses
, curses
.panel
, curses
.textpad
, curses
.wrapper
28 acctkeys
= '1234567890abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=;/.,'
32 self
.pairlock
= Lock()
33 self
.iolock
= MultiLock()
37 self
.pairlock
.acquire()
39 self
.pairs
= {self
._getpairindex
(curses
.COLOR_WHITE
,
40 curses
.COLOR_BLACK
): 0}
43 self
.pairlock
.release()
51 def locked(self
, target
, *args
, **kwargs
):
52 """Perform an operation with full locking."""
55 apply(target
, args
, kwargs
)
61 curses
.panel
.update_panels()
63 self
.locked(lockedstuff
)
66 return hasattr(self
, 'stdscr')
68 def _getpairindex(self
, fg
, bg
):
69 return '%d/%d' % (fg
,bg
)
71 def getpair(self
, fg
, bg
):
72 if not self
.has_color
:
74 pindex
= self
._getpairindex
(fg
, bg
)
75 self
.pairlock
.acquire()
77 if self
.pairs
.has_key(pindex
):
78 return curses
.color_pair(self
.pairs
[pindex
])
80 self
.pairs
[pindex
] = self
.nextpair
81 curses
.init_pair(self
.nextpair
, fg
, bg
)
83 return curses
.color_pair(self
.nextpair
- 1)
85 self
.pairlock
.release()
88 self
.stdscr
= curses
.initscr()
94 self
.has_color
= curses
.has_colors()
100 self
.oldcursor
= curses
.curs_set(0)
105 self
.stdscr
.refresh()
106 (self
.height
, self
.width
) = self
.stdscr
.getmaxyx()
110 if not hasattr(self
, 'stdscr'):
112 #self.stdscr.addstr(self.height - 1, 0, "\n",
113 # self.getpair(curses.COLOR_WHITE,
114 # curses.COLOR_BLACK))
115 if self
.oldcursor
!= None:
116 curses
.curs_set(self
.oldcursor
)
117 self
.stdscr
.refresh()
118 self
.stdscr
.keypad(0)
128 class CursesAccountFrame
:
129 def __init__(s
, master
, accountname
):
132 s
.accountname
= accountname
134 def drawleadstr(s
, secs
= None):
136 acctstr
= '%s: [active] %13.13s: ' % (s
.key
, s
.accountname
)
138 acctstr
= '%s: [%3d:%02d] %13.13s: ' % (s
.key
,
139 secs
/ 60, secs
% 60,
141 s
.c
.locked(s
.window
.addstr
, 0, 0, acctstr
)
142 s
.location
= len(acctstr
)
144 def setwindow(s
, window
, key
):
148 for child
in s
.children
:
149 child
.update(window
, 0, s
.location
)
152 def getnewthreadframe(s
):
153 tf
= CursesThreadFrame(s
.c
, s
.window
, 0, s
.location
)
155 s
.children
.append(tf
)
158 def startsleep(s
, sleepsecs
):
161 def sleeping(s
, sleepsecs
, remainingsecs
):
165 s
.drawleadstr(remainingsecs
)
169 time
.sleep(sleepsecs
)
177 return s
.sleeping_abort
182 class CursesThreadFrame
:
183 def __init__(s
, master
, window
, y
, x
):
184 """master should be a CursesUtil object."""
190 bg
= curses
.COLOR_BLACK
191 s
.colormap
= {'black': s
.c
.getpair(curses
.COLOR_BLACK
, bg
),
192 'gray': s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
193 'white': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_WHITE
, bg
),
194 'blue': s
.c
.getpair(curses
.COLOR_BLUE
, bg
),
195 'red': s
.c
.getpair(curses
.COLOR_RED
, bg
),
196 'purple': s
.c
.getpair(curses
.COLOR_MAGENTA
, bg
),
197 'cyan': s
.c
.getpair(curses
.COLOR_CYAN
, bg
),
198 'green': s
.c
.getpair(curses
.COLOR_GREEN
, bg
),
199 'orange': s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
200 'yellow': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_YELLOW
, bg
),
201 'pink': curses
.A_BOLD | s
.c
.getpair(curses
.COLOR_RED
, bg
)}
205 def setcolor(self
, color
):
206 self
.color
= self
.colormap
[color
]
207 self
.colorname
= color
212 if self
.getcolor() == 'black':
213 self
.window
.addstr(self
.y
, self
.x
, ' ', self
.color
)
215 self
.window
.addstr(self
.y
, self
.x
, '.', self
.color
)
216 self
.c
.stdscr
.move(self
.c
.height
- 1, self
.c
.width
- 1)
217 self
.window
.refresh()
218 self
.c
.locked(lockedstuff
)
221 return self
.colorname
223 def getcolorpair(self
):
226 def update(self
, window
, y
, x
):
232 def setthread(self
, newthread
):
233 self
.setcolor('black')
235 # self.setcolor('gray')
237 # self.setcolor('black')
240 def __init__(s
, util
):
245 s
.statuslock
= Lock()
250 s
.thread
= threadutil
.ExitNotifyThread(target
= s
.bgreaderloop
,
251 name
= "InputHandler loop")
252 s
.thread
.setDaemon(1)
257 s
.statuslock
.acquire()
258 if s
.lockheld
or s
.bgchar
== None:
259 s
.statuslock
.release()
262 s
.statuslock
.release()
263 ch
= s
.c
.stdscr
.getch()
264 s
.statuslock
.acquire()
266 if s
.lockheld
or s
.bgchar
== None:
271 s
.statuslock
.release()
273 def set_bgchar(s
, callback
):
274 """Sets a "background" character handler. If a key is pressed
275 while not doing anything else, it will be passed to this handler.
277 callback is a function taking a single arg -- the char pressed.
279 If callback is None, clears the request."""
280 s
.statuslock
.acquire()
281 oldhandler
= s
.bgchar
282 newhandler
= callback
285 if oldhandler
and not newhandler
:
287 if newhandler
and not oldhandler
:
290 s
.statuslock
.release()
292 def input_acquire(s
):
293 """Call this method when you want exclusive input control.
294 Make sure to call input_release afterwards!
297 s
.inputlock
.acquire()
298 s
.statuslock
.acquire()
300 s
.statuslock
.release()
302 def input_release(s
):
303 """Call this method when you are done getting input."""
304 s
.statuslock
.acquire()
306 s
.statuslock
.release()
307 s
.inputlock
.release()
310 class Blinkenlights(BlinkenBase
, UIBase
):
316 BlinkenBase
.init_banner(s
)
318 s
.inputhandler
= InputHandler(s
.c
)
319 s
.gettf().setcolor('red')
320 s
._msg
(version
.banner
)
321 s
.inputhandler
.set_bgchar(s
.keypress
)
322 signal
.signal(signal
.SIGWINCH
, s
.resizehandler
)
323 s
.resizelock
= Lock()
326 def resizehandler(s
, signum
, frame
):
329 def resizeterm(s
, dosleep
= 1):
330 if not s
.resizelock
.acquire(0):
333 signal
.signal(signal
.SIGWINCH
, signal
.SIG_IGN
)
343 s
.resizelock
.release()
344 signal
.signal(signal
.SIGWINCH
, s
.resizehandler
)
350 # Not a terminal? Can't use curses.
351 if not sys
.stdout
.isatty() and sys
.stdin
.isatty():
354 # No TERM specified? Can't use curses.
356 if not len(os
.environ
['TERM']):
360 # ncurses doesn't want to start? Can't use curses.
361 # This test is nasty because initscr() actually EXITS on error.
367 return not os
.WEXITSTATUS(os
.waitpid(pid
, 0)[1])
372 # If we didn't die by here, indicate success.
375 def keypress(s
, key
):
384 index
= acctkeys
.index(chr(key
))
386 # Key not a valid one: exit.
389 if index
>= len(s
.hotkeys
):
390 # Not in our list of valid hotkeys.
393 # Trying to end sleep somewhere.
395 s
.getaccountframe(s
.hotkeys
[index
]).syncnow()
397 def getpass(s
, accountname
, config
, errmsg
= None):
398 s
.inputhandler
.input_acquire()
400 # See comment on _msg for info on why both locks are obtained.
405 s
.gettf().setcolor('white')
406 s
._addline
(" *** Input Required", s
.gettf().getcolorpair())
407 s
._addline
(" *** Please enter password for account %s: " % accountname
,
408 s
.gettf().getcolorpair())
409 s
.logwindow
.refresh()
410 password
= s
.logwindow
.getstr()
414 s
.inputhandler
.input_release()
420 s
.bannerwindow
= curses
.newwin(1, s
.c
.width
, 0, 0)
421 s
.setupwindow_drawbanner()
422 s
.logheight
= s
.c
.height
- 1 - len(s
.af
.keys())
423 s
.logwindow
= curses
.newwin(s
.logheight
, s
.c
.width
, 1, 0)
425 s
.logwindow
.scrollok(1)
426 s
.logwindow
.move(s
.logheight
- 1, 0)
427 s
.setupwindow_drawlog()
428 accounts
= s
.af
.keys()
435 for account
in accounts
:
436 accountwindow
= curses
.newwin(1, s
.c
.width
, pos
, 0)
437 s
.af
[account
].setwindow(accountwindow
, acctkeys
[index
])
438 s
.hotkeys
.append(account
)
446 def setupwindow_drawbanner(s
):
448 color
= s
.c
.getpair(curses
.COLOR_WHITE
, curses
.COLOR_BLUE
) | \
451 color
= curses
.A_REVERSE
452 s
.bannerwindow
.bkgd(' ', color
) # Fill background with that color
453 s
.bannerwindow
.addstr("%s %s" % (version
.productname
,
455 s
.bannerwindow
.addstr(0, s
.bannerwindow
.getmaxyx()[1] - len(version
.copyright
) - 1,
458 s
.bannerwindow
.noutrefresh()
460 def setupwindow_drawlog(s
):
462 color
= s
.c
.getpair(curses
.COLOR_WHITE
, curses
.COLOR_BLACK
)
464 color
= curses
.A_NORMAL
465 s
.logwindow
.bkgd(' ', color
)
466 for line
, color
in s
.text
:
467 s
.logwindow
.addstr("\n" + line
, color
)
468 s
.logwindow
.noutrefresh()
470 def getaccountframe(s
, accountname
= None):
471 if accountname
== None:
472 accountname
= s
.getthreadaccount()
475 if accountname
in s
.af
:
476 return s
.af
[accountname
]
479 s
.af
[accountname
] = CursesAccountFrame(s
.c
, accountname
)
488 return s
.af
[accountname
]
491 def _display(s
, msg
, color
= None):
493 for thisline
in msg
.split("\n"):
497 # We must acquire both locks. Otherwise, deadlock can result.
498 # This can happen if one thread calls _msg (locking curses, then
499 # tf) and another tries to set the color (locking tf, then curses)
501 # By locking both up-front here, in this order, we prevent deadlock.
506 if not s
.c
.isactive():
507 # For dumping out exceptions and stuff.
511 s
.gettf().setcolor(color
)
512 s
._addline
(msg
, s
.gettf().getcolorpair())
513 s
.logwindow
.refresh()
518 def _addline(s
, msg
, color
):
521 s
.logwindow
.addstr("\n" + msg
, color
)
522 s
.text
.append((msg
, color
))
523 while len(s
.text
) > s
.logheight
:
528 def terminate(s
, exitstatus
= 0, errortitle
= None, errormsg
= None):
530 UIBase
.terminate(s
, exitstatus
= exitstatus
, errortitle
= errortitle
, errormsg
= errormsg
)
532 def threadException(s
, thread
):
534 UIBase
.threadException(s
, thread
)
536 def mainException(s
):
538 UIBase
.mainException(s
)
540 def sleep(s
, sleepsecs
):
541 s
.gettf().setcolor('red')
542 s
._msg
("Next sync in %d:%02d" % (sleepsecs
/ 60, sleepsecs
% 60))
543 BlinkenBase
.sleep(s
, sleepsecs
)
545 if __name__
== '__main__':
546 x
= Blinkenlights(None)
551 fgs
= {'black': curses
.COLOR_BLACK
, 'red': curses
.COLOR_RED
,
552 'green': curses
.COLOR_GREEN
, 'yellow': curses
.COLOR_YELLOW
,
553 'blue': curses
.COLOR_BLUE
, 'magenta': curses
.COLOR_MAGENTA
,
554 'cyan': curses
.COLOR_CYAN
, 'white': curses
.COLOR_WHITE
}
557 win1
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, 0)
558 win1
.addstr("Black/normal\n")
559 for name
, fg
in fgs
.items():
560 win1
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
))
561 win2
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1])
562 win2
.addstr("Blue/normal\n")
563 for name
, fg
in fgs
.items():
564 win2
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
))
565 win3
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] +
567 win3
.addstr("Black/bright\n")
568 for name
, fg
in fgs
.items():
569 win3
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLACK
) | \
571 win4
= curses
.newwin(x
.height
, x
.width
/ 4 - 1, 0, win1
.getmaxyx()[1] * 3)
572 win4
.addstr("Blue/bright\n")
573 for name
, fg
in fgs
.items():
574 win4
.addstr("%s\n" % name
, x
.getpair(fg
, curses
.COLOR_BLUE
) | \