-
Notifications
You must be signed in to change notification settings - Fork 0
/
aika.py
28 lines (25 loc) · 925 Bytes
/
aika.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
import sublime
import sublime_plugin
from datetime import datetime
def parse_time(s):
return datetime.strptime(s, '%H:%M')
def format_diff(diff):
s = str(diff)
splits = (s.split(':'))
return splits[0] + ':' + splits[1]
class AikaCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
# Get the selected text
s = self.view.substr(region)
# Split lines
lines = s.splitlines()
# Parse start and end times
start = parse_time(lines[0])
end = parse_time(lines[-1])
# Calculate difference
diff = end - start
diff_str = format_diff(diff)
# Replace the selection with transformed text
self.view.replace(edit, region, s + '\n\n' + diff_str)