Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

feat(cli): render tables in kubectl style (#1748)

devin-ai-integration[bot]bc14dd015862
diff --git a/.changeset/plenty-tables-shine.md b/.changeset/plenty-tables-shine.md
new file mode 100644
index 000000000..e7902201c
--- /dev/null
+++ b/.changeset/plenty-tables-shine.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': patch
+---
+
+Render CLI tables in kubectl style: uppercase headers, space-aligned columns, no borders or titles
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 2dd524d1a..d9a6e9d5e 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -80,11 +80,11 @@
"chalk": "^5.3.0",
"cli-highlight": "^2.1.11",
"commander": "^11.1.0",
- "console-table-printer": "^2.11.2",
"e2b": "workspace:^",
"handlebars": "^4.7.9",
"inquirer": "^12.10.0",
"simple-update-notifier": "^2.0.0",
+ "simple-wcswidth": "^1.1.2",
"statuses": "^2.0.1",
"yup": "^1.3.2"
},
diff --git a/packages/cli/src/commands/sandbox/list.ts b/packages/cli/src/commands/sandbox/list.ts
index a1b18c942..e877970d5 100644
--- a/packages/cli/src/commands/sandbox/list.ts
+++ b/packages/cli/src/commands/sandbox/list.ts
@@ -1,21 +1,13 @@
-import * as tablePrinter from 'console-table-printer'
import * as commander from 'commander'
import { components, Sandbox, SandboxInfo, SandboxListOrder } from 'e2b'
import { ensureAPIKey } from 'src/api'
+import { renderTable } from 'src/utils/table'
import { parseMetadata } from './utils'
const DEFAULT_LIMIT = 1000
const PAGE_LIMIT = 100
-function getStateTitle(state?: components['schemas']['SandboxState'][]) {
- if (state?.length === 1) {
- if (state?.includes('running')) return 'Running sandboxes'
- if (state?.includes('paused')) return 'Paused sandboxes'
- }
- return 'Sandboxes'
-}
-
export const listCommand = new commander.Command('list')
.description('list all sandboxes, by default it list only running ones')
.alias('ls')
@@ -76,7 +68,7 @@ export const listCommand = new commander.Command('list')
})
if (format === 'pretty') {
- renderTable(sandboxes, state, options.order)
+ renderSandboxTable(sandboxes, options.order)
if (hasMore) {
console.log(
`Showing first ${limit} sandboxes. Use --limit to change.`
@@ -124,9 +116,8 @@ export function buildTableRows(
}))
}
-function renderTable(
+function renderSandboxTable(
sandboxes: SandboxInfo[],
- state: components['schemas']['SandboxState'][],
order?: SandboxListOrder
) {
if (!sandboxes?.length) {
@@ -134,55 +125,18 @@ function renderTable(
return
}
- const table = new tablePrinter.Table({
- title: getStateTitle(state),
- columns: [
- { name: 'sandboxId', alignment: 'left', title: 'Sandbox ID' },
- {
- name: 'templateId',
- alignment: 'left',
- title: 'Template ID',
- maxLen: 20,
- },
- { name: 'name', alignment: 'left', title: 'Alias' },
- { name: 'startedAt', alignment: 'left', title: 'Started at' },
- { name: 'endAt', alignment: 'left', title: 'End at' },
- { name: 'state', alignment: 'left', title: 'State' },
- { name: 'cpuCount', alignment: 'left', title: 'vCPUs' },
- { name: 'memoryMB', alignment: 'left', title: 'RAM MiB' },
- { name: 'envdVersion', alignment: 'left', title: 'Envd version' },
- { name: 'metadata', alignment: 'left', title: 'Metadata' },
- ],
- disabledColumns: ['clientID'],
- rows: buildTableRows(sandboxes, order),
- style: {
- headerTop: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- headerBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- tableBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- vertical: '',
- },
- colorMap: {
- orange: '\x1b[38;5;216m',
- },
- })
- table.printTable()
-
- process.stdout.write('\n')
+ renderTable(buildTableRows(sandboxes, order), [
+ { header: 'Sandbox ID', value: (row) => row.sandboxId },
+ { header: 'Template ID', value: (row) => row.templateId },
+ { header: 'Alias', value: (row) => row.name },
+ { header: 'Started at', value: (row) => row.startedAt },
+ { header: 'End at', value: (row) => row.endAt },
+ { header: 'State', value: (row) => row.state },
+ { header: 'vCPUs', value: (row) => String(row.cpuCount) },
+ { header: 'RAM MiB', value: (row) => String(row.memoryMB) },
+ { header: 'Envd version', value: (row) => row.envdVersion },
+ { header: 'Metadata', value: (row) => row.metadata },
+ ])
}
type ListSandboxesOptions = {
diff --git a/packages/cli/src/commands/sandbox/snapshot.ts b/packages/cli/src/commands/sandbox/snapshot.ts
index 8c2786164..035accf7d 100644
--- a/packages/cli/src/commands/sandbox/snapshot.ts
+++ b/packages/cli/src/commands/sandbox/snapshot.ts
@@ -1,9 +1,9 @@
-import * as tablePrinter from 'console-table-printer'
import * as commander from 'commander'
import { NotFoundError, Sandbox, SnapshotInfo } from 'e2b'
import { ensureAPIKey } from 'src/api'
import { asBold } from 'src/utils/format'
+import { renderTable } from 'src/utils/table'
const DEFAULT_LIMIT = 1000
const PAGE_LIMIT = 100
@@ -91,7 +91,7 @@ const listSnapshotsCommand = new commander.Command('list')
paginator.hasNext || (limit ? allSnapshots.length > limit : false)
if (format === 'pretty') {
- renderTable(snapshots)
+ renderSnapshotTable(snapshots)
if (hasMore) {
console.log(
`Showing first ${limit} snapshots. Use --limit to change.`
@@ -155,50 +155,19 @@ export const snapshotCommand = new commander.Command('snapshot')
.addCommand(listSnapshotsCommand)
.addCommand(deleteSnapshotCommand)
-function renderTable(snapshots: SnapshotInfo[]) {
+function renderSnapshotTable(snapshots: SnapshotInfo[]) {
if (!snapshots?.length) {
console.log('No snapshots found')
return
}
- const table = new tablePrinter.Table({
- title: 'Snapshots',
- columns: [
- { name: 'snapshotId', alignment: 'left', title: 'Snapshot ID' },
- { name: 'names', alignment: 'left', title: 'Names' },
- ],
- rows: snapshots.map((snapshot) => ({
- ...snapshot,
- names: snapshot.names.map(stripControlChars).join(', '),
- })),
- style: {
- headerTop: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- headerBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- tableBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- vertical: '',
+ renderTable(snapshots, [
+ { header: 'Snapshot ID', value: (snapshot) => snapshot.snapshotId },
+ {
+ header: 'Names',
+ value: (snapshot) => snapshot.names.map(stripControlChars).join(', '),
},
- colorMap: {
- orange: '\x1b[38;5;216m',
- },
- })
- table.printTable()
-
- process.stdout.write('\n')
+ ])
}
function stripControlChars(value: string) {
diff --git a/packages/cli/src/commands/template/list.ts b/packages/cli/src/commands/template/list.ts
index cce6d066b..3296c32ba 100644
--- a/packages/cli/src/commands/template/list.ts
+++ b/packages/cli/src/commands/template/list.ts
@@ -1,8 +1,8 @@
-import * as tablePrinter from 'console-table-printer'
import * as commander from 'commander'
import * as e2b from 'e2b'
import { listAliases } from '../../utils/format'
+import { renderTable } from 'src/utils/table'
import { sortTemplatesAliases } from 'src/utils/templateSort'
import { client, ensureAPIKey, resolveProjectId } from 'src/api'
import {
@@ -33,7 +33,7 @@ export const listCommand = new commander.Command('list')
}
if (format === 'pretty') {
- renderTable(templates)
+ renderTemplateTable(templates)
} else if (format === 'json') {
console.log(JSON.stringify(templates, null, 2))
} else {
@@ -46,74 +46,40 @@ export const listCommand = new commander.Command('list')
}
})
-function renderTable(templates: e2b.components['schemas']['Template'][]) {
+function renderTemplateTable(
+ templates: e2b.components['schemas']['Template'][]
+) {
if (!templates?.length) {
console.log('No templates found.')
return
}
- const table = new tablePrinter.Table({
- title: 'Sandbox templates',
- columns: [
- { name: 'visibility', alignment: 'left', title: 'Access' },
- { name: 'templateID', alignment: 'left', title: 'Template ID' },
- {
- name: 'aliases',
- alignment: 'left',
- title: 'Template Name',
- color: 'orange',
- maxLen: 20,
- },
- { name: 'cpuCount', alignment: 'right', title: 'vCPUs' },
- { name: 'memoryMB', alignment: 'right', title: 'RAM MiB' },
- { name: 'createdBy', alignment: 'right', title: 'Created by' },
- { name: 'createdAt', alignment: 'right', title: 'Created at' },
- { name: 'diskSizeMB', alignment: 'right', title: 'Disk size MiB' },
- { name: 'envdVersion', alignment: 'right', title: 'Envd version' },
- ],
- disabledColumns: [
- 'public',
- 'buildID',
- 'buildCount',
- 'lastSpawnedAt',
- 'spawnCount',
- 'updatedAt',
- ],
- rows: templates.map((template) => ({
- ...template,
- visibility: template.public ? 'Public' : 'Private',
- aliases: listAliases(template.aliases),
- createdBy: template.createdBy?.email,
- createdAt: new Date(template.createdAt).toLocaleDateString(),
- })),
- style: {
- headerTop: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- headerBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- tableBottom: {
- left: '',
- right: '',
- mid: '',
- other: '',
- },
- vertical: '',
+ renderTable(templates, [
+ {
+ header: 'Access',
+ value: (template) => (template.public ? 'Public' : 'Private'),
},
- colorMap: {
- orange: '\x1b[38;5;216m',
+ { header: 'Template ID', value: (template) => template.templateID },
+ {
+ header: 'Template Name',
+ value: (template) => listAliases(template.aliases),
},
- })
- table.printTable()
-
- process.stdout.write('\n')
+ { header: 'vCPUs', value: (template) => String(template.cpuCount) },
+ { header: 'RAM MiB', value: (template) => String(template.memoryMB) },
+ {
+ header: 'Created by',
+ value: (template) => template.createdBy?.email,
+ },
+ {
+ header: 'Created at',
+ value: (template) => new Date(template.createdAt).toLocaleDateString(),
+ },
+ {
+ header: 'Disk size MiB',
+ value: (template) => String(template.diskSizeMB),
+ },
+ { header: 'Envd version', value: (template) => template.envdVersion },
+ ])
}
export async function listSandboxTemplates({
diff --git a/packages/cli/src/utils/table.ts b/packages/cli/src/utils/table.ts
new file mode 100644
index 000000000..c1cfff847
--- /dev/null
+++ b/packages/cli/src/utils/table.ts
@@ -0,0 +1,51 @@
+import { wcswidth } from 'simple-wcswidth'
+
+// Renders tables in the style of kubectl (k8s.io/cli-runtime/pkg/printers):
+// uppercase headers, left-aligned columns separated by spaces, no borders.
+// https://github.com/kubernetes/cli-runtime/blob/master/pkg/printers/tableprinter.go
+
+const COLUMN_PADDING = 3
+
+export interface Column<T> {
+ header: string
+ value: (item: T) => string | null | undefined
+}
+
+/**
+ * Renders `items` as a borderless, space-aligned table on stdout: a header
+ * line of uppercased column headers followed by one line per item.
+ *
+ * @param items rows to render, in the order they should be printed.
+ * @param columns column definitions; each one extracts a single cell from an item.
+ *
+ * @example
+ * ```ts
+ * renderTable(sandboxes, [
+ * { header: 'Sandbox ID', value: (sandbox) => sandbox.sandboxId },
+ * { header: 'State', value: (sandbox) => sandbox.state },
+ * ])
+ * ```
+ */
+export function renderTable<T>(items: T[], columns: Column<T>[]) {
+ const headers = columns.map((column) => column.header.toUpperCase())
+ const rows = items.map((item) =>
+ columns.map((column) => column.value(item) ?? '')
+ )
+
+ const widths = headers.map((header, i) =>
+ rows.reduce((max, row) => Math.max(max, wcswidth(row[i])), wcswidth(header))
+ )
+
+ for (const line of [headers, ...rows]) {
+ console.log(
+ line
+ .map((cell, i) =>
+ i === line.length - 1
+ ? cell
+ : cell + ' '.repeat(widths[i] + COLUMN_PADDING - wcswidth(cell))
+ )
+ .join('')
+ .trimEnd()
+ )
+ }
+}
diff --git a/packages/cli/tests/utils/table.test.ts b/packages/cli/tests/utils/table.test.ts
new file mode 100644
index 000000000..df8ef1ab2
--- /dev/null
+++ b/packages/cli/tests/utils/table.test.ts
@@ -0,0 +1,73 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { renderTable } from '../../src/utils/table'
+
+describe('renderTable', () => {
+ afterEach(() => {
+ vi.restoreAllMocks()
+ })
+
+ function capture() {
+ const lines: string[] = []
+ vi.spyOn(console, 'log').mockImplementation((line: string) =>
+ lines.push(line)
+ )
+ return lines
+ }
+
+ it('prints uppercase headers and space-aligned columns', () => {
+ const lines = capture()
+
+ renderTable(
+ [
+ { id: 'sbx-1', name: 'alpha' },
+ { id: 'sbx-longer-2', name: 'b' },
+ ],
+ [
+ { header: 'Sandbox ID', value: (row) => row.id },
+ { header: 'Name', value: (row) => row.name },
+ ]
+ )
+
+ expect(lines).toEqual([
+ 'SANDBOX ID NAME',
+ 'sbx-1 alpha',
+ 'sbx-longer-2 b',
+ ])
+ })
+
+ it('pads columns to the widest cell and trims trailing whitespace', () => {
+ const lines = capture()
+
+ renderTable(
+ [{ a: 'x', b: '' }],
+ [
+ { header: 'A', value: (row) => row.a },
+ { header: 'B', value: (row) => row.b },
+ ]
+ )
+
+ expect(lines).toEqual(['A B', 'x'])
+ })
+
+ it('aligns columns containing wide (CJK) characters by display width', () => {
+ const lines = capture()
+
+ renderTable(
+ [
+ { name: '日本語', state: 'ok' },
+ { name: 'abcdef', state: 'ok' },
+ ],
+ [
+ { header: 'Name', value: (row) => row.name },
+ { header: 'State', value: (row) => row.state },
+ ]
+ )
+
+ expect(lines).toEqual([
+ 'NAME STATE',
+ '日本語 ok',
+ 'abcdef ok',
+ ])
+ })
+})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 501ee35f6..421dc55d2 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -74,9 +74,6 @@ importers:
commander:
specifier: ^11.1.0
version: 11.1.0
- console-table-printer:
- specifier: ^2.11.2
- version: 2.11.2
e2b:
specifier: workspace:^
version: link:../js-sdk
@@ -89,6 +86,9 @@ importers:
simple-update-notifier:
specifier: ^2.0.0
version: 2.0.0
+ simple-wcswidth:
+ specifier: ^1.1.2
+ version: 1.1.2
statuses:
specifier: ^2.0.1
version: 2.0.1
@@ -2301,9 +2301,6 @@ packages:
resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
- resolution: {integrity: sha512-uuUHie0sfPP542TKGzPFal0W1wo1beuKAqIZdaavcONx8OoqdnJRKjkinbRTOta4FaCa1RcIL+7mMJWX3pQGVg==}
-
resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==}
@@ -3543,8 +3540,8 @@ packages:
resolution: {integrity: sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==}
engines: {node: '>=10'}
- resolution: {integrity: sha512-xMO/8eNREtaROt7tJvWJqHBDTMFN4eiQ5I4JRMuilwfnFcV5W9u7RUkueNkdw0jPqGMX36iCywelS5yilTuOxg==}
+ resolution: {integrity: sha512-j7piyCjAeTDSjzTSQ7DokZtMNwNlEAyxqSZeCS+CXH7fJ4jx3FuJ/mTW3mE+6JLs4VJBbcll0Kjn+KXI5t21Iw==}
resolution: {integrity: sha512-2wcC/oGxHis/BoHkkPwldgiPSYcpZK3JU28WoMVv55yHJgcZ8rlXvuG9iZggz+sU1d4bRgIGASwyWqjxu3FM0g==}
@@ -5847,10 +5844,6 @@ snapshots:
- dependencies:
- simple-wcswidth: 1.0.1
-
@@ -7245,7 +7238,7 @@ snapshots:
dependencies:
semver: 7.7.2
dependencies: