Implemented strict linting standards and prettier formatting config. (#248)
* Implemented strict linting standards and prettier formatting config. * More linter fixes and type updates. * More linter updates and type fixes * Remove noisy comments * Linter and type updates * Linter, formatting and type updates. * Linter updates * Type updates * Type updates * fixed all linter errors * Fixed all linting, formatting and type issues. * Resolve merge conflicts.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { ResourceLabelType } from 'lib/types/bar';
|
||||
import { GenericResourceData } from 'lib/types/customModules/generic';
|
||||
import { GenericResourceData, Postfix } from 'lib/types/customModules/generic';
|
||||
import { InputHandlerEvents } from 'lib/types/customModules/utils';
|
||||
import { ThrottleFn, ThrottleFnCallback } from 'lib/types/utils';
|
||||
import { GtkWidget } from 'lib/types/widget';
|
||||
import { Binding } from 'lib/utils';
|
||||
import { openMenu } from 'modules/bar/utils';
|
||||
import options from 'options';
|
||||
@@ -13,14 +15,11 @@ const { scrollSpeed } = options.bar.customModules;
|
||||
|
||||
export const runAsyncCommand = (
|
||||
cmd: string,
|
||||
fn: Function,
|
||||
events: { clicked: any; event: Gdk.Event }
|
||||
fn: (output: string) => void,
|
||||
events: { clicked: Button<GtkWidget, GtkWidget>; event: Gdk.Event },
|
||||
): void => {
|
||||
if (cmd.startsWith('menu:')) {
|
||||
// if the command starts with 'menu:', then it is a menu command
|
||||
// and we should App.toggleMenu("menuName") based on the input menu:menuName. Ignoring spaces and case
|
||||
const menuName = cmd.split(':')[1].trim().toLowerCase();
|
||||
|
||||
openMenu(events.clicked, events.event, `${menuName}menu`);
|
||||
|
||||
return;
|
||||
@@ -32,15 +31,10 @@ export const runAsyncCommand = (
|
||||
fn(output);
|
||||
}
|
||||
})
|
||||
.catch((err) =>
|
||||
console.error(`Error running command "${cmd}": ${err})`)
|
||||
);
|
||||
.catch((err) => console.error(`Error running command "${cmd}": ${err})`));
|
||||
};
|
||||
|
||||
export function throttle<T extends (...args: any[]) => void>(
|
||||
func: T,
|
||||
limit: number
|
||||
): T {
|
||||
export function throttle<T extends ThrottleFn>(func: T, limit: number): T {
|
||||
let inThrottle: boolean;
|
||||
return function (this: ThisParameterType<T>, ...args: Parameters<T>) {
|
||||
if (!inThrottle) {
|
||||
@@ -53,31 +47,23 @@ export function throttle<T extends (...args: any[]) => void>(
|
||||
} as T;
|
||||
}
|
||||
|
||||
export const throttledScrollHandler = (interval: number) =>
|
||||
throttle((cmd: string, fn: Function | undefined) => {
|
||||
export const throttledScrollHandler = (interval: number): ThrottleFn =>
|
||||
throttle((cmd: string, fn: ThrottleFnCallback) => {
|
||||
Utils.execAsync(`bash -c "${cmd}"`)
|
||||
.then((output) => {
|
||||
if (fn !== undefined) {
|
||||
fn(output);
|
||||
}
|
||||
})
|
||||
.catch((err) =>
|
||||
console.error(`Error running command "${cmd}": ${err}`)
|
||||
);
|
||||
.catch((err) => console.error(`Error running command "${cmd}": ${err}`));
|
||||
}, 200 / interval);
|
||||
|
||||
const dummyVar = Variable('');
|
||||
|
||||
export const inputHandler = (
|
||||
self: Button<Gtk.Widget, Gtk.Widget>,
|
||||
{
|
||||
onPrimaryClick,
|
||||
onSecondaryClick,
|
||||
onMiddleClick,
|
||||
onScrollUp,
|
||||
onScrollDown,
|
||||
}: InputHandlerEvents
|
||||
) => {
|
||||
{ onPrimaryClick, onSecondaryClick, onMiddleClick, onScrollUp, onScrollDown }: InputHandlerEvents,
|
||||
): void => {
|
||||
const sanitizeInput = (input: VariableType<string>): string => {
|
||||
if (input === undefined) {
|
||||
return '';
|
||||
@@ -89,46 +75,25 @@ export const inputHandler = (
|
||||
const interval = scrollSpeed.value;
|
||||
const throttledHandler = throttledScrollHandler(interval);
|
||||
|
||||
self.on_primary_click = (clicked: any, event: Gdk.Event) =>
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onPrimaryClick?.cmd || dummyVar),
|
||||
onPrimaryClick.fn,
|
||||
{ clicked, event }
|
||||
);
|
||||
self.on_primary_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onPrimaryClick?.cmd || dummyVar), onPrimaryClick.fn, { clicked, event });
|
||||
|
||||
self.on_secondary_click = (clicked: any, event: Gdk.Event) =>
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onSecondaryClick?.cmd || dummyVar),
|
||||
onSecondaryClick.fn,
|
||||
{ clicked, event }
|
||||
);
|
||||
self.on_secondary_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onSecondaryClick?.cmd || dummyVar), onSecondaryClick.fn, { clicked, event });
|
||||
|
||||
self.on_middle_click = (clicked: any, event: Gdk.Event) =>
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onMiddleClick?.cmd || dummyVar),
|
||||
onMiddleClick.fn,
|
||||
{ clicked, event }
|
||||
);
|
||||
self.on_middle_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onMiddleClick?.cmd || dummyVar), onMiddleClick.fn, { clicked, event });
|
||||
|
||||
self.on_scroll_up = () =>
|
||||
throttledHandler(
|
||||
sanitizeInput(onScrollUp?.cmd || dummyVar),
|
||||
onScrollUp.fn
|
||||
);
|
||||
self.on_scroll_up = (): void => throttledHandler(sanitizeInput(onScrollUp?.cmd || dummyVar), onScrollUp.fn);
|
||||
|
||||
self.on_scroll_down = () =>
|
||||
throttledHandler(
|
||||
sanitizeInput(onScrollDown?.cmd || dummyVar),
|
||||
onScrollDown.fn
|
||||
);
|
||||
self.on_scroll_down = (): void =>
|
||||
throttledHandler(sanitizeInput(onScrollDown?.cmd || dummyVar), onScrollDown.fn);
|
||||
};
|
||||
|
||||
// Initial setup of event handlers
|
||||
updateHandlers();
|
||||
|
||||
const sanitizeVariable = (
|
||||
someVar: VariableType<string> | undefined
|
||||
): Binding<string> => {
|
||||
const sanitizeVariable = (someVar: VariableType<string> | undefined): Binding<string> => {
|
||||
if (someVar === undefined || typeof someVar.bind !== 'function') {
|
||||
return dummyVar.bind('value');
|
||||
}
|
||||
@@ -145,37 +110,36 @@ export const inputHandler = (
|
||||
sanitizeVariable(onScrollUp),
|
||||
sanitizeVariable(onScrollDown),
|
||||
],
|
||||
updateHandlers
|
||||
updateHandlers,
|
||||
);
|
||||
};
|
||||
|
||||
export const divide = ([total, used]: number[], round: boolean) => {
|
||||
export const divide = ([total, used]: number[], round: boolean): number => {
|
||||
const percentageTotal = (used / total) * 100;
|
||||
if (round) {
|
||||
return total > 0 ? Math.round(percentageTotal) : 0;
|
||||
}
|
||||
return total > 0 ? parseFloat(percentageTotal.toFixed(2)) : 0;
|
||||
|
||||
};
|
||||
|
||||
export const formatSizeInKiB = (sizeInBytes: number, round: boolean) => {
|
||||
const sizeInGiB = sizeInBytes / (1024 ** 1);
|
||||
export const formatSizeInKiB = (sizeInBytes: number, round: boolean): number => {
|
||||
const sizeInGiB = sizeInBytes / 1024 ** 1;
|
||||
return round ? Math.round(sizeInGiB) : parseFloat(sizeInGiB.toFixed(2));
|
||||
};
|
||||
export const formatSizeInMiB = (sizeInBytes: number, round: boolean) => {
|
||||
const sizeInGiB = sizeInBytes / (1024 ** 2);
|
||||
export const formatSizeInMiB = (sizeInBytes: number, round: boolean): number => {
|
||||
const sizeInGiB = sizeInBytes / 1024 ** 2;
|
||||
return round ? Math.round(sizeInGiB) : parseFloat(sizeInGiB.toFixed(2));
|
||||
};
|
||||
export const formatSizeInGiB = (sizeInBytes: number, round: boolean) => {
|
||||
const sizeInGiB = sizeInBytes / (1024 ** 3);
|
||||
export const formatSizeInGiB = (sizeInBytes: number, round: boolean): number => {
|
||||
const sizeInGiB = sizeInBytes / 1024 ** 3;
|
||||
return round ? Math.round(sizeInGiB) : parseFloat(sizeInGiB.toFixed(2));
|
||||
};
|
||||
export const formatSizeInTiB = (sizeInBytes: number, round: boolean) => {
|
||||
const sizeInGiB = sizeInBytes / (1024 ** 4);
|
||||
export const formatSizeInTiB = (sizeInBytes: number, round: boolean): number => {
|
||||
const sizeInGiB = sizeInBytes / 1024 ** 4;
|
||||
return round ? Math.round(sizeInGiB) : parseFloat(sizeInGiB.toFixed(2));
|
||||
};
|
||||
|
||||
export const autoFormatSize = (sizeInBytes: number, round: boolean) => {
|
||||
export const autoFormatSize = (sizeInBytes: number, round: boolean): number => {
|
||||
// auto convert to GiB, MiB, KiB, TiB, or bytes
|
||||
if (sizeInBytes >= 1024 ** 4) return formatSizeInTiB(sizeInBytes, round);
|
||||
if (sizeInBytes >= 1024 ** 3) return formatSizeInGiB(sizeInBytes, round);
|
||||
@@ -183,22 +147,18 @@ export const autoFormatSize = (sizeInBytes: number, round: boolean) => {
|
||||
if (sizeInBytes >= 1024 ** 1) return formatSizeInKiB(sizeInBytes, round);
|
||||
|
||||
return sizeInBytes;
|
||||
}
|
||||
};
|
||||
|
||||
export const getPostfix = (sizeInBytes: number) => {
|
||||
export const getPostfix = (sizeInBytes: number): Postfix => {
|
||||
if (sizeInBytes >= 1024 ** 4) return 'TiB';
|
||||
if (sizeInBytes >= 1024 ** 3) return 'GiB';
|
||||
if (sizeInBytes >= 1024 ** 2) return 'MiB';
|
||||
if (sizeInBytes >= 1024 ** 1) return 'KiB';
|
||||
|
||||
return 'B';
|
||||
}
|
||||
};
|
||||
|
||||
export const renderResourceLabel = (
|
||||
lblType: ResourceLabelType,
|
||||
rmUsg: GenericResourceData,
|
||||
round: boolean
|
||||
) => {
|
||||
export const renderResourceLabel = (lblType: ResourceLabelType, rmUsg: GenericResourceData, round: boolean): string => {
|
||||
const { used, total, percentage, free } = rmUsg;
|
||||
|
||||
const formatFunctions = {
|
||||
@@ -206,7 +166,7 @@ export const renderResourceLabel = (
|
||||
GiB: formatSizeInGiB,
|
||||
MiB: formatSizeInMiB,
|
||||
KiB: formatSizeInKiB,
|
||||
B: (size: number, _: boolean) => size
|
||||
B: (size: number): number => size,
|
||||
};
|
||||
|
||||
// Get them datas in proper GiB, MiB, KiB, TiB, or bytes
|
||||
@@ -218,20 +178,20 @@ export const renderResourceLabel = (
|
||||
const formatUsed = formatFunctions[postfix] || formatFunctions['B'];
|
||||
const usedSizeFormatted = formatUsed(used, round);
|
||||
|
||||
if (lblType === "used/total") {
|
||||
if (lblType === 'used/total') {
|
||||
return `${usedSizeFormatted}/${totalSizeFormatted} ${postfix}`;
|
||||
}
|
||||
if (lblType === "used") {
|
||||
if (lblType === 'used') {
|
||||
return `${autoFormatSize(used, round)} ${getPostfix(used)}`;
|
||||
}
|
||||
if (lblType === "free") {
|
||||
if (lblType === 'free') {
|
||||
return `${autoFormatSize(free, round)} ${getPostfix(free)}`;
|
||||
}
|
||||
|
||||
return `${percentage}%`;
|
||||
};
|
||||
|
||||
export const formatTooltip = (dataType: string, lblTyp: ResourceLabelType) => {
|
||||
export const formatTooltip = (dataType: string, lblTyp: ResourceLabelType): string => {
|
||||
switch (lblTyp) {
|
||||
case 'used':
|
||||
return `Used ${dataType}`;
|
||||
@@ -244,4 +204,4 @@ export const formatTooltip = (dataType: string, lblTyp: ResourceLabelType) => {
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user