Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ in
bar.customModules.cpu.leftClick = mkStrOption "";
bar.customModules.cpu.middleClick = mkStrOption "";
bar.customModules.cpu.pollingInterval = mkIntOption 2000;
bar.customModules.cpu.historyLength = mkIntOption 10;
bar.customModules.cpu.showGraph = mkBoolOption false;
bar.customModules.cpu.rightClick = mkStrOption "";
bar.customModules.cpu.round = mkBoolOption true;
bar.customModules.cpu.scrollDown = mkStrOption "";
Expand Down Expand Up @@ -247,6 +249,8 @@ in
bar.customModules.ram.icon = mkStrOption "";
bar.customModules.ram.label = mkBoolOption true;
bar.customModules.ram.labelType = mkStrOption "percentage";
bar.customModules.ram.historyLength = mkIntOption 10;
bar.customModules.ram.showGraph = mkBoolOption false;
bar.customModules.ram.leftClick = mkStrOption "";
bar.customModules.ram.middleClick = mkStrOption "";
bar.customModules.ram.pollingInterval = mkIntOption 2000;
Expand Down
50 changes: 47 additions & 3 deletions src/components/bar/modules/cpu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,62 @@
import { Astal } from 'astal/gtk3';
import { BarBoxChild } from 'src/lib/types/bar.types';

const { label, round, leftClick, rightClick, middleClick, scrollUp, scrollDown, pollingInterval, icon } =
options.bar.customModules.cpu;
const {
label,
round,
leftClick,
rightClick,
middleClick,
scrollUp,
scrollDown,
pollingInterval,
showGraph,
historyLength,
icon,
} = options.bar.customModules.cpu;

export const cpuUsage = Variable(0);
const cpuHistory = Variable<number[]>([]);

const historyLengthBinding = Variable.derive([bind(historyLength)], (length: number) => length);

const cpuPoller = new FunctionPoller<number, []>(cpuUsage, [bind(round)], bind(pollingInterval), computeCPU);

cpuPoller.initialize('cpu');

const getBarGraph = (usage: number): string => {
if (usage < 12.5) return '▁';
if (usage < 25) return '▂';
if (usage < 37.5) return '▃';
if (usage < 50) return '▄';
if (usage < 62.5) return '▅';
if (usage < 75) return '▆';
if (usage < 87.5) return '▇';
return '█';
};

const graphBinding = showGraph
? Variable.derive([bind(cpuUsage), historyLengthBinding()], (cpuUsg: number, hstLength: number) => {
const history = cpuHistory.get();
history.push(cpuUsg);
if (history.length > hstLength) {
history.shift();
}
cpuHistory.set(history);

return history.map(getBarGraph).join('');
})
: Variable.derive([], () => '');

