-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·109 lines (90 loc) · 3.74 KB
/
cli.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
#!/usr/bin/env node
const si = require('systeminformation');
const asciichart = require('asciichart');
const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const boxen = require('boxen');
const beeper = require('beeper');
const cliCursor = require('cli-cursor');
const notifierText = '■';
const updateCheckInterval = 0;
const maxValueOfArray = 60;
const maxBeepCount = 6;
let totalDownload = [0];
let totalUpload = [0];
let totalDownloadSize = 0;
let totalUploadSize = 0;
let defaultNetwork;
let beepCount = 1;
cliCursor.hide();
const chartConfig = {
colors: [
asciichart.lightblue,
asciichart.green,
],
height: 10,
min: 1,
};
const pkg = require('./package.json');
const notifier = updateNotifier({
pkg: {
name: pkg.name,
version: pkg.version,
},
isGlobal: true,
shouldNotifyInNpmScript: true,
updateCheckInterval,
});
async function init() {
try {
defaultNetwork = await si.networkInterfaceDefault();
const networkInterfaces = await si.networkInterfaces();
defaultNetwork = networkInterfaces.find(networkInterface => networkInterface.iface === defaultNetwork);
} catch (error) {
console.error('Error fetching network information:', error);
}
}
async function updateNetworkStats() {
try {
const data = await si.networkStats();
if (totalDownload.length > maxValueOfArray) {
totalDownload.shift();
totalUpload.shift();
}
totalDownload.push((data[0].rx_sec / (1024 * 1024)).toFixed(2));
totalUpload.push((data[0].tx_sec / (1024 * 1024)).toFixed(2));
totalDownloadSize += data[0].rx_sec;
totalUploadSize += data[0].tx_sec;
console.clear();
console.log(' ' + chalk.blueBright(notifierText + ' download') + ' ' + chalk.green(notifierText + ' upload'));
console.log(asciichart.plot([totalDownload, totalUpload], chartConfig) + '\n');
console.log(' ' + chalk.green('▶ ' + defaultNetwork?.iface + ' ') + chalk.keyword('orange')('▶ ' + defaultNetwork?.ip4 + ' ')
+ chalk.yellowBright('▶ ' + defaultNetwork?.mac + ' ') + chalk.redBright('▶ ' + defaultNetwork?.type + ' '));
if (data[0].operstate !== 'up') {
console.log(' ' + chalk.whiteBright('Status : ') + chalk.redBright(data[0].operstate));
if (beepCount % maxBeepCount !== 0) {
beeper();
beepCount++;
}
} else {
console.log(' ' + chalk.whiteBright('Status : ') + chalk.yellowBright(data[0].operstate))
beepCount = 1;
}
console.log(' ' + chalk.blueBright('Download: ' + bytesToSize(data[0].rx_sec)) + chalk.green(' Upload: ' + bytesToSize(data[0].tx_sec)));
console.log(' ' + chalk.blueBright('Total Download: ' + bytesToSize(totalDownloadSize) + chalk.green(' Total Upload: ' + bytesToSize(totalUploadSize))));
if (notifier.update && notifier.update.latest > pkg.version) {
console.log(boxen('Update available ' + pkg.version + ' → ' + chalk.green(notifier.update.latest) + '\n' + 'Run ' + chalk.blue('npm i -g network-pocket') +
' to update after terminate network-pocket', { align: 'center', margin: { left: 7 }, borderColor: 'green' }));
}
} catch (error) {
console.error('Error updating network stats:', error);
}
}
function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
}
setInterval(updateNetworkStats, 1000);
init(); // Initialize network info