-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide.py
308 lines (273 loc) · 9.85 KB
/
guide.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
308
import sublime, sublime_plugin
import datetime, getpass
import re
class Util():
def getMoveValue(selfself, self, loc, edit):
line_pos = self.view.line(loc)
indent = selfself.getIndent(self, loc)
line_text = self.view.substr(line_pos).strip()
pos = line_text.find('>')
target = line_text[:pos]
is_increment = target.find('+')
if (pos != -1 and is_increment != -1):
increment_value = int(target[is_increment+1:])
target = target[:is_increment]
self.view.replace(edit, sublime.Region(line_pos.a + indent, line_pos.a + indent + len(target)), str(increment_value + int(target)))
is_decrement = target.find('-')
if (pos != -1 and is_decrement != -1):
decrement_value = int(target[is_decrement+1:])
target = target[:is_decrement]
if (-decrement_value + int(target) > 0 ):
self.view.replace(edit, sublime.Region(line_pos.a + indent, line_pos.a + indent + len(target)), str(-decrement_value + int(target)))
m = re.search('^\d+$',target)
if m:
times = int(m.group(0))
else:
times = 999
return times
def getIndent(selfself, self, loc):
line = self.view.substr(self.view.line(loc))
if (len(line) == 0):
return -1
m = re.search('(^[ ]*)[^ ]',line)
indent = len(m.group(1))
return indent
class MoveBlockCommand(sublime_plugin.TextCommand):
def run(self, edit, is_add_date = False):
if is_add_date:
self.view.run_command('add_date_time')
loc = self.view.sel()[0]
move_value = Util().getMoveValue(self, loc, edit)
line_pos = self.view.line(loc)
beg = line_pos.a
end = line_pos.b
line_text = self.view.substr(line_pos)
m = re.search('(^[ ]*)[^ ]',line_text)
base_ind = len(m.group(1))
next_ind = base_ind + 1
while (next_ind>base_ind):
end = line_pos.b
next_line_pos = self.view.line(line_pos.b+1)
if (len(next_line_pos) == 0 ):
break
next_line = self.view.substr(next_line_pos)
m = re.search('(^[ ]*)[^ ]',next_line)
next_ind = len(m.group(1))
line_pos = next_line_pos
r = sublime.Region(beg, end)
self.view.sel().add(r)
for _ in range(move_value):
next_line_indent = Util().getIndent(self, self.view.sel()[0].b+1)
if (next_line_indent < base_ind):
break
if (next_line_indent == base_ind):
self.view.run_command('swap_line_down')
next_line_indent = Util().getIndent(self, self.view.sel()[0].b+1)
while (next_line_indent > base_ind):
self.view.run_command('swap_line_down')
next_line_indent = Util().getIndent(self, self.view.sel()[0].b+1)
else:
self.view.run_command('swap_line_down')
self.view.sel().clear()
# self.view.run_command('fold_by_level', {"level": 1})
# self.view.sel().add(beg)
# self.view.show(beg)
# self.view.run_command('unfold')
# self.view.sel().clear()
self.view.sel().add(beg)
self.view.show(beg)
class LoggingCommand(sublime_plugin.TextCommand):
def run(self, edit):
pass
# daily output
# weekly output
# monthly output
# yearly output
# last 7 days
# last 30 days
# 1 get the logs of line and put them into a list
log_list = ''
# 2
class CalculatePercentageCommand(sublime_plugin.TextCommand):
def run(self, edit):
print_length = 100
print('beg#percentage')
# 1 Read data into a data structure
size = self.view.size()
init_loc = self.view.line(self.view.sel()[0]).a
lines = self.view.lines(sublime.Region(init_loc,size))
list = []
increment = '0'
for line in lines:
text = self.view.substr(line)
if len(text) <= 0:
continue
if text[0] == ' ':
continue
if (text == '###'):
break
pos_lt = text.find('>')
pos_pipe = text.find('|')
if (pos_pipe == -1):
pos_pipe = len(text)
if (pos_lt != -1):
value = text[:pos_lt].strip()
task = text[pos_lt+1:pos_pipe].strip().strip('>')
increment = '0'
pos_plus = value.find('+');
pos_minu = value.find('-');
if (pos_plus != -1):
increment = value[pos_plus:]
value = value[:pos_plus]
elif (pos_minu != -1):
increment = value[pos_minu:]
value = value[:pos_minu]
m = re.search('^\d+$',value)
if m:
value = int(m.group(0))
else:
value = 999
else:
value = 999
list.append( { 'value': value, 'task': text, 'played': 0, 'increment': int(increment) } )
if (len(list) == 0 ):
print('Guide: no item')
return
# 2 Simulate the data and populate the usage
simulation_number = 9999
for i in range(simulation_number):
# print(i)
list[0]['played'] = list[0]['played'] + 1
if 'first' not in list[0]:
list[0]['first'] = i
pos = 0
val = list[0]['value']
# list[0]['value'] += list[0]['increment']
# list[0]['value'] = max(list[0]['value'], 1)
for _ in range(int(val)):
if (pos+1 == len(list)):
break
list[pos], list[pos+1] = list[pos+1], list[pos]
pos = pos + 1
for element in list:
if 'first' not in element:
element['first'] = simulation_number
# 3 Output the results
list = sorted(list, key=lambda val: val['first'], reverse = False)
self.f(list, simulation_number, print_length)
print('###\n###')
list = sorted(list, key=lambda val: val['played'], reverse = True)
self.f(list, simulation_number, print_length)
head_tail_n = 5
print('###\n--First and last few tasks###')
list = sorted(list, key=lambda val: val['first'], reverse = False)
self.f(list[:head_tail_n], simulation_number, print_length)
print('---')
self.f(list[-head_tail_n:], simulation_number, print_length)
print('number of tasks: ' + str(len(list)))
def f(self, list, simulation_number, print_length):
for element in list:
percentage = round(100 * element['played']/simulation_number, 1)
first = str(element['first'])
val = str(element['value'])
if (val == '999'):
val = '>'
print(str(percentage) + '%\t\t' + str(percentage*70/100) + ' ' +
val + '>(' + first + ')' + element['task'][:print_length])
class MoveToLaterCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command("move_task_batch", {'times': 99 })
class SetupHomeCommand(sublime_plugin.TextCommand):
def run(self, edit):
pos = self.view.find('\d+>@research', 0)
self.view.replace(edit,pos,'5>@research')
self.view.run_command('go_to_first_line')
class SetupWorkCommand(sublime_plugin.TextCommand):
def run(self, edit):
pos = self.view.find('\d+>@research', 0)
self.view.replace(edit,pos,'1>@research')
line_pos = self.view.line(pos)
line_text = self.view.substr(line_pos)
self.view.sel().clear()
self.view.sel().add(sublime.Region(line_pos.a))
self.view.show(line_pos.a)
for _ in range(99):
self.view.run_command('swap_line_up')
self.view.run_command('swap_line_down')
self.view.run_command('go_to_first_line')
# end>[work:]work; another work; | 11/12:37/4/14 | 9/17:16/4/14
class ParseLineCommand(sublime_plugin.TextCommand):
def run(self, edit):
print('beg')
loc = self.view.sel()[0]
line_pos = self.view.line(loc)
line_text = self.view.substr(line_pos)#.strip()
print(line_text)
return line_text
class HorizontalTaskCommand(sublime_plugin.TextCommand):
def run(self, edit):
# line = self.view.window().run_command('parse_line')
# print(line)
pass
h = HorizontalTask()
h.pp()
class MoveTaskCommand(sublime_plugin.TextCommand):
def run(self, edit, is_add_date = False):
loc = self.view.sel()[0]
line_pos = self.view.line(loc)
line_text = self.view.substr(line_pos).strip()
pos = line_text.find('>')
target = line_text[:pos]
m = re.search('^\d+$',target)
if m:
times = int(m.group(0))
elif (target == 'end'):
times = 99
elif (target == '~'):
self.view.window().run_command('prompt_move_task')
return
else:
times = 99
self.view.run_command("move_task_batch", {'times': times, 'is_add_date': is_add_date, 'stop_before': True })
class PromptMoveTaskCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel('Move Down', '', self.on_done, None, None)
pass
def on_done(self, value):
self.window.run_command('move_task_batch', { 'times': int(value), 'is_add_date': True } )
class GoToFirstLine(sublime_plugin.TextCommand):
def run(self, edit, pos = 0):
# pos = 0
# if (position_region != 0):
# line = self.view.line(position_region)
# pos = line.a
self.view.sel().clear()
self.view.sel().add(sublime.Region(pos))
self.view.show(pos)
class MoveTaskBatchCommand(sublime_plugin.TextCommand):
def run(self, edit, times, is_add_date = False, stop_before = False):
init_loc = self.view.line(self.view.sel()[0]).a
for _ in range(times):
if (stop_before):
loc = self.view.sel()[0]
line_pos = self.view.line(loc)
next_line_pos = self.view.line(line_pos.b+1)
next_line_text = self.view.substr(next_line_pos)
if (next_line_text == '###'):
break
self.view.run_command('swap_line_down')
if ( is_add_date == True):# and times > 0):
self.view.run_command('add_date_time')
self.view.run_command('go_to_first_line', { 'pos': init_loc })#, { 'position_region': init_loc })
# http://stackoverflow.com/a/13882791
class AddDateTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
loc = self.view.sel()[0]
line_pos = self.view.line(loc)
line_text = self.view.substr(line_pos)
pos = line_text.find('|')
text = datetime.datetime.now().strftime("| %-d/%H:%M/%-m/%y ")
if (pos==-1):
pos = len(line_text)
text = " " + text.strip()
self.view.insert(edit, line_pos.a + pos, text)