Implemented the ability to specify the terminal to launch btop with.

This commit is contained in:
Jas Singh
2024-07-24 22:44:35 -07:00
parent 4c603d844c
commit 7fddcf0299
2 changed files with 283 additions and 262 deletions

View File

@@ -1,291 +1,311 @@
import options from "options";
const { terminal } = options;
const Stats = () => { const Stats = () => {
const divide = ([total, free]) => free / total; const divide = ([total, free]) => free / total;
const formatSizeInGB = (sizeInKB) => const formatSizeInGB = (sizeInKB) =>
Number((sizeInKB / 1024 ** 2).toFixed(2)); Number((sizeInKB / 1024 ** 2).toFixed(2));
const cpu = Variable(0, { const cpu = Variable(0, {
poll: [ poll: [
2000, 2000,
"top -b -n 1", "top -b -n 1",
(out) => { (out) => {
if (typeof out !== "string") { if (typeof out !== "string") {
return 0; return 0;
} }
const cpuOut = out.split("\n").find((line) => line.includes("Cpu(s)")); const cpuOut = out.split("\n").find((line) => line.includes("Cpu(s)"));
if (cpuOut === undefined) { if (cpuOut === undefined) {
return 0; return 0;
} }
return divide([100, cpuOut.split(/\s+/)[1].replace(",", ".")]); return divide([100, cpuOut.split(/\s+/)[1].replace(",", ".")]);
}, },
], ],
}); });
const ram = Variable( const ram = Variable(
{ total: 0, used: 0, percentage: 0 }, { total: 0, used: 0, percentage: 0 },
{ {
poll: [ poll: [
2000, 2000,
"free", "free",
(out) => { (out) => {
if (typeof out !== "string") { if (typeof out !== "string") {
return { total: 0, used: 0, percentage: 0 }; return { total: 0, used: 0, percentage: 0 };
} }
const ramOut = out.split("\n").find((line) => line.includes("Mem:")); const ramOut = out.split("\n").find((line) => line.includes("Mem:"));
if (ramOut === undefined) { if (ramOut === undefined) {
return { total: 0, used: 0, percentage: 0 }; return { total: 0, used: 0, percentage: 0 };
} }
const [totalRam, usedRam] = ramOut const [totalRam, usedRam] = ramOut
.split(/\s+/) .split(/\s+/)
.splice(1, 2) .splice(1, 2)
.map(Number); .map(Number);
return { return {
percentage: divide([totalRam, usedRam]), percentage: divide([totalRam, usedRam]),
total: formatSizeInGB(totalRam), total: formatSizeInGB(totalRam),
used: formatSizeInGB(usedRam), used: formatSizeInGB(usedRam),
}; };
},
],
}, },
], );
},
);
const gpu = Variable(0, { const gpu = Variable(0, {
poll: [ poll: [
2000, 2000,
"gpustat --json", "gpustat --json",
(out) => { (out) => {
if (typeof out !== "string") { if (typeof out !== "string") {
return 0; return 0;
} }
try { try {
const data = JSON.parse(out); const data = JSON.parse(out);
const totalGpu = 100; const totalGpu = 100;
const usedGpu = const usedGpu =
data.gpus.reduce((acc, gpu) => acc + gpu["utilization.gpu"], 0) / data.gpus.reduce((acc, gpu) => acc + gpu["utilization.gpu"], 0) /
data.gpus.length; data.gpus.length;
return divide([totalGpu, usedGpu]); return divide([totalGpu, usedGpu]);
} catch (e) { } catch (e) {
console.error("Error getting GPU stats:", e); console.error("Error getting GPU stats:", e);
return 0; return 0;
} }
}, },
], ],
}); });
const storage = Variable( const storage = Variable(
{ total: 0, used: 0, percentage: 0 }, { total: 0, used: 0, percentage: 0 },
{ {
poll: [ poll: [
2000, 2000,
"df -B1 /", "df -B1 /",
(out) => { (out) => {
if (typeof out !== "string") { if (typeof out !== "string") {
return { total: 0, used: 0, percentage: 0 }; return { total: 0, used: 0, percentage: 0 };
} }
const dfOut = out.split("\n").find((line) => line.startsWith("/")); const dfOut = out.split("\n").find((line) => line.startsWith("/"));
if (dfOut === undefined) { if (dfOut === undefined) {
return { total: 0, used: 0, percentage: 0 }; return { total: 0, used: 0, percentage: 0 };
} }
const parts = dfOut.split(/\s+/); const parts = dfOut.split(/\s+/);
const size = parseInt(parts[1], 10); const size = parseInt(parts[1], 10);
const used = parseInt(parts[2], 10); const used = parseInt(parts[2], 10);
const sizeInGB = formatSizeInGB(size); const sizeInGB = formatSizeInGB(size);
const usedInGB = formatSizeInGB(used); const usedInGB = formatSizeInGB(used);
return { return {
total: Math.floor(sizeInGB / 1000), total: Math.floor(sizeInGB / 1000),
used: Math.floor(usedInGB / 1000), used: Math.floor(usedInGB / 1000),
percentage: divide([size, used]), percentage: divide([size, used]),
}; };
},
],
}, },
], );
},
);
return Widget.Box({ return Widget.Box({
class_name: "dashboard-card stats-container", class_name: "dashboard-card stats-container",
vertical: true,
vpack: "fill",
hpack: "fill",
expand: true,
children: [
Widget.Box({
vertical: true, vertical: true,
vpack: "fill",
hpack: "fill",
expand: true,
children: [ children: [
Widget.Box({ Widget.Box({
class_name: "stat cpu", vertical: true,
hexpand: true, children: [
vpack: "center", Widget.Box({
children: [ class_name: "stat cpu",
Widget.Button({ hexpand: true,
on_primary_click: () => { vpack: "center",
App.closeWindow("dashboardmenu"); children: [
Utils.execAsync('bash -c "kitty -e btop"').catch( Widget.Button({
(err) => `Failed to open btop: ${err}`, on_primary_click: terminal.bind("value").as(term => {
); return () => {
}, App.closeWindow("dashboardmenu");
label: "", Utils.execAsync(`bash -c "${term} -e btop"`).catch(
}), (err) => `Failed to open btop: ${err}`,
Widget.Button({ );
on_primary_click: () => { }
App.closeWindow("dashboardmenu"); }),
Utils.execAsync('bash -c "kitty -e btop"').catch( label: "",
(err) => `Failed to open btop: ${err}`, }),
); Widget.Button({
}, on_primary_click: terminal.bind("value").as(term => {
child: Widget.LevelBar({ return () => {
class_name: "stats-bar", App.closeWindow("dashboardmenu");
hexpand: true, Utils.execAsync(`bash -c "${term} -e btop"`).catch(
vpack: "center", (err) => `Failed to open btop: ${err}`,
bar_mode: "continuous", );
max_value: 1, }
value: cpu.bind("value"), }),
}), child: Widget.LevelBar({
}), class_name: "stats-bar",
], hexpand: true,
}), vpack: "center",
Widget.Label({ bar_mode: "continuous",
hpack: "end", max_value: 1,
class_name: "stat-value cpu", value: cpu.bind("value"),
label: cpu.bind("value").as((v) => `${Math.floor(v * 100)}%`), }),
}), }),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value cpu",
label: cpu.bind("value").as((v) => `${Math.floor(v * 100)}%`),
}),
],
}),
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat ram",
vpack: "center",
hexpand: true,
children: [
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
label: "",
}),
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: ram.bind("value").as((v) => v.percentage),
}),
}),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value ram",
label: ram.bind("value").as((v) => `${v.used}/${v.total} GB`),
}),
],
}),
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat gpu",
hexpand: true,
vpack: "center",
children: [
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
label: "󰢮",
}),
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: gpu.bind("value"),
}),
}),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value gpu",
label: gpu.bind("value").as((v) => `${Math.floor(v * 100)}%`),
}),
],
}),
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat storage",
hexpand: true,
vpack: "center",
children: [
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
label: "󰋊",
}),
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: storage.bind("value").as((v) => v.percentage),
}),
})
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value storage",
label: storage.bind("value").as((v) => `${v.used}/${v.total} GB`),
}),
],
}),
], ],
}), });
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat ram",
vpack: "center",
hexpand: true,
children: [
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
label: "",
}),
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: ram.bind("value").as((v) => v.percentage),
}),
}),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value ram",
label: ram.bind("value").as((v) => `${v.used}/${v.total} GB`),
}),
],
}),
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat gpu",
hexpand: true,
vpack: "center",
children: [
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
label: "󰢮",
}),
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: gpu.bind("value"),
}),
}),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value gpu",
label: gpu.bind("value").as((v) => `${Math.floor(v * 100)}%`),
}),
],
}),
Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat storage",
hexpand: true,
vpack: "center",
children: [
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
label: "󰋊",
}),
Widget.Button({
on_primary_click: () => {
App.closeWindow("dashboardmenu");
Utils.execAsync('bash -c "kitty -e btop"').catch(
(err) => `Failed to open btop: ${err}`,
);
},
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: storage.bind("value").as((v) => v.percentage),
}),
}),
],
}),
Widget.Label({
hpack: "end",
class_name: "stat-value storage",
label: storage.bind("value").as((v) => `${v.used}/${v.total} GB`),
}),
],
}),
],
});
}; };
export { Stats }; export { Stats };

View File

@@ -12,6 +12,7 @@ export const BarGeneral = () => {
Option({ opt: options.theme.font.name, title: 'Font', type: 'font' }), Option({ opt: options.theme.font.name, title: 'Font', type: 'font' }),
Option({ opt: options.theme.font.size, title: 'Font Size', type: 'string' }), Option({ opt: options.theme.font.size, title: 'Font Size', type: 'string' }),
Option({ opt: options.theme.font.weight, title: 'Font Weight', subtitle: "100, 200, 300, etc.", type: 'number' }), Option({ opt: options.theme.font.weight, title: 'Font Weight', subtitle: "100, 200, 300, etc.", type: 'number' }),
Option({ opt: options.terminal, title: 'Terminal', subtitle: "Tools such as 'btop' will open in this terminal", type: 'string' }),
] ]
}) })
} }