Enhance Label module regex for better format handling and add CPU and Memory modules with dynamic updates

This commit is contained in:
Supertiger
2025-08-02 10:44:18 +01:00
parent 5f455921fd
commit 45197a34a4
5 changed files with 64 additions and 4 deletions

View File

@@ -41,10 +41,10 @@ export const createLabel = (opts: LabelOpts) => {
return "N/A"; return "N/A";
} }
const regex = /\{(\w+)\}|{:(.*?)}/g; const regex = /\{(\w+)?\}|{:(.*?)}/g;
const result = format.replace(regex, (match, key, dateFmt) => { const result = format.replace(regex, (match, key, dateFmt) => {
if (key !== undefined) { if (match === "{}" || key !== undefined) {
return data[key] !== undefined ? data[key] : match; return data[key || ""] !== undefined ? data[key || ""] : match;
} }
if (dateFmt !== undefined) { if (dateFmt !== undefined) {

View File

@@ -14,4 +14,10 @@ export const modules = {
import("./modules/temperature").then( import("./modules/temperature").then(
({ createTemperatureModule }) => createTemperatureModule ({ createTemperatureModule }) => createTemperatureModule
), ),
cpu: () =>
import("./modules/cpu").then(({ createCpuModule }) => createCpuModule),
memory: () =>
import("./modules/memory").then(
({ createMemoryModule }) => createMemoryModule
),
}; };

View File

@@ -9,7 +9,7 @@ export const createBatteryModule = (
config: WaybarConfig["battery"] config: WaybarConfig["battery"]
) => { ) => {
const label = createLabel({ const label = createLabel({
interval: 1000, interval: 2000,
update: () => update(), update: () => update(),
config: config!, config: config!,
module, module,

View File

@@ -0,0 +1,27 @@
import type { WaybarConfig } from "../configParser";
import type { Module } from "../createModule";
import { createLabel } from "../Label";
export const createCpuModule = (
module: Module,
config: WaybarConfig["cpu"]
) => {
const label = createLabel({
interval: 1000,
update: () => update(),
config: config!,
module,
});
module.element.appendChild(label.element);
const update = async () => {
const usage = Math.round(Math.random() * 100);
label.set({
usage,
});
};
update();
};

View File

@@ -0,0 +1,27 @@
import type { WaybarConfig } from "../configParser";
import type { Module } from "../createModule";
import { createLabel } from "../Label";
export const createMemoryModule = (
module: Module,
config: WaybarConfig["memory"]
) => {
const label = createLabel({
interval: 1000,
update: () => update(),
config: config!,
module,
});
module.element.appendChild(label.element);
const update = async () => {
const usage = Math.round(Math.random() * 100);
label.set({
"": usage,
});
};
update();
};