Skip to content

Commit

Permalink
fix: memory unit bug #1
Browse files Browse the repository at this point in the history
nexmoe committed Oct 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c7b5767 commit e91944f
Showing 2 changed files with 25 additions and 21 deletions.
29 changes: 15 additions & 14 deletions src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import prettyBytes from "./prettyBytes";
import * as SI from "systeminformation";
import prettyBytes from "./prettyBytes";
import { MetricCtrProps } from "./constants";

const pretty = (bytes: number, option: any = {}): string => {
return prettyBytes(
bytes,
{
binary: true,
space: false,
},
{
minimumSignificantDigits: 3,
maximumSignificantDigits: 3,
...option,
}
);
return prettyBytes(bytes, {
binary: true,
space: false,
single: true,
minimumFractionDigits: 1,
minimumIntegerDigits: 1,
minimumSignificantDigits: 4,
maximumSignificantDigits: 4,
...option,
});
};

const cpuText = async () => {
@@ -24,8 +22,11 @@ const cpuText = async () => {

const memText = async () => {
const m = await SI.mem();
const active = pretty(m.active).replace(/[a-zA-Z\s]+/, "");
const active = pretty(m.active);
const total = pretty(m.total);
if (active.slice(-1) === total.slice(-1)) {
return `$(server)${active.slice(0, -1)}/${total}`;
}
return `$(server)${active}/${total}`;
};

17 changes: 10 additions & 7 deletions src/prettyBytes.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ const BYTE_UNITS: string[] = [
"YB",
];

const BIBYTE_UNITS: string[] = [
const BI_BYTE_UNITS: string[] = [
"B",
"KiB",
"MiB",
@@ -34,7 +34,7 @@ const BIT_UNITS: string[] = [
"Ybit",
];

const BIBIT_UNITS: string[] = [
const BI_BIT_UNITS: string[] = [
"b",
"kibit",
"Mibit",
@@ -72,16 +72,19 @@ export default function prettyBytes(
bits = false,
binary = false,
space = true,
single = false,
locale,
signed,
...option2
}: {
bits?: boolean;
binary?: boolean;
space?: boolean;
single?: boolean;
locale?: string | string[] | boolean;
signed?: boolean;
},
option2: any
option2?: Intl.NumberFormatOptions;
}
): string {
if (!Number.isFinite(number)) {
throw new TypeError(
@@ -91,10 +94,10 @@ export default function prettyBytes(

const UNITS = bits
? binary
? BIBIT_UNITS
? BI_BIT_UNITS
: BIT_UNITS
: binary
? BIBYTE_UNITS
? BI_BYTE_UNITS
: BYTE_UNITS;

const separator = space ? " " : "";
@@ -129,7 +132,7 @@ export default function prettyBytes(

const numberString = toLocaleString(number, locale, option2);

const unit = UNITS[exponent];
const unit = single ? UNITS[exponent][0] : UNITS[exponent];

return prefix + numberString + separator + unit;
}

0 comments on commit e91944f

Please sign in to comment.