From 626a34be3e6986d58ca4b7711f899a85b741022b Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Wed, 4 Sep 2024 08:17:19 -0400 Subject: [PATCH] feat: add periodic refresh for battery info command --- extensions/system-info/src/index.ts | 162 ++++++++++++------------ extensions/system-info/src/mac-ioreg.ts | 15 --- 2 files changed, 82 insertions(+), 95 deletions(-) diff --git a/extensions/system-info/src/index.ts b/extensions/system-info/src/index.ts index c47254b..c9a50af 100644 --- a/extensions/system-info/src/index.ts +++ b/extensions/system-info/src/index.ts @@ -116,92 +116,94 @@ async function parseBatteryInfo( }) } -class ExtensionTemplate extends WorkerExtension { - async load() { - const platform = await os.platform() - - const batteries = await sysInfo.batteries() - console.log(await sysInfo.cpus()) - const sections: List.Section[] = await parseBatteryInfo(batteries) - if (platform === "macos") { - // mac is expected to have only one battery - const macInfo = await getMacBatteryInfo() - if (macInfo) { - sections[0].items = [ - new List.Item({ - title: "Percentage", - value: "percentage", - subTitle: `${macInfo.CurrentCapacity.toString()}%`, - icon: new Icon({ - type: IconEnum.Iconify, - value: "ic:outline-percentage" - }) - }), - new List.Item({ - title: "Time Remaining", - value: "time-remaining", - subTitle: macInfo.timeRemainingFormatted, - icon: new Icon({ - type: IconEnum.Iconify, - value: "mdi:clock-outline" - }) - }), - new List.Item({ - title: "Power Source", - value: "power-source", - subTitle: macInfo.formattedPowerSource, - icon: new Icon({ - type: IconEnum.Iconify, - value: "ic:outline-power" - }) - }), - new List.Item({ - title: "Condition", - value: "condition", - subTitle: macInfo.formattedCondition, - icon: new Icon({ - type: IconEnum.Iconify, - value: "emojione:battery" - }) - }), - new List.Item({ - title: "Charge", - value: "charge", - subTitle: macInfo.formattedCurrentCapacity, - icon: new Icon({ - type: IconEnum.Iconify, - value: "emojione:battery" - }) - }), - new List.Item({ - title: "Power Usage", - value: "power-usage", - subTitle: macInfo.powerUsage, - icon: new Icon({ - type: IconEnum.Iconify, - value: "emojione:battery" - }) - }), - ...sections[0].items - ] - } +async function getBatteryInSections(): Promise { + const platform = await os.platform() + const batteries = await sysInfo.batteries() + const sections: List.Section[] = await parseBatteryInfo(batteries) + if (platform === "macos") { + // mac is expected to have only one battery + const macInfo = await getMacBatteryInfo() + if (macInfo) { + sections[0].items = [ + new List.Item({ + title: "Percentage", + value: "percentage", + subTitle: `${macInfo.CurrentCapacity.toString()}%`, + icon: new Icon({ + type: IconEnum.Iconify, + value: "ic:outline-percentage" + }) + }), + new List.Item({ + title: "Time Remaining", + value: "time-remaining", + subTitle: macInfo.timeRemainingFormatted, + icon: new Icon({ + type: IconEnum.Iconify, + value: "mdi:clock-outline" + }) + }), + new List.Item({ + title: "Power Source", + value: "power-source", + subTitle: macInfo.formattedPowerSource, + icon: new Icon({ + type: IconEnum.Iconify, + value: "ic:outline-power" + }) + }), + new List.Item({ + title: "Condition", + value: "condition", + subTitle: macInfo.formattedCondition, + icon: new Icon({ + type: IconEnum.Iconify, + value: "emojione:battery" + }) + }), + new List.Item({ + title: "Charge", + value: "charge", + subTitle: macInfo.formattedCurrentCapacity, + icon: new Icon({ + type: IconEnum.Iconify, + value: "emojione:battery" + }) + }), + new List.Item({ + title: "Power Usage", + value: "power-usage", + subTitle: macInfo.powerUsage, + icon: new Icon({ + type: IconEnum.Iconify, + value: "emojione:battery" + }) + }), + ...sections[0].items + ] } - ui.setSearchBarPlaceholder("Search...") - return ui.render( + } + return sections +} + +async function run() { + getBatteryInSections().then((sections) => { + ui.render( new List.List({ sections }) ) - } - - onSearchTermChange(term: string): Promise { - console.log("Search term changed to:", term) - return Promise.resolve() - } + }) +} - onItemSelected(value: string): Promise { - console.log("Item selected:", value) - return Promise.resolve() +class ExtensionTemplate extends WorkerExtension { + load() { + ui.setSearchBarPlaceholder("Search...") + setInterval(() => { + console.log("Battery info updated") + run() + }, 10_000) + return run() } } diff --git a/extensions/system-info/src/mac-ioreg.ts b/extensions/system-info/src/mac-ioreg.ts index 5a794b0..2b27151 100644 --- a/extensions/system-info/src/mac-ioreg.ts +++ b/extensions/system-info/src/mac-ioreg.ts @@ -26,7 +26,6 @@ export async function getMacBatteryInfo() { const batteryInfoStdout = batteryInfoRet.stdout const ioreg: any = await utils.plist.parse(batteryInfoStdout) - console.log(ioreg) // check if ioreg is an array if (!Array.isArray(ioreg)) { @@ -48,20 +47,6 @@ export async function getMacBatteryInfo() { AdapterDetails, Temperature } = batteryInfo - console.log({ - TimeRemaining, - Voltage, - Amperage, - PermanentFailureStatus, - AppleRawCurrentCapacity, - CurrentCapacity, - AppleRawMaxCapacity, - MaxCapacity, - CycleCount, - ExternalConnected, - AdapterDetails, - Temperature - }) /* -------------------------------------------------------------------------- */ /* Time Remaining */ /* -------------------------------------------------------------------------- */