Skip to content

Commit d6ae511

Browse files
committed
fix: better byte size formatting & accepting
1 parent 2c351ce commit d6ae511

3 files changed

Lines changed: 60 additions & 31 deletions

File tree

sandbox/mod.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { join } from "@std/path";
77
import { Spinner } from "@std/cli/unstable-spinner";
88

99
import { getAppFromConfig, readConfig, writeConfig } from "../config.ts";
10-
import { error, renderTemporalTimestamp, withApp } from "../util.ts";
10+
import { error, parseSize, renderTemporalTimestamp, withApp } from "../util.ts";
1111
import { createTrpcClient, getAuth } from "../auth.ts";
1212
import type { GlobalOptions } from "../main.ts";
1313
import token_storage from "../token_storage.ts";
@@ -609,28 +609,6 @@ export function formatDuration(ms: number): string {
609609
return str;
610610
}
611611

612-
function parseSize(size: string | undefined): number | undefined {
613-
if (size === undefined) return undefined;
614-
615-
const regex = /^(\d+)(gb|mb)$/i;
616-
const res = regex.exec(size);
617-
618-
if (res === null) {
619-
// error
620-
error(
621-
false,
622-
"Invalid size format. Examples of valid size: '2gb', '1024mb'",
623-
);
624-
}
625-
626-
switch (res[2].toLowerCase()) {
627-
case "gb":
628-
return parseInt(res[1]) * 1024;
629-
case "mb":
630-
return parseInt(res[1]);
631-
}
632-
}
633-
634612
export const sandboxCommand = new Command<GlobalOptions>()
635613
.name("deno sandbox")
636614
.description("Interact with sandboxes")

sandbox/volumes.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Command } from "@cliffy/command";
22
import { ensureOrg, type SandboxContext } from "./mod.ts";
33
import { getAuth } from "../auth.ts";
4-
import { Client, type VolumeInit } from "@deno/sandbox";
5-
import { tablePrinter } from "../util.ts";
4+
import { Client } from "@deno/sandbox";
5+
import { formatSize, parseSize, tablePrinter } from "../util.ts";
66

77
export const volumesCreateCommand = new Command<SandboxContext>()
88
.description("Create a volume")
@@ -23,7 +23,7 @@ export const volumesCreateCommand = new Command<SandboxContext>()
2323

2424
const volume = await client.volumes.create({
2525
slug: name,
26-
capacity: options.capacity as VolumeInit["capacity"],
26+
capacity: parseSize(options.capacity)!,
2727
region: options.region,
2828
});
2929

@@ -52,15 +52,12 @@ export const volumesListCommand = new Command<SandboxContext>()
5252
["ID", "SLUG", "REGION", "USED", "TOTAL"],
5353
list.items,
5454
(volume) => {
55-
const used = volume.used.toFixed(2).toString();
56-
const total = volume.capacity.toFixed(2).toString();
57-
5855
return [
5956
volume.id,
6057
volume.slug,
6158
volume.region,
62-
used,
63-
total,
59+
formatSize(volume.used),
60+
formatSize(volume.capacity),
6461
];
6562
},
6663
);

util.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,60 @@ export function renderTemporalTimestamp(timestamp: string, hideDate = false) {
168168
return `${date.year}-${months}-${days} ${time}`;
169169
}
170170

171+
export const KIBIBYTE = 1024;
172+
export const MEBIBYTE = KIBIBYTE * 1024;
173+
export const GIBIBYTE = MEBIBYTE * 1024;
174+
175+
export const KILOBYTE = 1000;
176+
export const MEGABYTE = KILOBYTE * 1000;
177+
export const GIGABYTE = MEGABYTE * 1000;
178+
179+
export function formatSize(bytes: number): string {
180+
if (bytes === 0) return "0 Bytes";
181+
182+
if (bytes >= GIBIBYTE) {
183+
return `${parseFloat((bytes / GIBIBYTE).toFixed(2))} GiB`;
184+
}
185+
186+
if (bytes >= MEBIBYTE) {
187+
return `${parseFloat((bytes / MEBIBYTE).toFixed(2))} MiB`;
188+
}
189+
190+
if (bytes >= KIBIBYTE) {
191+
return `${parseFloat((bytes / KIBIBYTE).toFixed(2))} KiB`;
192+
}
193+
194+
return `${bytes} Bytes`;
195+
}
196+
197+
export function parseSize(size: string | undefined): number | undefined {
198+
if (size === undefined) return undefined;
199+
200+
const match = size.match(/^(\d+)(GB|MB|KB|GiB|MiB|KiB)$/i);
201+
if (!match) {
202+
error(
203+
false,
204+
"Invalid size format. Examples of valid size: '2gb', '1024mb'",
205+
);
206+
}
207+
const [, numStr, unit] = match;
208+
const num = parseInt(numStr, 10);
209+
switch (unit.toLowerCase()) {
210+
case "gb":
211+
return num * GIGABYTE;
212+
case "mb":
213+
return num * MEGABYTE;
214+
case "kb":
215+
return num * KILOBYTE;
216+
case "gib":
217+
return num * GIBIBYTE;
218+
case "mib":
219+
return num * MEBIBYTE;
220+
case "kib":
221+
return num * KIBIBYTE;
222+
}
223+
}
224+
171225
export function tablePrinter<T>(
172226
headers: string[],
173227
values: T[],

0 commit comments

Comments
 (0)