@flowscripter/flowscripter-io-cli 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowscripter/flowscripter-io-cli",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Example CLI using https://github.com/flowscripter/pluggable-io-framework",
5
5
  "keywords": [
6
6
  "bun",
@@ -0,0 +1,26 @@
1
+ const BYTE_UNITS: ReadonlyArray<{ unit: string; divisor: number }> = [
2
+ { unit: "GB", divisor: 1024 ** 3 },
3
+ { unit: "MB", divisor: 1024 ** 2 },
4
+ { unit: "KB", divisor: 1024 },
5
+ { unit: "bytes", divisor: 1 },
6
+ ];
7
+
8
+ /** Picks the largest unit that keeps `totalBytes` at 1 or more, so the scaled total isn't < 1. */
9
+ export function pickByteUnit(totalBytes: number): { unit: string; divisor: number } {
10
+ return (
11
+ BYTE_UNITS.find(({ divisor }) => totalBytes >= divisor) ?? BYTE_UNITS[BYTE_UNITS.length - 1]!
12
+ );
13
+ }
14
+
15
+ export interface ByteScale {
16
+ readonly unit: string;
17
+ readonly total: number;
18
+ scale(bytes: number): number;
19
+ }
20
+
21
+ /** Scales byte counts against a fixed total into the largest sensible unit (bytes/KB/MB/GB). */
22
+ export function createByteScale(totalBytes: number): ByteScale {
23
+ const { unit, divisor } = pickByteUnit(totalBytes);
24
+ const scale = (bytes: number): number => Math.round((bytes / divisor) * 100) / 100;
25
+ return { unit, total: scale(totalBytes), scale };
26
+ }
@@ -8,6 +8,7 @@ import {
8
8
  } from "@flowscripter/dynamic-cli-framework";
9
9
  import { copy as copyProvider } from "@flowscripter/pluggable-io-framework";
10
10
  import { getFilesystemProvider } from "../filesystemProvider.ts";
11
+ import { createByteScale } from "../byteScale.ts";
11
12
 
12
13
  const copy: SubCommand = {
13
14
  name: "copy",
@@ -33,16 +34,17 @@ const copy: SubCommand = {
33
34
  const provider = await getFilesystemProvider("");
34
35
  try {
35
36
  const { size } = await provider.getProperties(source);
37
+ const byteScale = createByteScale(size ?? 100);
36
38
  const handle = await printerService.showProgressBar(
37
- "bytes",
39
+ byteScale.unit,
38
40
  `Copying ${source}`,
39
- size ?? 100,
41
+ byteScale.total,
40
42
  );
41
43
  try {
42
44
  await copyProvider(provider, source, provider, destination, {
43
45
  telemetry: {
44
46
  onProgress: (event) => {
45
- printerService.updateProgressBar(handle, event.bytesProcessed);
47
+ printerService.updateProgressBar(handle, byteScale.scale(event.bytesProcessed));
46
48
  },
47
49
  },
48
50
  });
@@ -9,6 +9,7 @@ import {
9
9
  import { ChunkKind, type ChunkRef } from "@flowscripter/pluggable-io-framework-api";
10
10
  import { Sha256Hasher } from "@flowscripter/flowscripter-io-cli-hash-native";
11
11
  import { getFilesystemProvider } from "../filesystemProvider.ts";
12
+ import { createByteScale } from "../byteScale.ts";
12
13
 
13
14
  const hash: SubCommand = {
14
15
  name: "hash",
@@ -38,15 +39,16 @@ const hash: SubCommand = {
38
39
  const provider = await getFilesystemProvider("");
39
40
  try {
40
41
  const { size } = await provider.getProperties(path);
42
+ const byteScale = createByteScale(size ?? 100);
41
43
  const progressHandle = await printerService.showProgressBar(
42
- "bytes",
44
+ byteScale.unit,
43
45
  `Hashing ${path}`,
44
- size ?? 100,
46
+ byteScale.total,
45
47
  );
46
48
  let bytesProcessed = 0;
47
49
  const onChunk = (chunkLength: number): void => {
48
50
  bytesProcessed += chunkLength;
49
- printerService.updateProgressBar(progressHandle, bytesProcessed);
51
+ printerService.updateProgressBar(progressHandle, byteScale.scale(bytesProcessed));
50
52
  };
51
53
  try {
52
54
  const handle = await provider.getReadableStream(path);
@@ -8,6 +8,7 @@ import {
8
8
  } from "@flowscripter/dynamic-cli-framework";
9
9
  import { move as moveProvider } from "@flowscripter/pluggable-io-framework";
10
10
  import { getFilesystemProvider } from "../filesystemProvider.ts";
11
+ import { createByteScale } from "../byteScale.ts";
11
12
 
12
13
  const move: SubCommand = {
13
14
  name: "move",
@@ -33,12 +34,17 @@ const move: SubCommand = {
33
34
  const provider = await getFilesystemProvider("");
34
35
  try {
35
36
  const { size } = await provider.getProperties(source);
36
- const handle = await printerService.showProgressBar("bytes", `Moving ${source}`, size ?? 100);
37
+ const byteScale = createByteScale(size ?? 100);
38
+ const handle = await printerService.showProgressBar(
39
+ byteScale.unit,
40
+ `Moving ${source}`,
41
+ byteScale.total,
42
+ );
37
43
  try {
38
44
  await moveProvider(provider, source, provider, destination, {
39
45
  telemetry: {
40
46
  onProgress: (event) => {
41
- printerService.updateProgressBar(handle, event.bytesProcessed);
47
+ printerService.updateProgressBar(handle, byteScale.scale(event.bytesProcessed));
42
48
  },
43
49
  },
44
50
  });
@@ -0,0 +1,29 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { createByteScale, pickByteUnit } from "../src/byteScale.ts";
3
+
4
+ describe("pickByteUnit", () => {
5
+ test("scales to the largest unit that keeps the total at 1 or more", () => {
6
+ expect(pickByteUnit(500)).toEqual({ unit: "bytes", divisor: 1 });
7
+ expect(pickByteUnit(1024)).toEqual({ unit: "KB", divisor: 1024 });
8
+ expect(pickByteUnit(1024 ** 2)).toEqual({ unit: "MB", divisor: 1024 ** 2 });
9
+ expect(pickByteUnit(20_641_497_116)).toEqual({ unit: "GB", divisor: 1024 ** 3 });
10
+ });
11
+ });
12
+
13
+ describe("createByteScale", () => {
14
+ test("scales the total and subsequent values into the picked unit", () => {
15
+ const byteScale = createByteScale(20_641_497_116);
16
+
17
+ expect(byteScale.unit).toBe("GB");
18
+ expect(byteScale.total).toBeCloseTo(19.22, 2);
19
+ expect(byteScale.scale(13_817_151_488)).toBeCloseTo(12.87, 2);
20
+ });
21
+
22
+ test("keeps small totals in bytes", () => {
23
+ const byteScale = createByteScale(500);
24
+
25
+ expect(byteScale.unit).toBe("bytes");
26
+ expect(byteScale.total).toBe(500);
27
+ expect(byteScale.scale(250)).toBe(250);
28
+ });
29
+ });