-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·85 lines (70 loc) · 1.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
#!/usr/bin/env node
var fs = require('fs');
var os = require('os');
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
function clearTerminal() {
process.stdout.write('\033c');
}
var FILENAME = getUserHome() + '/.cao_data.json';
var text = process.argv.slice(2)[0];
if (!text) {
// show all cnms
try {
var data = JSON.parse(fs.readFileSync(FILENAME, 'utf-8'));
var newData = data.map(function(item) {
return {
body: item.body,
time: item.time,
showTime: getShowTime(parseInt(item.time), 10)
}
}).sort(function(a, b) {
// 降序
return b.time - a.time;
});
var result = [];
for (var i = 0; i < newData.length; i ++) {
result.push(newData[i].showTime + newData[i].body);
}
console.log(result.join(os.EOL));
} catch(e) {
console.log('cao anything you want.');
// no file yet
}
}
if (text) {
var data = [];
try {
var json = fs.readFileSync(FILENAME, 'utf-8');
data = JSON.parse(json);
} catch (e) {
}
data.push({
body: text,
time: Date.now()
});
fs.writeFileSync(FILENAME, JSON.stringify(data), 'utf-8');
clearTerminal();
}
function getShowTime(unixTime) {
try {
var date = new Date(unixTime);
var year = date.getFullYear();
var month = date.getMonth() + 1 + '';
var day = date.getDate() + '';
var hour = date.getHours() + '';
var minutes = ('0' + date.getMinutes()).slice(-2);
var showDate =`${year}/${month}/${day} ${hour}:${minutes}`;
// 补全到20字符
var length = 20 - showDate.length;
if (length > 0) {
for (var i = 0; i < length; i++) {
showDate += ' ';
}
}
return showDate;
} catch (e) {
return;
}
}