mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 04:14:30 +00:00
feat(cli): add @shumai/tableprinter package to handle terminal-friendly table layouts
This commit is contained in:
@@ -12,6 +12,8 @@ describe('CLI Commands', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let logSpy: any
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let writeSpy: any
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let errorSpy: any
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let exitSpy: any
|
||||
@@ -48,6 +50,7 @@ describe('CLI Commands', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks()
|
||||
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
|
||||
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
exitSpy = vi
|
||||
.spyOn(process, 'exit')
|
||||
@@ -60,6 +63,7 @@ describe('CLI Commands', () => {
|
||||
|
||||
afterEach(() => {
|
||||
logSpy.mockRestore()
|
||||
writeSpy.mockRestore()
|
||||
errorSpy.mockRestore()
|
||||
exitSpy.mockRestore()
|
||||
})
|
||||
@@ -82,8 +86,8 @@ describe('CLI Commands', () => {
|
||||
expect(mockClient.api.projects.$get).toHaveBeenCalledWith({
|
||||
query: { first: '200' },
|
||||
})
|
||||
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 1'))
|
||||
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 2'))
|
||||
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 1'))
|
||||
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Proj 2'))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -108,8 +112,8 @@ describe('CLI Commands', () => {
|
||||
await ls('parentId123')
|
||||
|
||||
expect(mockClient.api.folders[':folderId'].children.$get).toHaveBeenCalledTimes(2)
|
||||
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('Sub 1'))
|
||||
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('doc.pdf'))
|
||||
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('Sub 1'))
|
||||
expect(writeSpy).toHaveBeenCalledWith(expect.stringContaining('doc.pdf'))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+14
-27
@@ -1,3 +1,4 @@
|
||||
import { newTablePrinter, withTruncate } from '@shumai/tableprinter'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { getClient } from '../client'
|
||||
|
||||
@@ -61,38 +62,24 @@ export async function ls(parentId: string) {
|
||||
return
|
||||
}
|
||||
|
||||
const idWidth = 26
|
||||
const nameWidth = 35
|
||||
const typeWidth = 10
|
||||
const sizeWidth = 12
|
||||
const isTty = process.stdout.isTTY
|
||||
const maxWidth = process.stdout.columns || 80
|
||||
const tp = newTablePrinter(process.stdout, isTty, maxWidth)
|
||||
|
||||
console.log(
|
||||
'ID'.padEnd(idWidth) +
|
||||
' | ' +
|
||||
'Name'.padEnd(nameWidth) +
|
||||
' | ' +
|
||||
'Type'.padEnd(typeWidth) +
|
||||
' | ' +
|
||||
'Size'.padEnd(sizeWidth),
|
||||
)
|
||||
console.log('-'.repeat(idWidth + nameWidth + typeWidth + sizeWidth + 9))
|
||||
tp.addHeader(['ID', 'Name', 'Type', 'Size'])
|
||||
|
||||
for (const item of items) {
|
||||
const id = item.id || ''
|
||||
const name = item.name || ''
|
||||
const type = item.typeDisplay
|
||||
const size = item.typeDisplay === 'folder' ? '-' : formatSize(item.sizeByte || 0)
|
||||
|
||||
console.log(
|
||||
id.padEnd(idWidth) +
|
||||
' | ' +
|
||||
name.padEnd(nameWidth) +
|
||||
' | ' +
|
||||
type.padEnd(typeWidth) +
|
||||
' | ' +
|
||||
size.padEnd(sizeWidth),
|
||||
tp.addField(item.id || '', withTruncate(null))
|
||||
tp.addField(item.name || '')
|
||||
tp.addField(item.typeDisplay, withTruncate(null))
|
||||
tp.addField(
|
||||
item.typeDisplay === 'folder' ? '-' : formatSize(item.sizeByte || 0),
|
||||
withTruncate(null),
|
||||
)
|
||||
tp.endRow()
|
||||
}
|
||||
|
||||
tp.render()
|
||||
} catch (err) {
|
||||
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
|
||||
process.exit(1)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { newTablePrinter } from '@shumai/tableprinter'
|
||||
import { getClient } from '../client'
|
||||
|
||||
export async function projectLs() {
|
||||
@@ -20,28 +21,20 @@ export async function projectLs() {
|
||||
return
|
||||
}
|
||||
|
||||
const idWidth = 26
|
||||
const nameWidth = 30
|
||||
const rootWidth = 26
|
||||
const isTty = process.stdout.isTTY
|
||||
const maxWidth = process.stdout.columns || 80
|
||||
const tp = newTablePrinter(process.stdout, isTty, maxWidth)
|
||||
|
||||
console.log(
|
||||
'ID'.padEnd(idWidth) +
|
||||
' | ' +
|
||||
'Name'.padEnd(nameWidth) +
|
||||
' | ' +
|
||||
'Root Folder ID'.padEnd(rootWidth),
|
||||
)
|
||||
console.log('-'.repeat(idWidth + nameWidth + rootWidth + 6))
|
||||
tp.addHeader(['ID', 'Name', 'Root Folder ID'])
|
||||
|
||||
for (const p of projects) {
|
||||
const id = p.id || ''
|
||||
const name = p.name || ''
|
||||
const rootFolder = p.rootFolder || ''
|
||||
|
||||
console.log(
|
||||
id.padEnd(idWidth) + ' | ' + name.padEnd(nameWidth) + ' | ' + rootFolder.padEnd(rootWidth),
|
||||
)
|
||||
tp.addField(p.id || '')
|
||||
tp.addField(p.name || '')
|
||||
tp.addField(p.rootFolder || '')
|
||||
tp.endRow()
|
||||
}
|
||||
|
||||
tp.render()
|
||||
} catch (err) {
|
||||
console.error('Error connecting to API server:', err instanceof Error ? err.message : err)
|
||||
process.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user