Skip to content

Commit 6209879

Browse files
committed
collapse to single file
1 parent 05cb233 commit 6209879

File tree

6 files changed

+47
-61
lines changed

6 files changed

+47
-61
lines changed

src/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { whichCommand } from "./commands/which.ts";
2222
import { Box, delayToMs, errorToString, LoggerTreeBox } from "./common.ts";
2323
import type { Delay } from "./common.ts";
2424
import { symbols } from "./common.ts";
25-
import { isShowingProgressBars } from "./console/progress/mod.ts";
25+
import { isShowingProgressBars } from "./console/progress.ts";
2626
import {
2727
CapturingBufferWriter,
2828
CapturingBufferWriterSync,

src/console/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ export type { ConfirmOptions } from "./confirm.ts";
33
export { logger } from "./logger.ts";
44
export { maybeMultiSelect, multiSelect } from "./multiSelect.ts";
55
export type { MultiSelectOption, MultiSelectOptions } from "./multiSelect.ts";
6-
export type { ProgressOptions } from "./progress/mod.ts";
7-
export { isShowingProgressBars, ProgressBar } from "./progress/mod.ts";
6+
export type { ProgressOptions } from "./progress.ts";
7+
export { isShowingProgressBars, ProgressBar } from "./progress.ts";
88
export { maybePrompt, prompt } from "./prompt.ts";
99
export type { PromptInputMask, PromptOptions } from "./prompt.ts";
1010
export { maybeSelect, select } from "./select.ts";

src/console/progress/mod.test.ts renamed to src/console/progress.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals } from "@std/assert";
22
import { renderTextItems, stripAnsiCodes } from "@david/console-static-text";
3-
import { renderProgressBar } from "./mod.ts";
3+
import { humanDownloadSize, renderProgressBar } from "./progress.ts";
44

55
Deno.test("should render when no length", () => {
66
assertEquals(
@@ -155,3 +155,27 @@ function getOutput(state: Partial<Parameters<typeof renderProgressBar>[0]>) {
155155
}, { columns: 80, rows: 10 });
156156
return stripAnsiCodes(renderTextItems(items, { columns: 80, rows: 10 }) ?? "");
157157
}
158+
159+
Deno.test("should format bytes", () => {
160+
assertEquals(
161+
humanDownloadSize(1023, 1023),
162+
"1023 B",
163+
);
164+
assertEquals(
165+
humanDownloadSize(1023, 1024),
166+
"0.99 KiB",
167+
);
168+
assertEquals(
169+
humanDownloadSize(10486, Math.pow(1024, 2)),
170+
"0.01 MiB",
171+
);
172+
173+
assertEquals(
174+
humanDownloadSize(10737418, Math.pow(1024, 3)),
175+
"0.00 GiB",
176+
);
177+
assertEquals(
178+
humanDownloadSize(10737419, Math.pow(1024, 3)),
179+
"0.01 GiB",
180+
);
181+
});

src/console/progress/mod.ts renamed to src/console/progress.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import {
77
staticText,
88
type TextItem,
99
} from "@david/console-static-text";
10-
import { isOutputTty } from "../utils.ts";
11-
import { humanDownloadSize } from "./format.ts";
12-
import { logger, LoggerRefreshItemKind } from "../logger.ts";
13-
14-
const progressBars: RenderIntervalProgressBar[] = [];
10+
import { isOutputTty } from "./utils.ts";
11+
import { logger, LoggerRefreshItemKind } from "./logger.ts";
1512

1613
/** Options for showing progress. */
1714
export interface ProgressOptions {
@@ -32,7 +29,7 @@ export interface ProgressOptions {
3229
/** A progress bar instance created via `$.progress(...)`. */
3330
export class ProgressBar {
3431
#state: RenderState;
35-
#pb: RenderIntervalProgressBar;
32+
#pb: DeferredItem;
3633
#withCount = 0;
3734
#onLog: (...data: any[]) => void;
3835
#noClear: boolean;
@@ -252,20 +249,15 @@ export function renderProgressBar(state: RenderState, size: ConsoleSize | undefi
252249
}
253250
}
254251

255-
export interface RenderIntervalProgressBar {
256-
render(size: ConsoleSize | undefined): TextItem[];
257-
}
252+
const progressBars: DeferredItem[] = [];
258253

259-
function addProgressBar(render: (size: ConsoleSize) => TextItem[]): RenderIntervalProgressBar {
260-
const pb = {
261-
render,
262-
};
263-
progressBars.push(pb);
254+
function addProgressBar(render: DeferredItem): DeferredItem {
255+
progressBars.push(render);
264256
refresh();
265-
return pb;
257+
return render;
266258
}
267259

268-
function removeProgressBar(pb: RenderIntervalProgressBar) {
260+
function removeProgressBar(pb: DeferredItem) {
269261
const index = progressBars.indexOf(pb);
270262
if (index === -1) {
271263
return false;
@@ -278,15 +270,20 @@ function removeProgressBar(pb: RenderIntervalProgressBar) {
278270
function refresh() {
279271
logger.setItems(
280272
LoggerRefreshItemKind.ProgressBars,
281-
progressBars.map((p) => {
282-
const item: DeferredItem = (consoleSize) => {
283-
return p.render(consoleSize);
284-
};
285-
return item;
286-
}),
273+
progressBars,
287274
);
288275
}
289276

290277
export function isShowingProgressBars() {
291278
return isOutputTty && progressBars.length > 0;
292279
}
280+
281+
const units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
282+
283+
export function humanDownloadSize(byteCount: number, totalBytes?: number) {
284+
const exponentBasis = totalBytes ?? byteCount;
285+
const exponent = Math.min(units.length - 1, Math.floor(Math.log(exponentBasis) / Math.log(1024)));
286+
const unit = units[exponent];
287+
const prettyBytes = (Math.floor(byteCount / Math.pow(1024, exponent) * 100) / 100).toFixed(exponent === 0 ? 0 : 2);
288+
return `${prettyBytes} ${unit}`;
289+
}

src/console/progress/format.test.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/console/progress/format.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)