-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·306 lines (239 loc) · 8.5 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
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
#!/usr/bin/env node
const chalk = require('chalk');
const editors = require('./editors.js');
const timeUtils = require('./timeUtil.js');
const utils = require('./utils.js');
const exec = require('./promises/exec');
const fs = require('./promises/fs');
const fsOriginal = require('fs');
const home = require('os').homedir();
const codeEditorsNames = ['atom', 'subl', 'vscode', 'webstorm', 'nano', 'studio', 'idea'];
let colors = [
'bgBlack', 'bgRed', 'bgGreen',
'bgCyan', 'bgBlue', 'bgMagenta', 'bgYellow'
];
const laptop = '💻';
const boom = '💥'
const sparkles = '✨'
const ESC = '\033[';
const date = new Date().toDateString().split(' ').join('-');
let fileName = `codespell-${date}.json`;
fileName = home + '/.codespell/' + fileName;
let runningEditors;
let runningEditorNames;
try {
const readFileData = fsOriginal.readFileSync(fileName);
runningEditors = JSON.parse(String(readFileData));
const names = runningEditors.editors.map(editor => editor.name);
runningEditorNames = [...names];
} catch (err) {
runningEditors = {
closedEditorNames: [],
closedEditors: [],
editors: []
};
runningEditorNames = [];
}
const refreshTime = 1000;
let count = 0;
let fileDataIndex = 0;
const saveTime = 5;
let fileStore = [];
utils.hideCursor();
utils.term('\033c');
displayMetadata()
displayPast();
setInterval(execProcess, refreshTime);
function execProcess() {
count++;
codeEditorsNames.forEach(execAndAdd);
display();
if (!(count % saveTime) && runningEditors.editors.length > 0) {
save(runningEditors);
}
}
function execAndAdd(codeEditor, index) {
exec(`ps ax | grep ${codeEditor} -c`)
.then((stdout, stderr) => {
let data = String(stdout).trim();
return data;
})
.then((data) => {
addEditor(data, codeEditor, index);
})
.catch(errCallback);
}
function addEditor(data, codeEditor, index) {
let count = parseInt(data);
if (count > 2) {
// Process exists
if (runningEditorNames.indexOf(codeEditor) === -1) {
runningEditors.editors.push({
name: codeEditor
});
runningEditorNames.push(codeEditor);
} else {
addResult(codeEditor);
}
} else {
// Check if this still exists in editors because of time lag
if (runningEditorNames.indexOf(codeEditor) !== -1) {
let runningEditor = runningEditors.editors.find((editor) => {
return editor.name === codeEditor;
});
let runningEditorIndex = runningEditors.editors.findIndex((editor) => {
return editor.name === codeEditor;
});
deleteClosed(runningEditor, runningEditorIndex);
return;
}
}
}
function addResult(codeEditor) {
if (codeEditor === "vscode") {
codeEditor = "code";
}
exec(`ps -eo comm,etime | grep ${codeEditor} | head -1`)
.then((stdout, stderr) => {
if (codeEditor === "code") {
codeEditor = "vscode";
}
let timeString = String(stdout).trim().match(/\d{1,3}/g);
let time;
let runningEditor = runningEditors.editors.find((editor) => {
return editor.name === codeEditor;
});
let runningEditorIndex = runningEditors.editors.findIndex((editor) => {
return editor.name === codeEditor;
});
if (!timeString) {
deleteClosed(runningEditor, runningEditorIndex);
return;
};
// Check if this editor was closed
if (runningEditors.closedEditorNames.indexOf(codeEditor) !== -1) {
let closedEditor = runningEditors.closedEditors.find((editor) => {
return editor.name === codeEditor
});
let closedEditorIndex = runningEditors.closedEditors
.findIndex((editor) => {
return editor.name === codeEditor
});
let finalTime = timeUtils.addTime(closedEditor.time, timeString.join(":"));
runningEditor["time"] = timeUtils.formatTime(finalTime);
runningEditors.closedEditors = utils
.deleteItem(runningEditors.closedEditors, closedEditorIndex);
runningEditors.closedEditorNames = utils
.deleteItem(runningEditors.closedEditorNames, closedEditorIndex);
} else {
time = runningEditor.time ?
timeUtils.incrementTime(runningEditor.time) :
timeString;
runningEditor['time'] = timeUtils.formatTime(time);
}
});
}
function display() {
utils.term('\033c');
// Metadata gone. Print again.
displayMetadata();
displayPast(true);
runningEditors.editors.forEach((editor, index) => {
const name = editor.name;
const fullName = chalk[colors[index * 2]].white(editors[name]) + laptop;
if (!editor.time) {
(index === 0) ? utils.term('\nLoading..'): utils.term('Loading..');
return;
} else {
utils.hideCursor();
const time = chalk.yellow(editor.time);
const escapeString = '\033[' + (5 + fileDataIndex + index) + ';0f';
utils.term(`${escapeString} ${fullName}: ${time} ${sparkles}`);
}
});
}
function displayPast(flag) {
const date = new Date();
const today = date.toDateString();
const lastDay = new Date(date.setDate(date.getDate() - 1))
.toDateString()
.split(' ')
.join('-');
if (!flag) {
let fileName = `codespell-${lastDay}.json`;
fileName = home + '/.codespell/' + fileName;
let data;
let fileData;
try {
data = fsOriginal.readFileSync(fileName);
fileData = JSON.parse(String(data))
} catch (readError) {
if(readError) return;
}
utils.term(`${ESC}2;0f${chalk.bgGreen(lastDay).split('-').join(' ')}`);
fileData.closedEditors.forEach((entry, index) => {
const name = chalk[randomColor()].white(editors[entry.name]);
const time = chalk.blue(entry.time);
chalk.black
utils.term(`${ESC}${4 + index};2f${name} ${laptop}: ${time} ${boom}`);
});
fileDataIndex = fileData.closedEditors.length * 2;
utils.term(`${ESC}${3 + fileDataIndex};0f${chalk.bgRed(today)}`);
fileStore = [...fileData.closedEditors];
} else {
utils.term(`${ESC}2;0f${chalk.bgGreen(lastDay).split('-').join(' ')}`);
fileStore.forEach((entry, index) => {
const name = chalk[randomColor()].white(editors[entry.name]);
const time = chalk.blue(entry.time);
utils.term(`${ESC}${4 + index};2f${name} ${laptop}: ${time} ${boom}`);
});
utils.term(`${ESC}${4 + fileDataIndex};0f${chalk.bgRed(today)}`);
}
}
function displayMetadata() {
const title = chalk.bgBlue.white('CodeSpell');
exec('tput cols')
.then((stdout, stderr) => {
const columns = parseInt(String(stdout));
utils.term(`${ESC}` + '1;' + (Math.floor(columns / 2) - 3) + 'f' + title);
})
.catch(errCallback);
};
function save(codeEditors, index) {
utils.saveFile(fileName, JSON.stringify(codeEditors, null, 4));
}
function errCallback(err) {
console.log(err)
}
function randomColor() {
return colors[Math.floor(Math.random() * 7)];
}
function deleteClosed(runningEditor, runningEditorIndex) {
runningEditors.closedEditors.push(runningEditor);
runningEditors.closedEditorNames.push(runningEditor.name);
runningEditors.editors = utils
.deleteItem(runningEditors.editors, runningEditorIndex)
runningEditorNames = utils
.deleteItem(runningEditorNames, runningEditorIndex);
return;
}
/**
* https://stackoverflow.com/a/14861513/2231031
*/
if (process.platform === 'win32') {
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('SIGINT', function () {
process.emit('SIGINT');
});
}
process.on('SIGINT', function () {
// Clear console
console.log('\033c');
const names = runningEditors.editors.map(editor => editor.name);
runningEditors.closedEditorNames.push(...names);
runningEditors.closedEditors.push(...runningEditors.editors);
process.exit();
});