first commit
This commit is contained in:
commit
573c74ba5b
150
main.js
Normal file
150
main.js
Normal file
@ -0,0 +1,150 @@
|
||||
// Required Modules
|
||||
var lb = require("lemonbar");
|
||||
var wifi = require('node-wifi');
|
||||
const monitor = require('node-active-window');
|
||||
const jsonfile = require('jsonfile')
|
||||
const battery = require("battery");
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
|
||||
const isNotDefined = require("is-not-defined");
|
||||
|
||||
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
lb.launch({
|
||||
lemonbar: "/usr/bin/lemonbar", // Lemonbar binary
|
||||
shell: "/bin/sh", // Shell to use for actions
|
||||
shelloutput: true, // Print shell STDOUT
|
||||
|
||||
background: "#000", // Background color (#rgb, #rrggbb, #aarrggbb)
|
||||
foreground: "#000", // Foreground color
|
||||
lineWidth: 1, lineColor: "#666", // Underline/Overline config
|
||||
geometry: { // Window geometry
|
||||
x: 0, y: 0,
|
||||
width: null, height: 25
|
||||
},
|
||||
|
||||
bottom: false, // Dock bar at bottom instead of top
|
||||
forceDocking: false, // Force docking without asking the window manager
|
||||
name: null, // Set the WM_NAME atom value for the bar
|
||||
areas: 10 // Number of clickable areas
|
||||
|
||||
})
|
||||
|
||||
// This must be async for battery level and charging detection
|
||||
async function update() {
|
||||
|
||||
const { level, charging } = await battery();
|
||||
|
||||
// Lets check our cache files mtime to see if we need to check the current SSID yet
|
||||
fs.stat(file, function (err, stats) {
|
||||
let seconds = (new Date().getTime() - stats.mtime) / 1000;
|
||||
// If the mtime for the cache is over 1 hour, lets poll for a new SSID
|
||||
if (seconds > 3600) {
|
||||
|
||||
// Init the card
|
||||
wifi.init({
|
||||
iface: null // network interface, choose a random wifi interface if set to null
|
||||
});
|
||||
|
||||
// Grab the current Wifi SSID and details
|
||||
wifi.getCurrentConnections((error, currentConnections) => {
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
// If no error, write the result to the cache
|
||||
const obj = { ssid: currentConnections[0].ssid }
|
||||
|
||||
jsonfile.writeFile(file, obj, function (err) {
|
||||
if (err) console.error(err)
|
||||
console.log("WifiCache Is Written")
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
// Start main program printing & Check CPU
|
||||
// cpu.usage()
|
||||
// .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")));
|
||||
|
||||
|
||||
// Check the cache SSID
|
||||
jsonfile.readFile(file, function (err, obj) {
|
||||
if (err) console.error(err)
|
||||
// Set a new var containing the ID
|
||||
let wifiSSID = obj.ssid
|
||||
|
||||
// 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)
|
||||
} 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)
|
||||
}
|
||||
|
||||
|
||||
monitor.getActiveWindow((err, window) => {
|
||||
if (!err) {
|
||||
lb.append((lb.center + window.app.lbFg("#fff")))
|
||||
|
||||
} else
|
||||
console.log(err)
|
||||
}); // Grab the active window and and page name
|
||||
|
||||
// Write the data to lemonbar
|
||||
lb.write()
|
||||
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
update()
|
||||
|
||||
// Update current time every second
|
||||
setTimeout(function () {
|
||||
update()
|
||||
setInterval(update, 1500)
|
||||
}, 1500 - new Date().getMilliseconds())
|
Loading…
Reference in New Issue
Block a user