|
from talon import Context, Module, app, clip, cron, imgui, actions, ui, fs |
|
import os |
|
|
|
######################################################################## |
|
# global settings |
|
######################################################################## |
|
|
|
# a list of reminders where each line is a comma separated list |
|
# e.g. where,wear,ware |
|
# a suitable one can be found here: |
|
# https://github.com/pimentel/reminders |
|
cwd = os.path.dirname(os.path.realpath(__file__)) |
|
reminders_file = os.path.join(cwd, "reminders.csv") |
|
# if quick_replace, then when a word is selected and only one reminder exists, |
|
# replace it without bringing up the options |
|
quick_replace = True |
|
show_help = False |
|
######################################################################## |
|
|
|
ctx = Context() |
|
mod = Module() |
|
mod.mode("reminders") |
|
mod.list("reminders_canonicals", desc="list of words ") |
|
|
|
main_screen = ui.main_screen() |
|
|
|
|
|
def update_reminders(name, flags): |
|
if name != reminders_file: |
|
return |
|
|
|
phones = {} #create a dictionary called phones |
|
canonical_list = [] #create an empty array |
|
with open(reminders_file, "r") as f: #open the file |
|
for line in f: #get a line of the csv |
|
words = line.rstrip().split(",") #create an array of the values on the line of the csv |
|
canonical_list.append(words[0]) #take the first value and put it in the list of link words |
|
for word in words: # going through the words one by one |
|
word = word.lower() #make the word lowercase |
|
old_words = phones.get(word, []) #look for that word in the phones dictionary |
|
phones[word] = sorted(set(old_words + words)) #in the phones dictionary, for the key word, |
|
|
|
global all_reminders |
|
print(phones) |
|
all_reminders = phones |
|
ctx.lists["self.reminders_canonicals"] = canonical_list |
|
print(canonical_list) |
|
|
|
|
|
update_reminders(reminders_file, None) |
|
fs.watch(cwd, update_reminders) |
|
active_word_list = None |
|
is_selection = False |
|
|
|
|
|
def close_reminders(): |
|
gui.hide() |
|
actions.mode.disable("user.reminders") |
|
|
|
|
|
def raise_reminders(word, forced=False, selection=False): |
|
global quick_replace |
|
global active_word_list |
|
global show_help |
|
global force_raise |
|
global is_selection |
|
|
|
force_raise = forced |
|
is_selection = selection |
|
|
|
if is_selection: |
|
word = word.strip() |
|
|
|
is_capitalized = word == word.capitalize() |
|
is_upper = word.isupper() |
|
|
|
word = word.lower() |
|
|
|
if word not in all_reminders: |
|
app.notify("reminders.py", '"%s" not in reminders list' % word) |
|
return |
|
|
|
active_word_list = all_reminders[word] |
|
if ( |
|
is_selection |
|
and len(active_word_list) == 2 |
|
and quick_replace |
|
and not force_raise |
|
): |
|
if word == active_word_list[0].lower(): |
|
new = active_word_list[1] |
|
else: |
|
new = active_word_list[0] |
|
|
|
if is_capitalized: |
|
new = new.capitalize() |
|
elif is_upper: |
|
new = new.upper() |
|
|
|
clip.set(new) |
|
actions.edit.paste() |
|
|
|
return |
|
|
|
actions.mode.enable("user.reminders") |
|
show_help = False |
|
gui.show() |
|
|
|
|
|
@imgui.open(x=main_screen.x + main_screen.width / 2.6, y=main_screen.y) |
|
def gui(gui: imgui.GUI): |
|
global active_word_list |
|
if show_help: |
|
gui.text("Homephone help - todo") |
|
else: |
|
gui.text("Select a reminder") |
|
gui.line() |
|
index = 1 |
|
for word in active_word_list: |
|
gui.text("Choose {}: {} ".format(index, word)) |
|
index = index + 1 |
|
|
|
|
|
def show_help_gui(): |
|
global show_help |
|
show_help = True |
|
gui.show() |
|
|
|
|
|
@mod.capture(rule="{self.reminders_canonicals}") |
|
def reminders_canonical(m) -> str: |
|
"Returns a single string" |
|
return m.reminders_canonicals |
|
|
|
|
|
@mod.action_class |
|
class Actions: |
|
def reminders_hide(): |
|
"""Hides the reminders display""" |
|
close_reminders() |
|
|
|
def reminders_show(m: str): |
|
"""Sentence formatter""" |
|
print(m) |
|
raise_reminders(m, False, False) |
|
|
|
def reminders_show_selection(): |
|
"""Sentence formatter""" |
|
raise_reminders(actions.edit.selected_text(), False, True) |
|
|
|
def reminders_force_show(m: str): |
|
"""Sentence formatter""" |
|
raise_reminders(m, True, False) |
|
|
|
def reminders_force_show_selection(): |
|
"""Sentence formatter""" |
|
raise_reminders(actions.edit.selected_text(), True, True) |
|
|
|
def reminders_select(number: int) -> str: |
|
"""selects the reminder by number""" |
|
if number <= len(active_word_list) and number > 0: |
|
return active_word_list[number - 1] |
|
|
|
error = "reminders.py index {} is out of range (1-{})".format( |
|
number, len(active_word_list) |
|
) |
|
app.notify(error) |
|
raise error |
|
|