Skip to content

Commit

Permalink
table!
Browse files Browse the repository at this point in the history
  • Loading branch information
mallsoft committed Aug 16, 2024
1 parent d63fd45 commit 5280f8b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/lib/components/visuals/terminus/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,33 @@ new Command(
fetch('/api/stats')
.then((res) => res.json())
.then((data) => {
JSON.stringify(data, null, 2)
.split('\n')
.forEach((line, idx) => {
setTimeout(() => lines.write(line), 10 * idx);
const keys = Object.keys(data[0]);
const values = Object.values(data);

const columnIsFinite = keys.map((k) => values.every((v) => isFinite(v[k])));
const columnWidths = keys.map((k) =>
Math.max(k.length, ...values.map((row) => String(row[k]).length))
);

const head = keys.map((key, idx) => key.padEnd(columnWidths[idx])).join(' | ');
const middleLine = columnWidths.map((w) => '-'.repeat(w)).join('-|-');
const body = values.flatMap((row) =>
keys
.map((k, idx) =>
columnIsFinite[idx]
? String(row[k]).padStart(columnWidths[idx], ' ')
: String(row[k]).padEnd(columnWidths[idx], ' ')
)
.join(' | ')
);

lines.write(head);
setTimeout(() => {
lines.write(middleLine);
body.forEach((line, idx) => {
setTimeout(() => lines.write(line), 20 * idx);
});
}, 150);
});
return true;
},
Expand Down

0 comments on commit 5280f8b

Please sign in to comment.