snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 367bb5dff5b21ce0abea5420dec8e5c26d310cc4
parent e8dc011ecc296a2d91883b0945e250014ce43516
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Tue, 16 Jun 2020 18:43:21 -0400

Merge pull request #127 from karx1/master

speaknspell: Add ability to use input from history
Diffstat:
Msnakewm/apps/fun/speaknspell/speak.py | 47++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/snakewm/apps/fun/speaknspell/speak.py b/snakewm/apps/fun/speaknspell/speak.py @@ -54,6 +54,12 @@ class SpeakSpell(pygame_gui.elements.UIWindow): self.speak("Hello, thank you for using snakeware!") self.input.focus() + # history attributes + self.histsize = 100 + self.histindex = -1 + self.history = ["Hello, thank you for using snakeware!"] + self.cached_command = "" + def speak(self, text): if self.speakthrd is not None and self.speakthrd.is_alive(): return @@ -61,8 +67,7 @@ class SpeakSpell(pygame_gui.elements.UIWindow): if text == "": return - text = text.replace("\n", "<br>") - + text = re.sub(r"(\\r)?\\n", "<br>", text) spoken = re.sub(r"<(.*?)>", "", text) self.engine.say(spoken) self.speakthrd = threading.Thread(target=self.engine.runAndWait, args=()) @@ -71,8 +76,44 @@ class SpeakSpell(pygame_gui.elements.UIWindow): self.box.rebuild() self.input.set_text("") + def cache_command(self): + self.cached_command = self.input.get_text() + + def flush_command_cache(self): + self.cached_command = "" + + def set_histindex(self, increment): + try: + # self.history[self.histindex + increment] + self.histindex += increment + except IndexError: + pass + return self.histindex + + def set_from_history(self): + if self.histindex > -1: + self.input.set_text(self.history[self.histindex]) + else: + self.input.set_text(self.cached_command) + self.input.edit_position = len(self.input.get_text()) + + def add_to_history(self, text): + self.history = [text] + self.history + if len(self.history) > self.histsize: + del self.history[-1] + def process_event(self, event): super().process_event(event) if event.type == pygame.USEREVENT and event.ui_element == self.input: if event.user_type == pygame_gui.UI_TEXT_ENTRY_FINISHED: - self.speak(self.input.get_text()) + text = self.input.get_text() + self.add_to_history(text) + self.histindex = -1 + self.flush_command_cache() + self.speak(text) + elif event.type == pygame.KEYUP and event.key in (pygame.K_UP, pygame.K_DOWN): + increment = 1 if event.key == pygame.K_UP else -1 + if self.histindex == -1: + self.cache_command() + self.set_histindex(increment) + self.set_from_history()