-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (98 loc) · 3.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import sys
import pkgutil
from importlib import import_module
from prompt_toolkit import PromptSession
from prompt_toolkit.styles import Style
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.history import FileHistory
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.shortcuts import clear
def look_for_file(filename):
if not os.path.exists(filename):
with open(filename, "w") as f:
f.write("")
def time_emoji():
import datetime
now = datetime.datetime.now()
if now.hour >= 18 or now.hour < 6:
return "🌙"
else:
return "🌞"
class Todo():
base_dir = os.path.normpath(os.path.join(os.path.dirname(__file__)))
commands = {}
rprompt_message = time_emoji()
time_emoji = time_emoji()
style = Style.from_dict(
{
'rprompt': 'bg:black',
'btoolbar': 'bg:black',
}
)
keybinds = KeyBindings()
@keybinds.add('c-c')
def _(event):
# clear()
exit()
@keybinds.add('c-d')
def _(event):
# clear()
exit()
class inputValidator(Validator):
# validates all registered commands
def __init__(self, todoobject):
self.todoobject = todoobject
def validate(self, document):
text = document.text
user_input = text.split()
for i in user_input:
if i == " ":
return
if user_input and not user_input[0] in self.todoobject.commands:
raise ValidationError(message=f"Unknown command {text}")
def load_commands(todo, session):
path = os.path.join(os.path.dirname(__file__), "commands")
modules = pkgutil.iter_modules(path=[path])
for loader, mod_name, ispkg in modules:
# Ensure that module isn't already loaded
if mod_name not in sys.modules:
# Import module
loaded_mod = import_module("commands." + mod_name)
# Load class from imported module
class_name = "".join([x.title() for x in mod_name.split("_")])
loaded_class = getattr(loaded_mod, class_name, None)
if not loaded_class:
continue
# Create an instance of the class
instance = loaded_class(todo, session)
def main_loop():
todo = Todo()
look_for_file('todo.json')
look_for_file('.todo_history')
session = PromptSession(history=FileHistory('.todo_history'))
load_commands(Todo, session)
clear()
list_open = todo.commands.get("list")
list_open.do_command("list")
while True:
user_input = session.prompt(
"$ ",
# completer=ClaseCOmpletadora(),
# auto_suggest=AutoSuggestFromHistory(),
# validator=inputValidator(todo),
key_bindings=todo.keybinds,
rprompt=todo.rprompt_message,
style=todo.style,
)
if not user_input:
continue
else:
user_input = user_input.split()
command = todo.commands.get(user_input[0].lower()) or None
if not command:
print("Unknown command")
continue
command.do_command(*user_input[1:])
if __name__ == "__main__":
main_loop()