mirror of
https://github.com/SupertigerDev/waybar-online.git
synced 2026-03-24 08:06:37 +00:00
Enhance Label module regex for better format handling and add CPU and Memory modules with dynamic updates
This commit is contained in:
@@ -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) {
|
||||||
|
|||||||
@@ -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
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
27
src/routes/waybar/modules/cpu.ts
Normal file
27
src/routes/waybar/modules/cpu.ts
Normal 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();
|
||||||
|
};
|
||||||
27
src/routes/waybar/modules/memory.ts
Normal file
27
src/routes/waybar/modules/memory.ts
Normal 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();
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user