From 677a2f71206ad515a07738fff405235b9ba8b94b Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Fri, 19 Feb 2016 08:39:13 -0800 Subject: added ability to break pause loops --- oo.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/oo.py b/oo.py index 661fc1a..9b1532b 100644 --- a/oo.py +++ b/oo.py @@ -542,6 +542,34 @@ class ooPlay: self.screen.refresh() pause_length = 80 + def sleep(self, delay=1): + """Like most sleep functions, but can be escaped with spacebar. + + delay is in units of self.pause_length, + the standard delay per char for the class. + delay may be a positive integer or float. + + Implementation is such that keypresses other than spacebar + can shorten or lengthen the pause, + but the pause is capped at twice the intended delay. + + Returns True if spacebar was pressed. + """ + pause = int(self.pause_length * delay) + self.screen.timeout(pause) + while True: + inp = self.screen.getch() + if inp == -1: + space_pressed = False + break + if inp == ord(" "): + space_pressed = True + break + pause //= 2 + self.screen.timeout(pause) + self.screen.timeout(-1) + return space_pressed + def write(self, string=None, pause=None): """Write string to the bottom line. @@ -551,7 +579,7 @@ class ooPlay: scrolls through string if a string is not given, then "H" is used - if pause is True, there will be a pause for reading + if pause is True, there will be a pause for reading at the end if pause is not given but a string is, there will be a pause if neither a pause nor a string is given, there will not be a pause @@ -575,8 +603,7 @@ class ooPlay: centered_string = string.center(width, " ") self.screen.addstr(self.Y - 1, 0, centered_string, color) self.screen.refresh() - if pause: curses.napms(self.pause_length * len(string)) - curses.ungetch(0) # clear input + if pause: self.sleep(2*len(string)) return # scrolling through a wider string @@ -584,14 +611,12 @@ class ooPlay: strings = [string[i:i + width] for i in range(len(string) - width + 1)] self.screen.addstr(self.Y - 1, 0, strings[0], color) self.screen.refresh() - curses.napms(self.pause_length * width) + if self.sleep(width): return for s in strings: self.screen.addstr(self.Y - 1, 0, s, color) self.screen.refresh() - curses.napms(self.pause_length) - if pause: curses.napms(self.pause_length * width) - curses.ungetch(0) # clear input - #TODO: ungetch appears to not clear inputs as desired :/ + if self.sleep(): return + if pause: self.sleep(width) def write_help(self): """Write one of the help messages.""" -- cgit v1.2.3-54-g00ecf