feat(dacli): P4 v2 log.printRawTable primitive + 3 文件迁移
P4 v2 (2026-07-21): 加 log.printRawTable(headers, rows, opts) 收口 350 处
console.log 表格输出散布.
API:
log.printRawTable(
['ID', 'NAME', 'TYPE'],
[['123', 'myapp', 'MySQL'], ['456', 'test', 'PG']],
{ indent: '', maxColWidth: 36 }
)
- 自动按列宽对齐 (max(header.length, max(cell.length)) 但不超过 maxColWidth)
- 长字段 truncate 省略号 (… 替换最后 1 char, 总长 = maxColWidth)
- 跳过 quiet 模式 (跟 log.raw 同, 直出 stdout)
- 2-space 分隔符, 行末 newline
迁移 3 文件 (示范, 1-2h 后续可推广):
- datasource/list.ts (4 列 表格)
- dm/list.ts (2 列)
- dm/fields.ts (5 列)
新单测 5 个 (基础/长字段 truncate/maxColWidth 自定义/quiet 模式/空 rows).
原 wrapCommand 单测 + auto_fix + report_wired 全部仍 PASS.
验证:
- TS compile 56 baseline 不变 (含原 (s: string) => boolean pre-existing 错)
- 22/22 output_filter 单测 PASS
- 端到端: printRawTable primitive 独立验证输出对齐 + truncate
剩余 P4 v2 范围: 剩 ~330 处 console.log 表格输出待批量迁移, 建议下次 session
继续 (dacli 文档/迁移指南应出 'console.log → log.printRawTable 迁移对照表').
This commit is contained in:
1 parent
11f94a3dd0
commit
a9b7dd5083
5 files changed
+146
-24
No files matched your search
@@ -51,15 +51,15 @@ export function buildDatasourceListCommand(): Command {
|
||||
return ctx.success(summary)
|
||||
}
|
||||
log.info(`共 ${list.length} 个数据源`)
|
||||
console.log('ID'.padEnd(30) + 'FULL_NAME'.padEnd(23) + 'ENCODE'.padEnd(21) + 'DB_TYPE')
|
||||
console.log('-'.repeat(90))
|
||||
for (const d of list) {
|
||||
const id = String(d.id || '').padEnd(30)
|
||||
const name = String(d.fullName || '').padEnd(23)
|
||||
const code = String(d.enCode || '').padEnd(21)
|
||||
const type = String(d.dbType || '')
|
||||
console.log(`${id}${name}${code}${type}`)
|
||||
}
|
||||
log.printRawTable(
|
||||
['ID', 'FULL_NAME', 'ENCODE', 'DB_TYPE'],
|
||||
list.map((d) => [
|
||||
String(d.id || ''),
|
||||
String(d.fullName || ''),
|
||||
String(d.enCode || ''),
|
||||
String(d.dbType || ''),
|
||||
]),
|
||||
)
|
||||
ctx.success(summary)
|
||||
} catch (e) {
|
||||
ctx.fail(`datasource list 失败: ${(e as Error).message?.slice(0, 200)}`)
|
||||
|
||||
+10
-10
@@ -64,16 +64,16 @@ export function buildDmFieldsCommand(): Command {
|
||||
return ctx.success(summary)
|
||||
}
|
||||
log.info(`表 ${opts.table} 字段数=${arr.length}`)
|
||||
console.log('FIELD TYPE LENGTH NULL PK')
|
||||
console.log('-'.repeat(60))
|
||||
for (const f of arr as Array<Record<string, unknown>>) {
|
||||
const field = String(f.field || f.columnName || '').padEnd(20)
|
||||
const type = String(f.dataType || '').padEnd(14)
|
||||
const len = String(f.dataLength || '').padEnd(7)
|
||||
const nullable = f.allowNull === 0 || f.allowNull === '0' ? 'NO' : 'YES'
|
||||
const pk = f.primaryKey === 1 || f.primaryKey === '1' ? 'PK' : ''
|
||||
console.log(`${field}${type}${len}${nullable.padEnd(5)}${pk}`)
|
||||
}
|
||||
log.printRawTable(
|
||||
['FIELD', 'TYPE', 'LENGTH', 'NULL', 'PK'],
|
||||
(arr as Array<Record<string, unknown>>).map((f) => [
|
||||
String(f.field || f.columnName || ''),
|
||||
String(f.dataType || ''),
|
||||
String(f.dataLength || ''),
|
||||
f.allowNull === 0 || f.allowNull === '0' ? 'NO' : 'YES',
|
||||
f.primaryKey === 1 || f.primaryKey === '1' ? 'PK' : '',
|
||||
]),
|
||||
)
|
||||
ctx.success(summary)
|
||||
} catch (e) {
|
||||
ctx.fail(`dm fields 失败: ${(e as Error).message?.slice(0, 200)}`)
|
||||
|
||||
@@ -65,11 +65,10 @@ export function buildDmListCommand(): Command {
|
||||
return ctx.success(summary)
|
||||
}
|
||||
log.info(`dbLinkId=${dbLinkId} 共 ${list.length} 张表`)
|
||||
console.log('TABLE TABLE_NAME(中文)')
|
||||
console.log('-'.repeat(60))
|
||||
for (const t of list) {
|
||||
console.log(`${t.table.padEnd(30)}${t.tableName || ''}`)
|
||||
}
|
||||
log.printRawTable(
|
||||
['TABLE', 'TABLE_NAME(中文)'],
|
||||
list.map((t) => [t.table, t.tableName || '']),
|
||||
)
|
||||
ctx.success(summary)
|
||||
} catch (e) {
|
||||
ctx.fail(`dm list 失败: ${(e as Error).message?.slice(0, 200)}`)
|
||||
|
||||
@@ -74,6 +74,51 @@ export const log = {
|
||||
if (_quiet) { _buffer.push({ level: 'debug', args }); return }
|
||||
console.error(`${C.dim}[debug]${C.reset}`, ...args)
|
||||
},
|
||||
/**
|
||||
* 表格输出 (P4 v2 2026-07-21): 收口 350 处 console.log 表格散布的 consolidation 入口.
|
||||
*
|
||||
* 行为: 直出 stdout, 跳过 quiet 模式 (跟 log.raw 同). 输出对齐表格, 第一行
|
||||
* headers, 第二行 separator (- 重复), 之后各 rows.
|
||||
*
|
||||
* 用法 (逐步替代 console.log 表格):
|
||||
* log.printRawTable(
|
||||
* ['ID', 'FULL_NAME', 'ENCODE', 'DB_TYPE'],
|
||||
* list.map(d => [String(d.id), String(d.fullName), String(d.enCode), String(d.dbType)]),
|
||||
* )
|
||||
*
|
||||
* 兼容性: 原 console.log 表格调用 (333 处) 仍能跑, 不阻塞. 这个 primitive 是
|
||||
* 起点, 让后续 P6 续 + P4 v3 可以批量迁移.
|
||||
*
|
||||
* @param headers 列名数组
|
||||
* @param rows 数据行 (每行字符串数组, 长度应跟 headers 对齐)
|
||||
* @param opts.indent 缩进字符串 (默认 '')
|
||||
* @param opts.maxColWidth 单列最大宽度 (默认 36, 防止 long 字段撑破)
|
||||
*/
|
||||
printRawTable(
|
||||
headers: string[],
|
||||
rows: string[][],
|
||||
opts: { indent?: string; maxColWidth?: number } = {},
|
||||
): void {
|
||||
const indent = opts.indent ?? ''
|
||||
const maxColWidth = opts.maxColWidth ?? 36
|
||||
// 计算每列最大宽度 (按 truncate 后)
|
||||
const widths = headers.map((h, i) => {
|
||||
const cellWidths = rows.map((r) => (r[i] ?? '').length)
|
||||
return Math.min(maxColWidth, Math.max(h.length, ...cellWidths))
|
||||
})
|
||||
const pad = (s: string, w: number) => {
|
||||
const truncated = s.length > w ? s.slice(0, w - 1) + '…' : s
|
||||
return truncated.padEnd(w)
|
||||
}
|
||||
const headerLine = indent + headers.map((h, i) => pad(h, widths[i])).join(' ') + '\n'
|
||||
const sepLine = indent + widths.map((w) => '-'.repeat(w)).join(' ') + '\n'
|
||||
const body = rows
|
||||
.map((r) => indent + r.map((c, i) => pad(c ?? '', widths[i])).join(' ') + '\n')
|
||||
.join('')
|
||||
const output = headerLine + sepLine + body
|
||||
if (_quiet) { _buffer.push({ level: 'raw', args: [output] }); return }
|
||||
process.stdout.write(output)
|
||||
},
|
||||
}
|
||||
|
||||
const _writers: Record<string, (...args: unknown[]) => void> = {
|
||||
|
||||
@@ -354,3 +354,81 @@ describe('wrapCommand — 5c 收口 helper (v2.2+ 2026-07-21)', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('log.printRawTable — 表格输出 primitive (P4 v2 2026-07-21)', () => {
|
||||
let stdoutSpy: ReturnType<typeof vi.spyOn>
|
||||
|
||||
beforeEach(() => {
|
||||
stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(((_s: string) => {
|
||||
// no-op
|
||||
}) as never)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
stdoutSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('基础表格: header + separator + rows 对齐', () => {
|
||||
log.printRawTable(
|
||||
['ID', 'NAME', 'TYPE'],
|
||||
[
|
||||
['123', 'myapp', 'MySQL'],
|
||||
['456', 'test', 'PG'],
|
||||
],
|
||||
)
|
||||
const output = stdoutSpy.mock.calls.map((c) => String(c[0])).join('')
|
||||
// header widths: ID(3) NAME(5) TYPE(5); 2-space separator
|
||||
expect(output).toContain('ID')
|
||||
expect(output).toContain('NAME')
|
||||
expect(output).toContain('TYPE')
|
||||
expect(output).toContain('--- ----- -----') // 5 dashes for NAME+TYPE
|
||||
expect(output).toContain('123')
|
||||
expect(output).toContain('myapp')
|
||||
expect(output).toContain('MySQL')
|
||||
expect(output).toContain('456')
|
||||
expect(output).toContain('PG')
|
||||
// 3 lines: header + separator + at least 2 data rows
|
||||
expect(output.trim().split('\n').length).toBeGreaterThanOrEqual(3)
|
||||
})
|
||||
|
||||
it('长字段 truncate 省略号', () => {
|
||||
log.printRawTable(
|
||||
['NAME'],
|
||||
[['this is a very long name that exceeds the 36 char default maxColWidth']],
|
||||
)
|
||||
const output = stdoutSpy.mock.calls.map((c) => String(c[0])).join('')
|
||||
expect(output).toContain('…')
|
||||
// 数据行被截断到 default maxColWidth=36 char (含 '…')
|
||||
const lines = output.trim().split('\n')
|
||||
const dataLine = lines[lines.length - 1] // 最后一行 = 数据行
|
||||
expect(dataLine.length).toBeLessThanOrEqual(36 + 10) // 36 + 一些 padding
|
||||
})
|
||||
|
||||
it('maxColWidth 自定义', () => {
|
||||
log.printRawTable(
|
||||
['NAME'],
|
||||
[['short'], ['much longer than twenty characters in this test']],
|
||||
{ maxColWidth: 20 },
|
||||
)
|
||||
const output = stdoutSpy.mock.calls.map((c) => String(c[0])).join('')
|
||||
expect(output).toContain('much longer than tw…')
|
||||
expect(output).toContain('short')
|
||||
})
|
||||
|
||||
it('quiet mode: 写入 buffer 而非 stdout', () => {
|
||||
// 进入 quiet mode (默认)
|
||||
log.setQuiet(true)
|
||||
log.printRawTable(['A'], [['x']])
|
||||
// quiet 模式应写到 buffer 而非 stdout
|
||||
expect(stdoutSpy).not.toHaveBeenCalled()
|
||||
log.setQuiet(false)
|
||||
})
|
||||
|
||||
it('空 rows: 只打 header + separator', () => {
|
||||
log.printRawTable(['A', 'B'], [])
|
||||
const output = stdoutSpy.mock.calls.map((c) => String(c[0])).join('')
|
||||
expect(output).toContain('A')
|
||||
expect(output).toContain('B')
|
||||
expect(output).toContain('-') // separator
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user