-
Notifications
You must be signed in to change notification settings - Fork 0
/
twfzf.py
executable file
·88 lines (66 loc) · 2.31 KB
/
twfzf.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
#!/usr/bin/env python3
#
# timew-fzf
#
# A timewarrior extension to list and restart
# recently tracked intervals with the help of fzf
import os
import sys
from pyfzf.pyfzf import FzfPrompt
from plumbum.commands.processes import ProcessExecutionError
from timewreport.parser import TimeWarriorParser
def tags2key(tags):
"""A hashable string from a list of tags"""
return "|".join(sorted(tags))
def get_recent_uniquely_tagged_intervals(intervals):
# Filter out open intervals
intervals = [i for i in intervals if not i.is_open()]
tags2interval = dict()
for interval in intervals:
key = tags2key(interval.get_tags())
tags2interval[key] = interval
intervals = list(tags2interval.values())
intervals.sort(key=lambda i: i.get_start(), reverse=True)
return intervals
def get_lines_for_fzf(intervals):
"""
Get lines to feed to fzf. We used | to delimit fields in the output
which means you can not use | within your timewarrior tags if you want this to work
"""
lines = []
for interval in get_recent_uniquely_tagged_intervals(intervals):
start = interval.get_start()
end = interval.get_end()
minutes = int((end - start).seconds / 60)
tags = interval.get_tags()
# Build the tags string
tags_str = ""
for tag in tags:
# tags with spaces needs to be in quotation marks
if " " in tag:
tags_str += f'"{tag}" '
else:
tags_str += f"{tag} "
lines.append(f"{start} |{minutes:4}| {tags_str}")
return lines
try:
# Get the parser, config and intervals
parser = TimeWarriorParser(sys.stdin)
tw_config = parser.get_config()
intervals = parser.get_intervals()
config_printonly = tw_config.get_boolean("printonly", False)
# Launch fzf and get a selection
selection = FzfPrompt().prompt(get_lines_for_fzf(intervals), "--no-sort --exact")[0]
tags_selection = selection.split("|")[-1].strip()
# Put the cmd together
cmd = f"timew start {tags_selection}"
if config_printonly:
# Print it
print(cmd)
else:
# Run it
os.system(cmd)
except ProcessExecutionError:
# We get here if the user exited fzf without making a selection
# in which case we will do nothing
pass