export const Cpu = (): BarBoxChild => {
const labelBinding = Variable.derive([bind(cpuUsage), bind(round)], (cpuUsg: number, round: boolean) => {
const percentageBinding = Variable.derive([bind(cpuUsage), bind(round)], (cpuUsg: number, round: boolean) => {

Check failure on line 58 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[bind(cpuUsage),·bind(round)],` with `⏎········[bind(cpuUsage),·bind(round)],⏎·······`

Check failure on line 58 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[bind(cpuUsage),·bind(round)],` with `⏎········[bind(cpuUsage),·bind(round)],⏎·······`
return round ? `${Math.round(cpuUsg)}%` : `${cpuUsg.toFixed(2)}%`;

Check failure on line 59 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`

Check failure on line 59 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`
});

Check failure on line 60 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `}` with `····},⏎····`

Check failure on line 60 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `}` with `····},⏎····`

const labelBinding = Variable.derive([percentageBinding(), graphBinding()], (percentage: string, graph: string) => {

Check failure on line 62 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[percentageBinding(),·graphBinding()],` with `⏎········[percentageBinding(),·graphBinding()],⏎·······`

Check failure on line 62 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[percentageBinding(),·graphBinding()],` with `⏎········[percentageBinding(),·graphBinding()],⏎·······`
return showGraph ? `${percentage} ${graph}` : percentage;

Check failure on line 63 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`

Check failure on line 63 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`
});

Check failure on line 64 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `}` with `····},⏎····`

Check failure on line 64 in src/components/bar/modules/cpu/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `}` with `····},⏎····`

const cpuModule = Module({
textIcon: bind(icon),
label: labelBinding(),
Expand Down Expand Up @@ -48,6 +90,8 @@
});
},
onDestroy: () => {
percentageBinding.drop();
graphBinding.drop();
labelBinding.drop();
},
},
Expand Down
38 changes: 35 additions & 3 deletions src/components/bar/modules/ram/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import { BarBoxChild, ResourceLabelType } from 'src/lib/types/bar.types';
import { GenericResourceData } from 'src/lib/types/customModules/generic.types';

const { label, labelType, round, leftClick, rightClick, middleClick, pollingInterval, icon } =
const { label, labelType, round, leftClick, rightClick, middleClick, pollingInterval, showGraph, historyLength, icon } =

Check failure on line 12 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `·label,·labelType,·round,·leftClick,·rightClick,·middleClick,·pollingInterval,·showGraph,·historyLength,·icon·}·=⏎···` with `⏎····label,⏎····labelType,⏎····round,⏎····leftClick,⏎····rightClick,⏎····middleClick,⏎····pollingInterval,⏎····showGraph,⏎····historyLength,⏎····icon,⏎}·=`

Check failure on line 12 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `·label,·labelType,·round,·leftClick,·rightClick,·middleClick,·pollingInterval,·showGraph,·historyLength,·icon·}·=⏎···` with `⏎····label,⏎····labelType,⏎····round,⏎····leftClick,⏎····rightClick,⏎····middleClick,⏎····pollingInterval,⏎····showGraph,⏎····historyLength,⏎····icon,⏎}·=`
options.bar.customModules.ram;

const defaultRamData: GenericResourceData = { total: 0, used: 0, percentage: 0, free: 0 };
const ramUsage = Variable<GenericResourceData>(defaultRamData);
const ramHistory = Variable<number[]>([]);

const historyLengthBinding = Variable.derive([bind(historyLength)], (length: number) => length);

const ramPoller = new FunctionPoller<GenericResourceData, [Variable<boolean>]>(
ramUsage,
Expand All @@ -25,19 +28,46 @@

ramPoller.initialize('ram');

const getBarGraph = (usage: number): string => {
if (usage < 12.5) return '▁';
if (usage < 25) return '▂';
if (usage < 37.5) return '▃';
if (usage < 50) return '▄';
if (usage < 62.5) return '▅';
if (usage < 75) return '▆';
if (usage < 87.5) return '▇';
return '█';
};

const graphBinding = showGraph
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use conditionals like this because once showGraph changes, it won't be tracked unless put in a binding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do i fix this?

? Variable.derive([bind(ramUsage), historyLengthBinding()], (rmUsg: GenericResourceData, hstLength: number) => {

Check failure on line 43 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[bind(ramUsage),·historyLengthBinding()],` with `⏎··········[bind(ramUsage),·historyLengthBinding()],⏎·········`

Check failure on line 43 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `[bind(ramUsage),·historyLengthBinding()],` with `⏎··········[bind(ramUsage),·historyLengthBinding()],⏎·········`
const history = ramHistory.get();

Check failure on line 44 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `··········` with `··············`

Check failure on line 44 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Replace `··········` with `··············`
history.push(rmUsg.percentage);

Check failure on line 45 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`

Check failure on line 45 in src/components/bar/modules/ram/index.tsx

View workflow job for this annotation

GitHub Actions / code_quality

Insert `····`
if (history.length > hstLength) {
history.shift();
}
ramHistory.set(history);

return history.map(getBarGraph).join('');
})
: Variable.derive([], () => '');

export const Ram = (): BarBoxChild => {
const labelBinding = Variable.derive(
[bind(ramUsage), bind(labelType), bind(round)],
(rmUsg: GenericResourceData, lblTyp: ResourceLabelType, round: boolean) => {
const returnValue = renderResourceLabel(lblTyp, rmUsg, round);

return returnValue;
},
);

const combinedLabelBinding = Variable.derive([labelBinding(), graphBinding()], (label: string, graph: string) => {
return showGraph ? `${label} ${graph}` : label;
});

const ramModule = Module({
textIcon: bind(icon),
label: labelBinding(),
label: combinedLabelBinding(),
tooltipText: bind(labelType).as((lblTyp) => {
return formatTooltip('RAM', lblTyp);
}),
Expand Down Expand Up @@ -78,6 +108,8 @@
},
onDestroy: () => {
labelBinding.drop();
graphBinding.drop();
combinedLabelBinding.drop();
},
},
});
Expand Down
18 changes: 18 additions & 0 deletions src/components/bar/settings/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export const CustomModuleSettings = (): JSX.Element => {
max={60 * 24 * 1000}
increment={1000}
/>
<Option
opt={options.bar.customModules.ram.historyLength}
title="History Length"
type="number"
min={1}
max={30}
increment={1}
/>
<Option opt={options.bar.customModules.ram.showGraph} title="Show Graph" type="boolean" />
<Option opt={options.bar.customModules.ram.leftClick} title="Left Click" type="string" />
<Option opt={options.bar.customModules.ram.rightClick} title="Right Click" type="string" />
<Option opt={options.bar.customModules.ram.middleClick} title="Middle Click" type="string" />
Expand All @@ -109,6 +118,15 @@ export const CustomModuleSettings = (): JSX.Element => {
max={60 * 24 * 1000}
increment={1000}
/>
<Option
opt={options.bar.customModules.cpu.historyLength}
title="History Length"
type="number"
min={1}
max={30}
increment={1}
/>
<Option opt={options.bar.customModules.cpu.showGraph} title="Show Graph" type="boolean" />
<Option opt={options.bar.customModules.cpu.leftClick} title="Left Click" type="string" />
<Option opt={options.bar.customModules.cpu.rightClick} title="Right Click" type="string" />
<Option opt={options.bar.customModules.cpu.middleClick} title="Middle Click" type="string" />
Expand Down
4 changes: 4 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,8 @@ const options = mkOptions({
labelType: opt<ResourceLabelType>('percentage'),
round: opt(true),
pollingInterval: opt(2000),
historyLength: opt(10),
showGraph: opt(false),
leftClick: opt(''),
rightClick: opt(''),
middleClick: opt(''),
Expand All @@ -1099,6 +1101,8 @@ const options = mkOptions({
label: opt(true),
round: opt(true),
pollingInterval: opt(2000),
historyLength: opt(10),
showGraph: opt(false),
leftClick: opt(''),
rightClick: opt(''),
middleClick: opt(''),
Expand Down