diff --git a/.config/ags/modules/sideright/sideright.js b/.config/ags/modules/sideright/sideright.js index e671ee53..8c53d957 100644 --- a/.config/ags/modules/sideright/sideright.js +++ b/.config/ags/modules/sideright/sideright.js @@ -66,14 +66,47 @@ const timeRow = Box({ Widget.Label({ hpack: 'center', className: 'txt-small txt', - setup: (self) => self - .poll(5000, label => { - execAsync(['bash', '-c', `uptime -p | sed -e 's/...//;s/ day\\| days/d/;s/ hour\\| hours/h/;s/ minute\\| minutes/m/;s/,[^,]*//2'`]) - .then(upTimeString => { - label.label = `Uptime ${upTimeString}`; - }).catch(print); - }) - , + setup: (self) => { + const getUptime = async () => { + try { + await execAsync(['bash', '-c', 'uptime -p']); + return execAsync(['bash', '-c', `uptime -p | sed -e 's/...//;s/ day\\| days/d/;s/ hour\\| hours/h/;s/ minute\\| minutes/m/;s/,[^,]*//2'`]); + } 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 }), // ModuleEditIcon({ hpack: 'end' }), // TODO: Make this work