-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (109 loc) · 3.97 KB
/
index.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
135
136
const fs = require('fs');
const ProgressBar = require('progressbar');
const colors = require('colors');
const files = fs.readdirSync(__dirname).filter(x => x.endsWith('.md')
|| x.endsWith('.mdown')
|| x.endsWith('.markdown')
&&
(!x.includes('INSTR.md') || !x.includes('CHANGELOG.md')));
let count = (input, occur) => {
return input.split(occur).length - 1;
}
const headerMode = require('./modes.json').header_mode;
const displayFilename = require('./modes.json').display_filename;
files.forEach((element) => {
fs.readFile(element, 'utf8', (err, data) => {
if (err) {
console.error('Error via reading file:', err);
return;
}
const lines = data.split('\n');
let totalTasks = 0;
let completedWeightedTasks = 0;
let totalWeight = 0;
let currentHeader = '';
let headerProgress = new Map();
lines.forEach(line => {
if (line.startsWith('## ')) {
currentHeader = line.slice(3).trim();
headerProgress.set(currentHeader, { totalTasks: 0, completedWeightedTasks: 0, totalWeight: 0 });
}
if (line.includes('- [')) {
if (headerMode) {
headerProgress.get(currentHeader).totalTasks++;
} else {
totalTasks++;
}
let weight = 1;
const match = line.match(/\{([\d.]+)(?:[*+]|)/);
let tabs = count(line, ' ');
if (require('./modes.json').hierarchy_mode == false)
tabs = 0;
let coef = 1 + tabs;
if (match) {
const weightStr = match[1];
if (weightStr === '*') {
weight = 2;
} else {
weight = parseFloat(weightStr);
}
if (headerMode) {
headerProgress.get(currentHeader).totalWeight += weight / coef;
} else {
totalWeight += weight / coef;
}
} else {
if (headerMode) {
headerProgress.get(currentHeader).totalWeight += (1 / coef);
} else {
totalWeight += (1 / coef);
}
}
if (line.includes('[x]')) {
if (headerMode) {
headerProgress.get(currentHeader).completedWeightedTasks += weight / coef;
} else {
completedWeightedTasks += weight / coef;
}
}
}
});
if (!headerMode) {
if (totalWeight === 0) {
console.log("There are no tasks with weights. Cannot calculate progress.");
return;
}
const progress = completedWeightedTasks / totalWeight;
const bar = ProgressBar.create();
bar.step('arrange of completion');
bar.setTotal(totalWeight);
bar.setTick(completedWeightedTasks);
let color = '';
if (progress * 100 <= 33) color = 'red';
else if (progress * 100 <= 66) color = 'yellow';
else color = 'green';
console.log(colors[color](`\nProgress: ${Math.round(progress * 100)}%`));
bar.finish();
} else {
headerProgress.forEach((value, key) => {
if (value.totalWeight === 0) {
console.log(`There are no tasks with weights under header "${key}". Cannot calculate progress.`);
return;
}
const progress = value.completedWeightedTasks / value.totalWeight;
const bar = ProgressBar.create();
bar.step(`[${key}] Progress`);
bar.setTotal(value.totalWeight);
bar.setTick(value.completedWeightedTasks);
let color = '';
if (progress * 100 <= 33) color = 'red';
else if (progress * 100 <= 66) color = 'yellow';
else color = 'green';
if(displayFilename)
console.log(colors.underline(colors.magenta(element)));
console.log(colors[color](`\n${key} Progress: ${Math.round(progress * 100)}%`));
bar.finish();
});
}
});
});