forked from tarnas14/vimflowy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTimeTagCounter.js
137 lines (117 loc) · 5.03 KB
/
TimeTagCounter.js
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
var timeTagCounterTimeoutReference;
let previousTimeTagCounterMsg;
const NumHoursInDay = 2;
const NumDaysInWeek = 7;
function clearTimeTagCounter(){
clearTimeout(timeTagCounterTimeoutReference);
}
function updateTimeTagCounter()
{
function applyToEachItem(functionToApply, parent) {
functionToApply(parent);
for (let child of parent.getChildren()) {
applyToEachItem(functionToApply, child)
}
}
function findMatchingItems(itemPredicate, parent) {
const matches = [];
function addIfMatch(item) {
if (itemPredicate(item)) {
matches.push(item)
}
}
applyToEachItem(addIfMatch, parent);
return matches
}
const itemHasTimeTag = item => WF.getItemNameTags(item).concat(WF.getItemNoteTags(item)).some(t => t.tag.match(/([@#])(\d+)([hm])$/i));
const getTimeTags = item => WF.getItemNameTags(item).concat(WF.getItemNoteTags(item)).filter(t => t.tag.match(/([@#])(\d+)([hm])$/i) !== null);
const matchHasTimeTag = item => item.data.search_result && item.data.search_result.matches && itemHasTimeTag(item);
function getMinutes(str) {
const m = str.match(/(\d+)([hm])/i);
return m[2] === "h" ? parseInt(m[1]) * 60 : parseInt(m[1])
}
function convertMinsToStr(mins) {
const hours = Math.floor(mins / 60);
const netMins = mins - hours * 60;
const days = (hours / 8).toFixed(2);
const weeks = (days / 5).toFixed(2);
return `${hours.toString().padStart(4," ")}h ${netMins.toString().padStart(2," ")}m | ${days.toString().padStart(2," ")} days | ${weeks.toString().padStart(2," ")} weeks`
}
function getTimeTagInfo(items) {
let hashComplete = 0,
hashInComplete = 0,
atComplete = 0,
atInComplete = 0;
items.forEach(item => {
getTimeTags(item).forEach(t => {
let tag = t.tag,
addMins = getMinutes(tag);
if (tag.startsWith("#")) {
item.isWithinCompleted() ? hashComplete += addMins : hashInComplete += addMins
} else {
item.isWithinCompleted() ? atComplete += addMins : atInComplete += addMins
}
})
});
const hashAll = hashComplete + hashInComplete,
atAll = atComplete + atInComplete;
if (atAll + hashAll === 0) return null;
hashTotals = hashAll > 0 ? `<br>#Total: \t${convertMinsToStr(hashAll)}<br> Incomplete:\t${convertMinsToStr(hashInComplete)}<br> Complete:\t${convertMinsToStr(hashComplete)}` : "";
atTotals = atAll > 0 ? `<br>@Total:\t${convertMinsToStr(atAll)}<br> Complete:\t${convertMinsToStr(atComplete)}<br> Incomplete:\t${convertMinsToStr(atInComplete)}` : "";
return `<pre>${hashTotals}<br>${atTotals}</pre>`
}
function convertTimeToStr(mins) {
const hours = Math.floor(mins / 60);
const days = (hours / NumHoursInDay).toFixed(2);
const weeks = (days / NumDaysInWeek).toFixed(2);
// return ` ${weeks.toString()} weeks (${NumDaysInWeek.toString()}d) // ${days.toString()} days (${NumHoursInDay}h) // ${hours.toString()} hrs`
return ` ${weeks.toString()} weeks // ${days.toString()} days // ${hours.toString()} hrs`
}
function getTimeTagInfoSimple(items) {
let hashComplete = 0,
hashInComplete = 0,
atComplete = 0,
atInComplete = 0;
items.forEach(item =>
{
getTimeTags(item).forEach(t =>
{
let tag = t.tag,
addMins = getMinutes(tag);
if (tag.startsWith("#"))
{
item.isWithinCompleted() ? hashComplete += addMins : hashInComplete += addMins
}
else
{
item.isWithinCompleted() ? atComplete += addMins : atInComplete += addMins
}
})
});
const hashAll = hashInComplete;
const atAll = atComplete + atInComplete;
if (atAll + hashAll === 0) return null;
hashTotals = hashAll > 0 ? `\t${convertTimeToStr(hashAll)}` : "";
return `${hashTotals}`
}
const focusedItem = WF.focusedItem();
const currentItem = WF.currentItem();
const parentItem = focusedItem
? focusedItem.equals(currentItem) ? focusedItem : focusedItem.getParent()
: currentItem;
const itemsToCount = WF.currentSearchQuery()
? findMatchingItems(matchHasTimeTag, parentItem)
: findMatchingItems(itemHasTimeTag, parentItem);
const timeTagInfo = getTimeTagInfo(itemsToCount);
const name = parentItem.getNameInPlainText();
const msg = "'" + name + "'" + " === " + getTimeTagInfoSimple(itemsToCount);
if(msg === previousTimeTagCounterMsg)
return;
previousTimeTagCounterMsg = msg;
WF.hideMessage();
clearTimeTagCounter();
if (!timeTagInfo)
WF.showMessage("No Time Tags found.".bold(), true);
else
WF.showMessage(msg.bold(), false);
};