2003-01-05 05:51:17 +01:00
|
|
|
# Curses-based interfaces
|
2016-06-28 23:58:03 +02:00
|
|
|
# Copyright (C) 2003-2016 John Goerzen & contributors.
|
2003-01-05 05:51:17 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2003-01-05 05:51:17 +01:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-01-05 05:51:17 +01:00
|
|
|
|
2011-11-03 12:25:55 +01:00
|
|
|
from threading import RLock, currentThread, Lock, Event
|
2011-10-26 16:47:21 +02:00
|
|
|
from collections import deque
|
2010-12-22 12:35:41 +01:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
2011-05-11 18:25:13 +02:00
|
|
|
import curses
|
2011-10-26 16:47:21 +02:00
|
|
|
import logging
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
from offlineimap.ui.UIBase import UIBase
|
|
|
|
from offlineimap.threadutil import ExitNotifyThread
|
2010-12-22 12:35:41 +01:00
|
|
|
import offlineimap
|
2003-01-05 05:51:17 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
|
2015-01-01 21:41:11 +01:00
|
|
|
class CursesUtil:
|
2011-10-26 16:47:21 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# iolock protects access to the
|
|
|
|
self.iolock = RLock()
|
|
|
|
self.tframe_lock = RLock()
|
2015-01-14 22:58:25 +01:00
|
|
|
# tframe_lock protects the self.threadframes manipulation to
|
|
|
|
# only happen from 1 thread.
|
2011-10-26 16:47:21 +02:00
|
|
|
self.colormap = {}
|
|
|
|
"""dict, translating color string to curses color pair number"""
|
|
|
|
|
|
|
|
def curses_colorpair(self, col_name):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return the curses color pair, that corresponds to the color."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
return curses.color_pair(self.colormap[col_name])
|
|
|
|
|
|
|
|
def init_colorpairs(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Initialize the curses color pairs available."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
# set special colors 'gray' and 'banner'
|
|
|
|
self.colormap['white'] = 0 #hardcoded by curses
|
|
|
|
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
|
|
|
|
self.colormap['banner'] = 1 # color 'banner' for bannerwin
|
|
|
|
|
|
|
|
bcol = curses.COLOR_BLACK
|
|
|
|
colors = ( # name, color, bold?
|
|
|
|
('black', curses.COLOR_BLACK, False),
|
|
|
|
('blue', curses.COLOR_BLUE,False),
|
|
|
|
('red', curses.COLOR_RED, False),
|
|
|
|
('purple', curses.COLOR_MAGENTA, False),
|
|
|
|
('cyan', curses.COLOR_CYAN, False),
|
|
|
|
('green', curses.COLOR_GREEN, False),
|
|
|
|
('orange', curses.COLOR_YELLOW, False))
|
|
|
|
#set the rest of all colors starting at pair 2
|
|
|
|
i = 1
|
|
|
|
for name, fcol, bold in colors:
|
|
|
|
i += 1
|
|
|
|
self.colormap[name] = i
|
|
|
|
curses.init_pair(i, fcol, bcol)
|
|
|
|
|
|
|
|
def lock(self, block=True):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Locks the Curses ui thread.
|
2011-01-25 13:24:52 +01:00
|
|
|
|
|
|
|
Can be invoked multiple times from the owning thread. Invoking
|
|
|
|
from a non-owning thread blocks and waits until it has been
|
|
|
|
unlocked by the owning thread."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
return self.iolock.acquire(block)
|
2003-01-06 22:58:29 +01:00
|
|
|
|
|
|
|
def unlock(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Unlocks the Curses ui thread.
|
2011-01-25 13:24:52 +01:00
|
|
|
|
|
|
|
Decrease the lock counter by one and unlock the ui thread if the
|
|
|
|
counter reaches 0. Only call this method when the calling
|
|
|
|
thread owns the lock. A RuntimeError is raised if this method is
|
|
|
|
called when the lock is unlocked."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
self.iolock.release()
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def exec_locked(self, target, *args, **kwargs):
|
2003-01-06 22:58:29 +01:00
|
|
|
"""Perform an operation with full locking."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
self.lock()
|
|
|
|
try:
|
2011-09-27 12:58:23 +02:00
|
|
|
target(*args, **kwargs)
|
2003-01-06 22:58:29 +01:00
|
|
|
finally:
|
|
|
|
self.unlock()
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
def lockedstuff():
|
|
|
|
curses.panel.update_panels()
|
|
|
|
curses.doupdate()
|
2011-10-26 16:47:21 +02:00
|
|
|
self.exec_locked(lockedstuff)
|
2003-01-05 05:51:17 +01:00
|
|
|
|
2003-01-05 08:51:35 +01:00
|
|
|
def isactive(self):
|
|
|
|
return hasattr(self, 'stdscr')
|
|
|
|
|
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
class CursesAccountFrame:
|
2011-10-26 16:47:21 +02:00
|
|
|
"""Notable instance variables:
|
|
|
|
|
2011-11-03 13:45:44 +01:00
|
|
|
- account: corresponding Account()
|
2011-10-26 16:47:21 +02:00
|
|
|
- children
|
|
|
|
- ui
|
|
|
|
- key
|
|
|
|
- window: curses window associated with an account
|
|
|
|
"""
|
|
|
|
|
2011-11-03 13:45:44 +01:00
|
|
|
def __init__(self, ui, account):
|
2011-11-03 14:21:25 +01:00
|
|
|
"""
|
|
|
|
:param account: An Account() or None (for eg SyncrunnerThread)"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.children = []
|
2011-11-03 14:21:25 +01:00
|
|
|
self.account = account if account else '*Control'
|
2011-10-26 16:47:21 +02:00
|
|
|
self.ui = ui
|
2011-11-02 17:03:40 +01:00
|
|
|
self.window = None
|
2015-01-14 22:58:25 +01:00
|
|
|
# Curses window associated with this acc.
|
2011-11-03 12:25:55 +01:00
|
|
|
self.acc_num = None
|
2015-01-14 22:58:25 +01:00
|
|
|
# Account number (& hotkey) associated with this acc.
|
2011-11-03 12:25:55 +01:00
|
|
|
self.location = 0
|
2015-01-14 22:58:25 +01:00
|
|
|
# length of the account prefix string
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2011-11-03 12:25:55 +01:00
|
|
|
def drawleadstr(self, secs = 0):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Draw the account status string.
|
2011-11-03 12:25:55 +01:00
|
|
|
|
|
|
|
secs tells us how long we are going to sleep."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
|
|
|
sleepstr = '%3d:%02d'% (secs // 60, secs % 60) if secs else 'active'
|
|
|
|
accstr = '%s: [%s] %12.12s: '% (self.acc_num, sleepstr, self.account)
|
2011-11-03 12:25:55 +01:00
|
|
|
|
|
|
|
self.ui.exec_locked(self.window.addstr, 0, 0, accstr)
|
|
|
|
self.location = len(accstr)
|
|
|
|
|
|
|
|
def setwindow(self, curses_win, acc_num):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Register an curses win and a hotkey as Account window.
|
2011-11-03 12:25:55 +01:00
|
|
|
|
|
|
|
:param curses_win: the curses window associated with an account
|
|
|
|
:param acc_num: int denoting the hotkey associated with this account."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.window = curses_win
|
2011-11-03 12:25:55 +01:00
|
|
|
self.acc_num = acc_num
|
2011-10-26 16:47:21 +02:00
|
|
|
self.drawleadstr()
|
2016-10-14 13:42:33 +02:00
|
|
|
self.ui.exec_locked(self.window.noutrefresh)
|
2011-10-26 16:47:21 +02:00
|
|
|
# Update the child ThreadFrames
|
|
|
|
for child in self.children:
|
|
|
|
child.update(curses_win, self.location, 0)
|
|
|
|
self.location += 1
|
|
|
|
|
|
|
|
def get_new_tframe(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Create a new ThreadFrame and append it to self.children.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
:returns: The new ThreadFrame"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
tf = CursesThreadFrame(self.ui, self.window, self.location, 0)
|
|
|
|
self.location += 1
|
|
|
|
self.children.append(tf)
|
2003-01-05 12:50:01 +01:00
|
|
|
return tf
|
|
|
|
|
2011-11-02 12:48:22 +01:00
|
|
|
def sleeping(self, sleepsecs, remainingsecs):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Show how long we are going to sleep and sleep.
|
2012-01-07 14:54:16 +01:00
|
|
|
|
|
|
|
:returns: Boolean, whether we want to abort the sleep"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-11-02 12:48:22 +01:00
|
|
|
self.drawleadstr(remainingsecs)
|
|
|
|
self.ui.exec_locked(self.window.refresh)
|
|
|
|
time.sleep(sleepsecs)
|
2012-01-07 14:54:16 +01:00
|
|
|
return self.account.get_abort_event()
|
2003-01-07 03:18:09 +01:00
|
|
|
|
2011-11-03 14:21:25 +01:00
|
|
|
def syncnow(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Request that we stop sleeping asap and continue to sync."""
|
|
|
|
|
2011-11-03 14:21:25 +01:00
|
|
|
# if this belongs to an Account (and not *Control), set the
|
|
|
|
# skipsleep pref
|
|
|
|
if isinstance(self.account, offlineimap.accounts.Account):
|
2015-01-14 22:58:25 +01:00
|
|
|
self.ui.info("Requested synchronization for acc: %s"% self.account)
|
|
|
|
self.account.config.set('Account %s'% self.account.name,
|
|
|
|
'skipsleep', '1')
|
2003-01-07 03:18:09 +01:00
|
|
|
|
2003-01-05 12:50:01 +01:00
|
|
|
class CursesThreadFrame:
|
2015-01-14 22:58:25 +01:00
|
|
|
"""curses_color: current color pair for logging."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def __init__(self, ui, acc_win, x, y):
|
|
|
|
"""
|
|
|
|
:param ui: is a Blinkenlights() instance
|
|
|
|
:param acc_win: curses Account window"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.ui = ui
|
|
|
|
self.window = acc_win
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.curses_color = curses.color_pair(0) #default color
|
|
|
|
|
|
|
|
def setcolor(self, color, modifier=0):
|
2011-11-02 17:03:40 +01:00
|
|
|
"""Draw the thread symbol '@' in the specified color
|
2015-01-14 22:58:25 +01:00
|
|
|
|
|
|
|
:param modifier: Curses modified, such as curses.A_BOLD
|
|
|
|
"""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.curses_color = modifier | self.ui.curses_colorpair(color)
|
2003-01-07 03:18:09 +01:00
|
|
|
self.colorname = color
|
2003-01-06 22:58:29 +01:00
|
|
|
self.display()
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2003-01-06 22:58:29 +01:00
|
|
|
def display(self):
|
2011-10-26 16:47:21 +02:00
|
|
|
def locked_display():
|
2011-11-02 17:03:40 +01:00
|
|
|
self.window.addch(self.y, self.x, '@', self.curses_color)
|
2003-01-06 05:54:59 +01:00
|
|
|
self.window.refresh()
|
2011-10-26 16:47:21 +02:00
|
|
|
# lock the curses IO while fudging stuff
|
|
|
|
self.ui.exec_locked(locked_display)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def update(self, acc_win, x, y):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Update the xy position of the '.' (and possibly the aframe)."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.window = acc_win
|
2003-01-06 00:07:58 +01:00
|
|
|
self.y = y
|
|
|
|
self.x = x
|
2003-01-06 22:58:29 +01:00
|
|
|
self.display()
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def std_color(self):
|
2003-01-06 22:58:29 +01:00
|
|
|
self.setcolor('black')
|
2003-01-05 08:51:35 +01:00
|
|
|
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
class InputHandler(ExitNotifyThread):
|
|
|
|
"""Listens for input via the curses interfaces"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
#TODO, we need to use the ugly exitnotifythread (rather than simply
|
|
|
|
#threading.Thread here, so exiting this thread via the callback
|
|
|
|
#handler, kills off all parents too. Otherwise, they would simply
|
|
|
|
#continue.
|
|
|
|
def __init__(self, ui):
|
|
|
|
super(InputHandler, self).__init__()
|
|
|
|
self.char_handler = None
|
|
|
|
self.ui = ui
|
|
|
|
self.enabled = Event()
|
2015-01-14 22:58:25 +01:00
|
|
|
# We will only parse input if we are enabled.
|
2011-10-26 16:47:21 +02:00
|
|
|
self.inputlock = RLock()
|
2015-01-14 22:58:25 +01:00
|
|
|
# denotes whether we should be handling the next char.
|
2011-10-26 16:47:21 +02:00
|
|
|
self.start() #automatically start the thread
|
|
|
|
|
|
|
|
def get_next_char(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return the key pressed or -1.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
Wait until `enabled` and loop internally every stdscr.timeout()
|
|
|
|
msecs, releasing the inputlock.
|
|
|
|
:returns: char or None if disabled while in here"""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.enabled.wait()
|
|
|
|
while self.enabled.is_set():
|
|
|
|
with self.inputlock:
|
|
|
|
char = self.ui.stdscr.getch()
|
|
|
|
if char != -1: yield char
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
char_gen = self.get_next_char()
|
|
|
|
for char in char_gen:
|
|
|
|
self.char_handler(char)
|
|
|
|
#curses.ungetch(char)
|
|
|
|
|
|
|
|
def set_char_hdlr(self, callback):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Sets a character callback handler.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
If a key is pressed it will be passed to this handler. Keys
|
|
|
|
include the curses.KEY_RESIZE key.
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
callback is a function taking a single arg -- the char pressed.
|
|
|
|
If callback is None, input will be ignored."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
with self.inputlock:
|
|
|
|
self.char_handler = callback
|
|
|
|
# start or stop the parsing of things
|
|
|
|
if callback is None:
|
|
|
|
self.enabled.clear()
|
|
|
|
else:
|
|
|
|
self.enabled.set()
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def input_acquire(self):
|
2003-01-05 08:51:35 +01:00
|
|
|
"""Call this method when you want exclusive input control.
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
Make sure to call input_release afterwards! While this lockis
|
2015-01-14 22:58:25 +01:00
|
|
|
held, input can go to e.g. the getpass input."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.enabled.clear()
|
|
|
|
self.inputlock.acquire()
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def input_release(self):
|
2003-01-05 08:51:35 +01:00
|
|
|
"""Call this method when you are done getting input."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.inputlock.release()
|
|
|
|
self.enabled.set()
|
|
|
|
|
|
|
|
|
|
|
|
class CursesLogHandler(logging.StreamHandler):
|
2011-11-08 13:51:36 +01:00
|
|
|
"""self.ui has been set to the UI class before anything is invoked"""
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def emit(self, record):
|
2012-02-19 17:55:06 +01:00
|
|
|
log_str = logging.StreamHandler.format(self, record)
|
2011-10-26 16:47:21 +02:00
|
|
|
color = self.ui.gettf().curses_color
|
|
|
|
# We must acquire both locks. Otherwise, deadlock can result.
|
|
|
|
# This can happen if one thread calls _msg (locking curses, then
|
|
|
|
# tf) and another tries to set the color (locking tf, then curses)
|
|
|
|
#
|
|
|
|
# By locking both up-front here, in this order, we prevent deadlock.
|
|
|
|
self.ui.tframe_lock.acquire()
|
|
|
|
self.ui.lock()
|
|
|
|
try:
|
2011-11-08 13:51:36 +01:00
|
|
|
y,x = self.ui.logwin.getyx()
|
|
|
|
if y or x: self.ui.logwin.addch(10) # no \n before 1st item
|
|
|
|
self.ui.logwin.addstr(log_str, color)
|
2017-04-12 12:25:46 +02:00
|
|
|
self.ui.logwin.noutrefresh()
|
|
|
|
self.ui.stdscr.refresh()
|
2011-10-26 16:47:21 +02:00
|
|
|
finally:
|
|
|
|
self.ui.unlock()
|
|
|
|
self.ui.tframe_lock.release()
|
|
|
|
|
|
|
|
class Blinkenlights(UIBase, CursesUtil):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Curses-cased fancy UI.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
Notable instance variables self. ....:
|
|
|
|
|
|
|
|
- stdscr: THe curses std screen
|
|
|
|
- bannerwin: The top line banner window
|
|
|
|
- width|height: The total curses screen dimensions
|
|
|
|
- logheight: Available height for the logging part
|
|
|
|
- log_con_handler: The CursesLogHandler()
|
|
|
|
- threadframes:
|
|
|
|
- accframes[account]: 'Accountframe'"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Blinkenlights, self).__init__(*args, **kwargs)
|
|
|
|
CursesUtil.__init__(self)
|
|
|
|
|
|
|
|
################################################## UTILS
|
|
|
|
def setup_consolehandler(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Backend specific console handler.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
Sets up things and adds them to self.logger.
|
|
|
|
:returns: The logging.Handler() for console output"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
# create console handler with a higher log level
|
|
|
|
ch = CursesLogHandler()
|
|
|
|
#ch.setLevel(logging.DEBUG)
|
|
|
|
# create formatter and add it to the handlers
|
|
|
|
self.formatter = logging.Formatter("%(message)s")
|
|
|
|
ch.setFormatter(self.formatter)
|
|
|
|
# add the handlers to the logger
|
|
|
|
self.logger.addHandler(ch)
|
|
|
|
# the handler is not usable yet. We still need all the
|
|
|
|
# intialization stuff currently done in init_banner. Move here?
|
|
|
|
return ch
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2003-01-07 04:15:22 +01:00
|
|
|
def isusable(s):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Returns true if the backend is usable ie Curses works."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2003-01-07 04:15:22 +01:00
|
|
|
# Not a terminal? Can't use curses.
|
|
|
|
if not sys.stdout.isatty() and sys.stdin.isatty():
|
2011-10-26 16:47:21 +02:00
|
|
|
return False
|
2003-01-07 04:15:22 +01:00
|
|
|
# No TERM specified? Can't use curses.
|
2011-10-26 16:47:21 +02:00
|
|
|
if not os.environ.get('TERM', None):
|
|
|
|
return False
|
|
|
|
# Test if ncurses actually starts up fine. Only do so for
|
|
|
|
# python>=2.6.6 as calling initscr() twice messing things up.
|
|
|
|
# see http://bugs.python.org/issue7567 in python 2.6 to 2.6.5
|
|
|
|
if sys.version_info[0:3] < (2,6) or sys.version_info[0:3] >= (2,6,6):
|
|
|
|
try:
|
|
|
|
curses.initscr()
|
|
|
|
curses.endwin()
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def init_banner(self):
|
|
|
|
self.availablethreadframes = {}
|
|
|
|
self.threadframes = {}
|
|
|
|
self.accframes = {}
|
|
|
|
self.aflock = Lock()
|
|
|
|
|
|
|
|
self.stdscr = curses.initscr()
|
|
|
|
# turn off automatic echoing of keys to the screen
|
|
|
|
curses.noecho()
|
|
|
|
# react to keys instantly, without Enter key
|
|
|
|
curses.cbreak()
|
|
|
|
# return special key values, eg curses.KEY_LEFT
|
|
|
|
self.stdscr.keypad(1)
|
|
|
|
# wait 1s for input, so we don't block the InputHandler infinitely
|
|
|
|
self.stdscr.timeout(1000)
|
|
|
|
curses.start_color()
|
|
|
|
# turn off cursor and save original state
|
|
|
|
self.oldcursor = None
|
2003-01-07 04:15:22 +01:00
|
|
|
try:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.oldcursor = curses.curs_set(0)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.stdscr.clear()
|
|
|
|
self.stdscr.refresh()
|
|
|
|
self.init_colorpairs()
|
|
|
|
# set log handlers ui to ourself
|
|
|
|
self._log_con_handler.ui = self
|
|
|
|
self.setupwindows()
|
|
|
|
# Settup keyboard handler
|
|
|
|
self.inputhandler = InputHandler(self)
|
|
|
|
self.inputhandler.set_char_hdlr(self.on_keypressed)
|
|
|
|
|
|
|
|
self.gettf().setcolor('red')
|
|
|
|
self.info(offlineimap.banner)
|
|
|
|
|
|
|
|
def acct(self, *args):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Output that we start syncing an account (and start counting)."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.gettf().setcolor('purple')
|
|
|
|
super(Blinkenlights, self).acct(*args)
|
|
|
|
|
|
|
|
def connecting(self, *args):
|
|
|
|
self.gettf().setcolor('white')
|
|
|
|
super(Blinkenlights, self).connecting(*args)
|
|
|
|
|
|
|
|
def syncfolders(self, *args):
|
|
|
|
self.gettf().setcolor('blue')
|
|
|
|
super(Blinkenlights, self).syncfolders(*args)
|
|
|
|
|
|
|
|
def syncingfolder(self, *args):
|
|
|
|
self.gettf().setcolor('cyan')
|
|
|
|
super(Blinkenlights, self).syncingfolder(*args)
|
|
|
|
|
|
|
|
def skippingfolder(self, *args):
|
|
|
|
self.gettf().setcolor('cyan')
|
|
|
|
super(Blinkenlights, self).skippingfolder(*args)
|
|
|
|
|
|
|
|
def loadmessagelist(self, *args):
|
|
|
|
self.gettf().setcolor('green')
|
|
|
|
super(Blinkenlights, self).loadmessagelist(*args)
|
|
|
|
|
|
|
|
def syncingmessages(self, *args):
|
|
|
|
self.gettf().setcolor('blue')
|
|
|
|
super(Blinkenlights, self).syncingmessages(*args)
|
|
|
|
|
2016-06-28 23:58:03 +02:00
|
|
|
def ignorecopyingmessage(self, *args):
|
|
|
|
self.gettf().setcolor('red')
|
|
|
|
super(Blinkenlights, self).ignorecopyingmessage(*args)
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def copyingmessage(self, *args):
|
|
|
|
self.gettf().setcolor('orange')
|
|
|
|
super(Blinkenlights, self).copyingmessage(*args)
|
|
|
|
|
|
|
|
def deletingmessages(self, *args):
|
|
|
|
self.gettf().setcolor('red')
|
|
|
|
super(Blinkenlights, self).deletingmessages(*args)
|
|
|
|
|
|
|
|
def addingflags(self, *args):
|
|
|
|
self.gettf().setcolor('blue')
|
|
|
|
super(Blinkenlights, self).addingflags(*args)
|
|
|
|
|
|
|
|
def deletingflags(self, *args):
|
|
|
|
self.gettf().setcolor('blue')
|
|
|
|
super(Blinkenlights, self).deletingflags(*args)
|
|
|
|
|
|
|
|
def callhook(self, *args):
|
|
|
|
self.gettf().setcolor('white')
|
|
|
|
super(Blinkenlights, self).callhook(*args)
|
|
|
|
|
|
|
|
############ Generic logging functions #############################
|
|
|
|
def warn(self, msg, minor=0):
|
|
|
|
self.gettf().setcolor('red', curses.A_BOLD)
|
|
|
|
super(Blinkenlights, self).warn(msg)
|
|
|
|
|
|
|
|
def threadExited(self, thread):
|
2011-11-03 14:21:25 +01:00
|
|
|
acc = self.getthreadaccount(thread)
|
2011-10-26 16:47:21 +02:00
|
|
|
with self.tframe_lock:
|
2011-11-03 14:21:25 +01:00
|
|
|
if thread in self.threadframes[acc]:
|
|
|
|
tf = self.threadframes[acc][thread]
|
2011-10-26 16:47:21 +02:00
|
|
|
tf.setcolor('black')
|
2011-11-03 14:21:25 +01:00
|
|
|
self.availablethreadframes[acc].append(tf)
|
|
|
|
del self.threadframes[acc][thread]
|
2011-10-26 16:47:21 +02:00
|
|
|
super(Blinkenlights, self).threadExited(thread)
|
|
|
|
|
|
|
|
def gettf(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return the ThreadFrame() of the current thread."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
cur_thread = currentThread()
|
2011-11-03 14:21:25 +01:00
|
|
|
acc = self.getthreadaccount() #Account() or None
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
with self.tframe_lock:
|
|
|
|
# Ideally we already have self.threadframes[accountname][thread]
|
|
|
|
try:
|
2011-11-03 14:21:25 +01:00
|
|
|
if cur_thread in self.threadframes[acc]:
|
|
|
|
return self.threadframes[acc][cur_thread]
|
2011-10-26 16:47:21 +02:00
|
|
|
except KeyError:
|
|
|
|
# Ensure threadframes already has an account dict
|
2011-11-03 14:21:25 +01:00
|
|
|
self.threadframes[acc] = {}
|
|
|
|
self.availablethreadframes[acc] = deque()
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
# If available, return a ThreadFrame()
|
2011-11-03 14:21:25 +01:00
|
|
|
if len(self.availablethreadframes[acc]):
|
|
|
|
tf = self.availablethreadframes[acc].popleft()
|
2011-10-26 16:47:21 +02:00
|
|
|
tf.std_color()
|
|
|
|
else:
|
2011-11-03 14:21:25 +01:00
|
|
|
tf = self.getaccountframe(acc).get_new_tframe()
|
|
|
|
self.threadframes[acc][cur_thread] = tf
|
2011-10-26 16:47:21 +02:00
|
|
|
return tf
|
|
|
|
|
|
|
|
def on_keypressed(self, key):
|
|
|
|
# received special KEY_RESIZE, resize terminal
|
|
|
|
if key == curses.KEY_RESIZE:
|
|
|
|
self.resizeterm()
|
2016-10-14 13:42:33 +02:00
|
|
|
return
|
2003-01-07 04:15:22 +01:00
|
|
|
|
2008-07-16 21:12:38 +02:00
|
|
|
if key < 1 or key > 255:
|
2003-01-07 03:18:09 +01:00
|
|
|
return
|
|
|
|
if chr(key) == 'q':
|
2012-01-07 14:59:42 +01:00
|
|
|
# Request to quit completely.
|
|
|
|
self.warn("Requested shutdown via 'q'")
|
|
|
|
offlineimap.accounts.Account.set_abort_event(self.config, 3)
|
2003-01-07 03:18:09 +01:00
|
|
|
try:
|
2011-11-03 12:25:55 +01:00
|
|
|
index = int(chr(key))
|
2003-01-07 03:18:09 +01:00
|
|
|
except ValueError:
|
2011-11-03 13:45:44 +01:00
|
|
|
return # Key not a valid number: exit.
|
2011-10-26 16:47:21 +02:00
|
|
|
if index >= len(self.hotkeys):
|
2003-01-07 03:18:09 +01:00
|
|
|
# Not in our list of valid hotkeys.
|
|
|
|
return
|
|
|
|
# Trying to end sleep somewhere.
|
2011-10-26 16:47:21 +02:00
|
|
|
self.getaccountframe(self.hotkeys[index]).syncnow()
|
|
|
|
|
2011-10-27 16:58:44 +02:00
|
|
|
def sleep(self, sleepsecs, account):
|
|
|
|
self.gettf().setcolor('red')
|
2015-01-14 22:58:25 +01:00
|
|
|
self.info("Next sync in %d:%02d"% (sleepsecs / 60, sleepsecs % 60))
|
2011-10-27 16:58:44 +02:00
|
|
|
return super(Blinkenlights, self).sleep(sleepsecs, account)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def sleeping(self, sleepsecs, remainingsecs):
|
2011-11-02 12:48:22 +01:00
|
|
|
if not sleepsecs:
|
|
|
|
# reset color to default if we are done sleeping.
|
|
|
|
self.gettf().setcolor('white')
|
2011-11-03 14:21:25 +01:00
|
|
|
accframe = self.getaccountframe(self.getthreadaccount())
|
|
|
|
return accframe.sleeping(sleepsecs, remainingsecs)
|
2011-10-26 16:47:21 +02:00
|
|
|
|
|
|
|
def resizeterm(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Resize the current windows."""
|
|
|
|
|
2011-11-02 17:03:40 +01:00
|
|
|
self.exec_locked(self.setupwindows, True)
|
2003-01-07 03:18:09 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def mainException(self):
|
|
|
|
UIBase.mainException(self)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
def getpass(self, accountname, config, errmsg=None):
|
2011-10-26 16:47:21 +02:00
|
|
|
# disable the hotkeys inputhandler
|
|
|
|
self.inputhandler.input_acquire()
|
2003-01-06 22:58:29 +01:00
|
|
|
|
|
|
|
# See comment on _msg for info on why both locks are obtained.
|
2011-10-26 16:47:21 +02:00
|
|
|
self.lock()
|
2003-01-05 09:07:00 +01:00
|
|
|
try:
|
2011-10-26 16:47:21 +02:00
|
|
|
#s.gettf().setcolor('white')
|
|
|
|
self.warn(" *** Input Required")
|
|
|
|
self.warn(" *** Please enter password for account %s: " % \
|
|
|
|
accountname)
|
|
|
|
self.logwin.refresh()
|
|
|
|
password = self.logwin.getstr()
|
2003-01-05 09:07:00 +01:00
|
|
|
finally:
|
2011-10-26 16:47:21 +02:00
|
|
|
self.unlock()
|
|
|
|
self.inputhandler.input_release()
|
2003-01-05 09:07:00 +01:00
|
|
|
return password
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def setupwindows(self, resize=False):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Setup and draw bannerwin and logwin.
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2011-11-08 13:51:36 +01:00
|
|
|
If `resize`, don't create new windows, just adapt size. This
|
|
|
|
function should be invoked with CursesUtils.locked()."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
self.height, self.width = self.stdscr.getmaxyx()
|
|
|
|
self.logheight = self.height - len(self.accframes) - 1
|
|
|
|
if resize:
|
2016-10-14 13:42:33 +02:00
|
|
|
if curses.is_term_resized(self.height, self.width):
|
|
|
|
curses.resizeterm(self.height, self.width)
|
2011-10-26 16:47:21 +02:00
|
|
|
self.bannerwin.resize(1, self.width)
|
2011-11-02 17:03:40 +01:00
|
|
|
self.logwin.resize(self.logheight, self.width)
|
2016-10-14 13:42:33 +02:00
|
|
|
self.stdscr.clear()
|
|
|
|
self.stdscr.noutrefresh()
|
2011-10-26 16:47:21 +02:00
|
|
|
else:
|
|
|
|
self.bannerwin = curses.newwin(1, self.width, 0, 0)
|
|
|
|
self.logwin = curses.newwin(self.logheight, self.width, 1, 0)
|
|
|
|
|
|
|
|
self.draw_bannerwin()
|
2011-11-08 13:51:36 +01:00
|
|
|
self.logwin.idlok(True) # needed for scrollok below
|
2011-11-02 17:03:40 +01:00
|
|
|
self.logwin.scrollok(True) # scroll window when too many lines added
|
2011-10-26 16:47:21 +02:00
|
|
|
self.draw_logwin()
|
|
|
|
self.accounts = reversed(sorted(self.accframes.keys()))
|
|
|
|
pos = self.height - 1
|
|
|
|
index = 0
|
|
|
|
self.hotkeys = []
|
|
|
|
for account in self.accounts:
|
2011-11-02 17:03:40 +01:00
|
|
|
acc_win = curses.newwin(1, self.width, pos, 0)
|
2011-11-03 12:25:55 +01:00
|
|
|
self.accframes[account].setwindow(acc_win, index)
|
2011-11-02 17:03:40 +01:00
|
|
|
self.hotkeys.append(account)
|
|
|
|
index += 1
|
|
|
|
pos -= 1
|
2011-10-26 16:47:21 +02:00
|
|
|
curses.doupdate()
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
def draw_bannerwin(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Draw the top-line banner line."""
|
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
if curses.has_colors():
|
|
|
|
color = curses.A_BOLD | self.curses_colorpair('banner')
|
2003-01-11 00:47:35 +01:00
|
|
|
else:
|
|
|
|
color = curses.A_REVERSE
|
2011-11-02 17:03:40 +01:00
|
|
|
self.bannerwin.clear() # Delete old content (eg before resizes)
|
2011-10-26 16:47:21 +02:00
|
|
|
self.bannerwin.bkgd(' ', color) # Fill background with that color
|
2017-04-12 12:25:46 +02:00
|
|
|
string = "%s %s" % (offlineimap.__productname__,
|
|
|
|
offlineimap.__version__)
|
|
|
|
spaces = " " * max(1, (self.width - len(offlineimap.__copyright__)
|
|
|
|
- len(string) - 1))
|
|
|
|
string = "%s%s%s" % (string, spaces, offlineimap.__copyright__)
|
|
|
|
self.bannerwin.addnstr(0, 0, string, self.width - 1, color)
|
2011-10-26 16:47:21 +02:00
|
|
|
self.bannerwin.noutrefresh()
|
|
|
|
|
|
|
|
def draw_logwin(self):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""(Re)draw the current logwindow."""
|
|
|
|
|
2011-11-02 17:03:40 +01:00
|
|
|
if curses.has_colors():
|
|
|
|
color = curses.color_pair(0) #default colors
|
|
|
|
else:
|
|
|
|
color = curses.A_NORMAL
|
2011-11-08 13:51:36 +01:00
|
|
|
self.logwin.move(0, 0)
|
2016-10-14 13:42:33 +02:00
|
|
|
self.logwin.clear()
|
2011-10-26 16:47:21 +02:00
|
|
|
self.logwin.bkgd(' ', color)
|
2016-10-14 13:42:33 +02:00
|
|
|
self.logwin.noutrefresh()
|
2011-10-26 16:47:21 +02:00
|
|
|
|
2011-11-03 14:21:25 +01:00
|
|
|
def getaccountframe(self, acc_name):
|
2015-01-14 22:58:25 +01:00
|
|
|
"""Return an AccountFrame() corresponding to acc_name.
|
2011-11-03 14:21:25 +01:00
|
|
|
|
|
|
|
Note that the *control thread uses acc_name `None`."""
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2011-10-26 16:47:21 +02:00
|
|
|
with self.aflock:
|
|
|
|
# 1) Return existing or 2) create a new CursesAccountFrame.
|
|
|
|
if acc_name in self.accframes: return self.accframes[acc_name]
|
|
|
|
self.accframes[acc_name] = CursesAccountFrame(self, acc_name)
|
2011-11-02 17:03:40 +01:00
|
|
|
# update the window layout
|
|
|
|
self.setupwindows(resize= True)
|
2011-10-26 16:47:21 +02:00
|
|
|
return self.accframes[acc_name]
|
|
|
|
|
|
|
|
def terminate(self, *args, **kwargs):
|
|
|
|
curses.nocbreak();
|
|
|
|
self.stdscr.keypad(0);
|
|
|
|
curses.echo()
|
|
|
|
curses.endwin()
|
|
|
|
# need to remove the Curses console handler now and replace with
|
|
|
|
# basic one, so exceptions and stuff are properly displayed
|
|
|
|
self.logger.removeHandler(self._log_con_handler)
|
|
|
|
UIBase.setup_consolehandler(self)
|
2012-02-22 21:50:46 +01:00
|
|
|
# reset the warning method, we do not have curses anymore
|
|
|
|
self.warn = super(Blinkenlights, self).warn
|
2011-10-26 16:47:21 +02:00
|
|
|
# finally call parent terminate which prints out exceptions etc
|
|
|
|
super(Blinkenlights, self).terminate(*args, **kwargs)
|
2003-01-05 08:51:35 +01:00
|
|
|
|
2011-11-03 14:21:25 +01:00
|
|
|
def threadException(self, thread):
|
2011-10-26 16:47:21 +02:00
|
|
|
#self._log_con_handler.stop()
|
2011-11-03 14:21:25 +01:00
|
|
|
UIBase.threadException(self, thread)
|
2003-01-05 08:51:35 +01:00
|
|
|
|