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

44
main.js
View File

@ -2,10 +2,14 @@
var lb = require("lemonbar"); var lb = require("lemonbar");
var wifi = require('node-wifi'); var wifi = require('node-wifi');
const monitor = require('node-active-window'); const monitor = require('node-active-window');
var address = require('network-address')
const jsonfile = require('jsonfile') const jsonfile = require('jsonfile')
const battery = require("battery"); const battery = require("battery");
const fs = require('fs'); const fs = require('fs');
const os = require('os'); 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) { function cpuUsage(cb, core, sampleMs) {
var deltaUsed; var deltaUsed;
@ -20,7 +24,7 @@ function cpuUsage(cb, core, sampleMs) {
var sample0 = os.cpus(); var sample0 = os.cpus();
setTimeout(function() { setTimeout(function () {
var sample1 = os.cpus(); var sample1 = os.cpus();
deltaUsed = timesKeys.reduce( deltaUsed = timesKeys.reduce(
@ -32,14 +36,24 @@ function cpuUsage(cb, core, sampleMs) {
cb(100 * (deltaUsed / (deltaUsed + deltaIdle))); cb(100 * (deltaUsed / (deltaUsed + deltaIdle)));
} }
}, sampleMs || 1000); }, 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' const file = '/home/raven/Documents/cache.json'
// Launching Main PID for LemonBar // Launching Main PID for LemonBar
@ -65,7 +79,11 @@ lb.launch({
// This must be async for battery level and charging detection // This must be async for battery level and charging detection
async function update() { async function update() {
const now = new Date();
const pattern = date.compile('ddd, DD MMM YYYY');
// The cache file
const { level, charging } = await battery(); const { level, charging } = await battery();
// Lets check our cache files mtime to see if we need to check the current SSID yet // 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 => { // .then(cpuPercentage => {
// lb.append(lb.left + "RavenScott | CPU: ".lbFg("#fff") + cpuPercentage.toString().lbFg("#fff")) // lb.append(lb.left + "RavenScott | CPU: ".lbFg("#fff") + cpuPercentage.toString().lbFg("#fff"))
// }) // })
checkDiskSpace('/').then((diskSpace) => {
cpuUsage(perc => lb.append(lb.left + "RavenScott | CPU: ".lbFg("#fff") + (Math.round(perc * 100) / 100).toFixed(2).lbFg("#fff"))); 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 // Check the cache SSID
jsonfile.readFile(file, function (err, obj) { jsonfile.readFile(file, function (err, obj) {
if (err) console.error(err) if (err) console.error(err)
@ -110,10 +130,10 @@ async function update() {
// If charging show its status, else, show nothing for charging // If charging show its status, else, show nothing for charging
if (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 { } 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) => { monitor.getActiveWindow((err, window) => {