Added live clock, better dates, free disk, free ram, local ip address, username

This commit is contained in:
snxraven 2021-09-23 21:00:09 -04:00
parent 9c8004580a
commit fb755a02a5
1 changed files with 40 additions and 20 deletions

60
main.js
View File

@ -2,44 +2,58 @@
var lb = require("lemonbar");
var wifi = require('node-wifi');
const monitor = require('node-active-window');
var address = require('network-address')
const jsonfile = require('jsonfile')
const battery = require("battery");
const fs = require('fs');
const os = require('os');
const date = require('date-and-time');
var free = require('freem');
const checkDiskSpace = require('check-disk-space').default
function cpuUsage(cb, core, sampleMs) {
var deltaUsed;
var deltaIdle;
var timesKeys = ["user", "nice", "sys", "irq"];
var allCores = null === core || !(core > -1);
var byCore = (cpu, i) => allCores || core === i;
var bySampleKey = (sample, key) => sample.filter(byCore).reduce((sum, cpu) => sum + cpu.times[key], 0);
var sample0 = os.cpus();
setTimeout(function() {
setTimeout(function () {
var sample1 = os.cpus();
deltaUsed = timesKeys.reduce(
(diff, key) => diff + bySampleKey(sample1, key) - bySampleKey(sample0, key), 0);
deltaIdle = bySampleKey(sample1, "idle") - bySampleKey(sample0, "idle");
if ("function" === typeof cb) {
cb(100 * (deltaUsed / (deltaUsed + deltaIdle)));
}
}, sampleMs || 1000);
}
}
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
let date_ob = new Date();
process.on('uncaughtException', err => {
console.error('There was an uncaught error', err)
process.exit(1) //mandatory (as per the Node.js docs)
})
// The cache file
const file = '/home/raven/Documents/cache.json'
// Launching Main PID for LemonBar
@ -65,7 +79,11 @@ lb.launch({
// This must be async for battery level and charging detection
async function update() {
const now = new Date();
const pattern = date.compile('ddd, DD MMM YYYY');
// The cache file
const { level, charging } = await battery();
// Lets check our cache files mtime to see if we need to check the current SSID yet
@ -99,9 +117,11 @@ async function update() {
// .then(cpuPercentage => {
// lb.append(lb.left + "RavenScott | CPU: ".lbFg("#fff") + cpuPercentage.toString().lbFg("#fff"))
// })
cpuUsage(perc => lb.append(lb.left + "RavenScott | CPU: ".lbFg("#fff") + (Math.round(perc * 100) / 100).toFixed(2).lbFg("#fff")));
checkDiskSpace('/').then((diskSpace) => {
free.m(function (err, freeRamMB) {
cpuUsage(perc => lb.append(lb.left + require("os").userInfo().username.lbFg("#fff") + " | CPU: ".lbFg("#fff") + (Math.round(perc * 100) / 100).toFixed(2).lbFg("#fff") + " | Mem: ".lbFg("#fff") + freeRamMB[0].free.lbFg("#fff") + " MB".lbFg("#fff") + " | ".lbFg("#fff") + formatBytes(diskSpace.free).toString().lbFg("#fff") + " of ".lbFg("#fff") + formatBytes(diskSpace.size).toString().lbFg("#fff") ));
});
})
// Check the cache SSID
jsonfile.readFile(file, function (err, obj) {
if (err) console.error(err)
@ -110,10 +130,10 @@ async function update() {
// If charging show its status, else, show nothing for charging
if (charging) {
lb.append(lb.right + (" " + new Date().toDateString().lbFg("#fff") + " | BAT: ".lbFg("#fff") + level.toString().lbFg("#fff") + " (Charging)".lbFg("#fff") + " " + "| WIFI: ".lbFg("#fff") + wifiSSID.ssid.lbFg("#fff")).lbSwap)
lb.append(lb.right + (date.format(now, 'hh:mm:ss').toString.lbFg("#fff") + " | ".lbFg("#fff") + date.format(now, pattern).toString().lbFg("#fff") + " | BAT: ".lbFg("#fff") + level.toString().lbFg("#fff") + " (Charging)".lbFg("#fff") + " " + "| WIFI: ".lbFg("#fff") + wifiSSID.ssid.lbFg("#fff") + " | IP: ".lbFg("#fff") + address().lbFg("#fff")).lbSwap)
} else {
lb.append(lb.right + (" " + new Date().toDateString().lbFg("#fff") + " | BAT: ".lbFg("#fff") + level.toString().lbFg("#fff") + " " + "| WIFI: ".lbFg("#fff") + wifiSSID.toString().lbFg("#fff")).lbSwap)
lb.append(lb.right + (date.format(now, 'hh:mm:ss').toString().lbFg("#fff") + " | ".lbFg("#fff") + date.format(now, pattern).toString().lbFg("#fff") + " | BAT: ".lbFg("#fff") + level.toString().lbFg("#fff") + " " + "| WIFI: ".lbFg("#fff") + wifiSSID.toString().lbFg("#fff") + " | IP: ".lbFg("#fff") + address().lbFg("#fff")).lbSwap)
}
monitor.getActiveWindow((err, window) => {