Skip to content

Commit

Permalink
Use relative formatting for ls output (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjduffy authored Nov 9, 2023
1 parent b81fe04 commit 7517229
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/replay/src/cli/formatRecordings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ import table from "text-table";
import { generateDefaultTitle } from "../generateDefaultTitle";
import { ExternalRecordingEntry } from "../types";

const MsPerSecond = 1000;
const MsPerMinute = MsPerSecond * 60;
const MsPerHour = MsPerMinute * 60;
const MsPerDay = MsPerHour * 24;

function formatTime(time: Date) {
const fmt = new Intl.RelativeTimeFormat("en", {
style: "long",
});

const ds = Date.now() - time.getTime();
if (ds < MsPerMinute) {
return fmt.format(Math.round(-ds / MsPerSecond), "second");
} else if (ds < MsPerHour) {
return fmt.format(Math.round(-ds / MsPerMinute), "minute");
} else if (ds < MsPerDay) {
return fmt.format(Math.round(-ds / MsPerHour), "hour");
}

return fmt.format(Math.round(-ds / MsPerDay), "day");
}

export function formatAllRecordingsHumanReadable(recordings: ExternalRecordingEntry[]) {
// sort by created at date
recordings.sort((a, b) => {
Expand All @@ -16,11 +38,11 @@ export function formatAllRecordingsHumanReadable(recordings: ExternalRecordingEn
typeof recording.metadata?.title === "string"
? recording.metadata.title
: generateDefaultTitle(recording.metadata);
return [recording.id, recording.status, title || "", recording.createTime.toISOString()];
return [recording.id, recording.status, title || "", formatTime(recording.createTime)];
});

const tableBody: Array<Array<string>> = [
["ID", "Status", "Title", "Created At"],
["ID", "Status", "Title", "Created"],
...formattedRecordings,
];

Expand Down

0 comments on commit 7517229

Please sign in to comment.