Have a timeout in read_char to enable timed events

This commit is contained in:
Florent Le Coz 2011-04-08 16:36:00 +02:00
parent 36daddfaf2
commit 27a20b349c
2 changed files with 49 additions and 3 deletions

View file

@ -172,6 +172,8 @@ class Core(object):
self.xmpp.add_event_handler("chatstate_gone", self.on_chatstate_gone)
self.xmpp.add_event_handler("chatstate_inactive", self.on_chatstate_inactive)
self.timed_events = set()
def start(self):
"""
Init curses, create the first tab, etc
@ -617,13 +619,29 @@ class Core(object):
tab.resize()
self.refresh_window()
def check_timed_events(self):
pass
def read_keyboard(self):
"""
Get the next keyboard key pressed and returns it.
read_char() has a timeout: it returns None when the timeout
occurs. In that case we do not return (we loop until we get
a non-None value), but we check for timed events instead.
"""
res = read_char(self.stdscr)
while res is None:
self.check_timed_events()
res = read_char(self.stdscr)
return res
def main_loop(self):
"""
main loop waiting for the user to press a key
"""
# curses.ungetch(0) # FIXME
while self.running:
char = read_char(self.stdscr)
char = self.read_keyboard()
# Special case for M-x where x is a number
if char.startswith('M-') and len(char) == 3:
try:
@ -1275,12 +1293,12 @@ class Core(object):
Read 2 more chars and go to the tab
with the given number
"""
char = read_char(self.stdscr)
char = self.read_keyboard()
try:
nb1 = int(char)
except ValueError:
return
char = read_char(self.stdscr)
char = self.read_keyboard()
try:
nb2 = int(char)
except ValueError:

28
src/timed_events.py Normal file
View file

@ -0,0 +1,28 @@
# Copyright 2010-2011 Le Coz Florent <louiz@louiz.org>
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Poezio 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 Poezio. If not, see <http://www.gnu.org/licenses/>.
class TimedEvent(object):
"""
An event with a callback that is called when the specified time is passed
Note that these events can NOT be used for very small delay or a very
precise date, since the check for events is done once per second, as
a maximum
"""
def __init__(self, callback, *args, **kwargs):
self._callback = callback
self.args = args