phrase_history.py (2037B)
1 from talon import Module, actions, imgui 2 import logging 3 4 mod = Module() 5 6 # list of recent phrases, most recent first 7 phrase_history = [] 8 phrase_history_length = 40 9 phrase_history_display_length = 40 10 11 @mod.action_class 12 class Actions: 13 def get_last_phrase() -> str: 14 """Gets the last phrase""" 15 return phrase_history[0] if phrase_history else "" 16 17 def get_recent_phrase(number: int) -> str: 18 """Gets the nth most recent phrase""" 19 try: return phrase_history[number-1] 20 except IndexError: return "" 21 22 def clear_last_phrase(): 23 """Clears the last phrase""" 24 # Currently, this removes the cleared phrase from the phrase history, so 25 # that repeated calls clear successively earlier phrases, which is often 26 # useful. But it would be nice if we could do this without removing 27 # those phrases from the history entirely, so that they were still 28 # accessible for copying, for example. 29 if not phrase_history: 30 logging.warning("clear_last_phrase(): No last phrase to clear!") 31 return 32 for _ in phrase_history[0]: 33 actions.edit.delete() 34 phrase_history.pop(0) 35 36 def select_last_phrase(): 37 """Selects the last phrase""" 38 if not phrase_history: 39 logging.warning("select_last_phrase(): No last phrase to select!") 40 return 41 for _ in phrase_history[0]: 42 actions.edit.extend_left() 43 44 def add_phrase_to_history(text: str): 45 """Adds a phrase to the phrase history""" 46 global phrase_history 47 phrase_history.insert(0, text) 48 phrase_history = phrase_history[:phrase_history_length] 49 50 def toggle_phrase_history(): 51 """Toggles list of recent phrases""" 52 if gui.showing: gui.hide() 53 else: gui.show() 54 55 @imgui.open() 56 def gui(gui: imgui.GUI): 57 gui.text("Recent phrases") 58 gui.line() 59 for index, text in enumerate(phrase_history[:phrase_history_display_length], 1): 60 gui.text(f"{index}: {text}")