fixed uptime in AGS sideright module

This commit is contained in:
aliqu
2024-06-20 22:46:32 +01:00
parent b72685f6a3
commit b0d802bd08
+41 -8
View File
@@ -66,14 +66,47 @@ const timeRow = Box({
Widget.Label({ Widget.Label({
hpack: 'center', hpack: 'center',
className: 'txt-small txt', className: 'txt-small txt',
setup: (self) => self setup: (self) => {
.poll(5000, label => { const getUptime = async () => {
execAsync(['bash', '-c', `uptime -p | sed -e 's/...//;s/ day\\| days/d/;s/ hour\\| hours/h/;s/ minute\\| minutes/m/;s/,[^,]*//2'`]) try {
.then(upTimeString => { await execAsync(['bash', '-c', 'uptime -p']);
label.label = `Uptime ${upTimeString}`; return execAsync(['bash', '-c', `uptime -p | sed -e 's/...//;s/ day\\| days/d/;s/ hour\\| hours/h/;s/ minute\\| minutes/m/;s/,[^,]*//2'`]);
}).catch(print); } catch {
}) return execAsync(['bash', '-c', 'uptime']).then(output => {
, const uptimeRegex = /up\s+((\d+)\s+days?,\s+)?((\d+):(\d+)),/;
const matches = uptimeRegex.exec(output);
if (matches) {
const days = matches[2] ? parseInt(matches[2]) : 0;
const hours = matches[4] ? parseInt(matches[4]) : 0;
const minutes = matches[5] ? parseInt(matches[5]) : 0;
let formattedUptime = '';
if (days > 0) {
formattedUptime += `${days} d `;
}
if (hours > 0) {
formattedUptime += `${hours} h `;
}
formattedUptime += `${minutes} m`;
return formattedUptime;
} else {
throw new Error('Failed to parse uptime output');
}
});
}
};
self.poll(5000, label => {
getUptime().then(upTimeString => {
label.label = `Uptime: ${upTimeString}`;
}).catch(err => {
console.error(`Failed to fetch uptime: ${err}`);
});
});
},
}), }),
Widget.Box({ hexpand: true }), Widget.Box({ hexpand: true }),
// ModuleEditIcon({ hpack: 'end' }), // TODO: Make this work // ModuleEditIcon({ hpack: 'end' }), // TODO: Make this work