-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_list.py
307 lines (246 loc) · 8.91 KB
/
todo_list.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
import json
import sys
import sublime
import sublime_plugin
class TodoListMainObject(object):
def __init__(self):
self.selected_list = None
try:
if os.path.exists(TodoListMainObject.file_path):
with open(TodoListMainObject.file_path) as json_file:
TodoListMainObject.loaded_lists = json.load(json_file)
else:
TodoListMainObject.save_file()
except IOError:
return None
def move_todo(self, idx, new_idx):
todo_item = TodoListMainObject.loaded_lists[
self.selected_list
].pop(idx)
TodoListMainObject.loaded_lists[
self.selected_list
].insert(
new_idx,
todo_item
)
TodoListMainObject.save_file()
@staticmethod
def save_file():
with open(TodoListMainObject.file_path, 'w') as outfile:
json.dump(TodoListMainObject.loaded_lists, outfile)
@staticmethod
def get_setting(key, default=None):
settings = sublime.load_settings('TodoList.sublime-settings')
if os.name == 'nt':
os_specific_settings = sublime.load_settings(
'TodoList (Windows).sublime-settings'
)
elif sys.platform == 'darwin':
os_specific_settings = sublime.load_settings(
'TodoList (OSX).sublime-settings'
)
else:
os_specific_settings = sublime.load_settings(
'TodoList (Linux).sublime-settings'
)
return os_specific_settings.get(key, settings.get(key, default))
""" Class variables """
loaded_lists = {}
custom_file_path = get_setting.__func__('file_path')
"""defaults to user-path of OS"""
if custom_file_path:
file_path = custom_file_path
else:
file_path = os.path.expanduser(
os.path.join('~', 'todo_list.json')
)
class TodoListMenuCommand(sublime_plugin.WindowCommand):
def run(self):
self.menu = []
if not getattr(self.window, 'todo_instance', None):
self.window.todo_instance = TodoListMainObject()
if self.window.todo_instance.selected_list:
if len(TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
]) > 0:
self.menu.append(
{
'text': 'Todos: List all',
'command': 'todo_list_list_all'
}
)
self.menu.append(
{
'text': 'Todos: Add', 'command': 'todo_list_add_todo'
}
)
if len(TodoListMainObject.loaded_lists) > 0:
self.menu.append(
{
'text': 'List: Switch',
'command': 'todo_list_load_list'
}
)
self.menu.append(
{
'text': 'List: Delete current',
'command': 'todo_list_delete_current_list'
}
)
else:
if len(TodoListMainObject.loaded_lists) > 0:
self.menu.append(
{
'text': 'List: Load', 'command': 'todo_list_load_list'
}
)
self.menu.append(
{
'text': 'List: Delete',
'command': 'todo_list_delete_list'
}
)
self.menu.append(
{
'text': 'List: Create new', 'command': 'todo_list_create_list'
}
)
self.window.show_quick_panel(
[item['text'] for item in self.menu],
self.on_done,
)
def on_done(self, idx: int):
if idx >= 0 and idx < len(self.menu):
self.window.run_command(self.menu[idx]['command'])
class TodoListCreateListCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel(
'Enter list name:',
'',
self.on_done,
None,
None
)
def on_done(self, list_name: str):
TodoListMainObject.loaded_lists[list_name] = []
self.window.todo_instance.selected_list = list_name
TodoListMainObject.save_file()
self.window.run_command('todo_list_menu')
class TodoListLoadListCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_quick_panel(
list(TodoListMainObject.loaded_lists.keys()),
self.on_done,
)
def on_done(self, list_idx: int):
self.window.todo_instance.selected_list = list(
TodoListMainObject.loaded_lists.keys()
)[list_idx]
self.window.run_command('todo_list_menu')
class TodoListDeleteListCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_quick_panel(
list(TodoListMainObject.loaded_lists.keys()),
self.on_done,
)
def on_done(self, list_idx: int):
list_name = list(
TodoListMainObject.loaded_lists.keys()
)[list_idx]
del TodoListMainObject.loaded_lists[list_name]
TodoListMainObject.save_file()
if self.window.todo_instance.selected_list == list_name:
self.window.todo_instance.selected_list = None
self.window.run_command('todo_list_menu')
class TodoListDeleteCurrentListCommand(sublime_plugin.WindowCommand):
def run(self):
if self.window.todo_instance.selected_list:
del TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
]
TodoListMainObject.save_file()
self.window.todo_instance.selected_list = None
self.window.run_command('todo_list_menu')
class TodoListListAllCommand(sublime_plugin.WindowCommand):
def run(self):
if (not getattr(self.window, 'todo_instance', None) or
not self.window.todo_instance.selected_list):
self.window.run_command('todo_list_menu')
self.window.show_quick_panel(
TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
],
self.on_done,
)
def on_done(self, idx: int):
if idx >= 0:
self.submenu = []
if idx > 0:
self.submenu.append({
'text': 'Move up',
'command': 'todo_list_move_up',
'todo_idx': idx
})
if idx < len(TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
]) - 1:
self.submenu.append({
'text': 'Move down',
'command': 'todo_list_move_down',
'todo_idx': idx
})
self.submenu.append({
'text': 'Remove item',
'command': 'todo_list_remove_todo',
'todo_idx': idx
})
self.window.show_quick_panel(
[item['text'] for item in self.submenu],
self.on_sub_select
)
elif idx == -1:
self.window.run_command('todo_list_menu')
def on_sub_select(self, idx: int):
"""on selected action on todo-item"""
if idx >= 0 and idx < len(self.submenu):
self.window.run_command(
self.submenu[idx]['command'],
{'todo_idx': self.submenu[idx]['todo_idx']}
)
else:
self.window.run_command('todo_list_list_all')
class TodoListRemoveTodoCommand(sublime_plugin.WindowCommand):
def run(self, **args):
del TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
][args['todo_idx']]
TodoListMainObject.save_file()
self.window.run_command('todo_list_list_all')
class TodoListAddTodoCommand(sublime_plugin.WindowCommand):
def on_done(self, text: str):
TodoListMainObject.loaded_lists[
self.window.todo_instance.selected_list
].append(text)
TodoListMainObject.save_file()
self.window.run_command('todo_list_list_all')
def run(self):
self.window.show_input_panel(
'Add to-do:',
'',
self.on_done,
None,
None
)
class TodoListMoveUpCommand(sublime_plugin.WindowCommand):
def run(self, **args):
self.window.todo_instance.move_todo(
args['todo_idx'], args['todo_idx'] - 1
)
self.window.run_command('todo_list_list_all')
class TodoListMoveDownCommand(sublime_plugin.WindowCommand):
def run(self, **args):
self.window.todo_instance.move_todo(
args['todo_idx'], args['todo_idx'] + 1
)
self.window.run_command('todo_list_list_all')