Skip to content

Instantly share code, notes, and snippets.

@linuxenko
Forked from madbence/app.js
Created October 5, 2016 14:52
Show Gist options
  • Save linuxenko/1cf901d3a080b95bd2fba99d658c039a to your computer and use it in GitHub Desktop.
Save linuxenko/1cf901d3a080b95bd2fba99d658c039a to your computer and use it in GitHub Desktop.

usage

$ node app $(i3 --get-socketpath) | lemonbar -B '#2b303b' -F '#eff1f5' -f 'Iosevka Medium:size=11' -f 'FontAwesome' -u 3 | node client
const net = require('net');
const exec = require('child_process').exec;
const os = require('os');
function run(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (err) {
return reject(err);
}
resolve(stdout.toString());
});
});
}
function format(type, payload) {
const data = payload ? JSON.stringify(payload) : '';
const length = data ? data.length : 0;
const msg = new Buffer(14 + length);
msg.write('i3-ipc', 0);
msg.writeUInt32LE(length, 6);
msg.writeUInt32LE(type, 10);
msg.write(data, 14);
return msg;
}
function parse(msg) {
const length = msg.readUInt32LE(6);
return {
type: msg.readUInt32LE(10),
length,
payload: JSON.parse(msg.slice(14, 14 + length)),
};
}
let active = 0;
let name = '';
function print() {
let str = ' %{+o}%{+u}%{B#b48ead}%{F#2b303b}%{U#debad7} ' + name + ' %{-o}%{-u}%{B-}%{U-}%{F-}%{c}';
for (let i = 0; i < 10; i++) {
str += i === active ? '◆' : '◇';
}
run('pactl list sinks | grep "Volume: fr" | awk \'{print $5}\' | head -1').then(vol => {
const load = os.loadavg()[0];
const total = os.totalmem();
const used = total - os.freemem();
const mem = used / total * 100;
str += '%{r}';
str += '%{+u}%{+o}%{B#bf616a}%{U#e29ca3}%{F#2b303b} load ' + load.toFixed(2) + ' %{B-}%{F-}%{-o}%{-u} ';
str += '%{+u}%{+o}%{B#d08770}%{U#e9a996}%{F#2b303b} mem ' + mem.toFixed(0) + '% %{B-}%{F-}%{-o}%{-u} ';
str += '%{+u}%{+o}%{A4:v+:}%{A5:v-:}%{B#a3be8c}%{U#bce996}%{F#2b303b} %{T2}\uf028%{T1} ';
vol = +vol.match(/\d+/)[0] / 100;
for (let i = 0; i < 10; i++) {
if ((i + 1) / 10 < vol) str += '━';
else if (i / 10 < vol) str += '╉';
else str += '─';
}
str += ` ${(vol * 100).toFixed(0)}%`;
return run(`date +'%Y-%m-%d %H:%M'`);
}).then(date => {
date = date.trim();
str += ' %{B-}%{F-}%{-o}%{-u}%{A}%{A}';
str += ' %{B#ebcb8b}%{F#2b303b} ' + date + ' %{B-}%{F-} '
console.log(str);
}).catch(e => console.error(e.stack));
}
const s = net.connect(process.argv[2], () => {
s.write(format(1));
s.write(format(2, ['workspace', 'window']));
let buf = new Buffer(0);
s.on('data', chunk => {
buf = Buffer.concat([buf, chunk]);
while (buf.length) {
try {
const msg = parse(buf);
console.error(msg.type);
buf = buf.slice(msg.length + 14);
switch (msg.type) {
case 2147483648:
if (msg.payload.change !== 'focus') {
continue;
}
active = msg.payload.current.num - 1;
break;
case 1:
active = msg.payload.reduce((active, curr) => curr.visible ? curr.num - 1 : active, 0);
break;
case 2147483651:
if (msg.payload.change !== 'focus') {
continue;
}
name = msg.payload.container.name;
break;
default:
console.error(msg);
continue;
}
print();
} catch (e) {
console.error(e.stack);
console.error(buf, buf.toString());
buf = new Buffer(0);
}
}
});
});
setInterval(print, 1000);
const exec = require('child_process').exec;
function run(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (err) {
return reject(err);
}
resolve(stdout.toString());
});
});
}
process.stdin.on('data', chunk => {
chunk = chunk.toString();
if (chunk.match(/v\+/)) {
console.log('v+');
return run('pactl set-sink-volume 0 +3%');
}
if (chunk.match(/v-/)) {
console.log('v-');
return run('pactl set-sink-volume 0 -3%');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment