From 64ae209daeb74bdbb4a7473a38551af34eacf37c Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Wed, 13 Nov 2024 10:11:39 +0200 Subject: [PATCH 1/2] fix: `table` format output --- src/bin/vip-logs.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/bin/vip-logs.js b/src/bin/vip-logs.js index 07ce6b6d1..79047283d 100755 --- a/src/bin/vip-logs.js +++ b/src/bin/vip-logs.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import chalk from 'chalk'; +import CliTable3 from 'cli-table3'; import { setTimeout } from 'timers/promises'; import * as logsLib from '../lib/app-logs/app-logs'; @@ -144,11 +145,36 @@ function printLogs( logs, format ) { let output = ''; if ( format && 'table' === format ) { - const rows = []; + const options = { + wordWrap: true, + wrapOnWordBoundary: true, + head: [ 'Timestamp', 'Message' ], + style: { + head: [ 'cyan' ], + border: [ 'grey' ], + }, + }; + + if ( process.stdout.isTTY && process.stdout.columns ) { + options.colWidths = [ + 'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ'.length + 2 /* padding */, + Math.max( + process.stdout.columns - '│ │ │'.length - 'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ'.length, + 20 + ), + ]; + } else { + options.style.head = []; + options.style.border = []; + } + + const table = new CliTable3( options ); for ( const { timestamp, message } of logs ) { - rows.push( `${ timestamp } ${ message }` ); - output = rows.join( '\n' ); + const msg = message.trimRight().replace( /\t/g, ' ' ); + table.push( [ timestamp, msg ] ); } + + output = table.toString(); } else { output = formatData( logs, format ); } From 7ea3f6729b976955a419a3c4119b054cefeafa4c Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Wed, 13 Nov 2024 10:21:30 +0200 Subject: [PATCH 2/2] test: fix tests --- __tests__/bin/vip-logs.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/__tests__/bin/vip-logs.js b/__tests__/bin/vip-logs.js index 7a66d8a73..2e2700294 100644 --- a/__tests__/bin/vip-logs.js +++ b/__tests__/bin/vip-logs.js @@ -63,14 +63,26 @@ describe( 'getLogs', () => { ], } ) ); - await getLogs( [], opts ); + const isTTY = process.stdout.isTTY; + try { + process.stdout.isTTY = false; + await getLogs( [], opts ); + } finally { + process.stdout.isTTY = isTTY; + } expect( logsLib.getRecentLogs ).toHaveBeenCalledTimes( 1 ); expect( logsLib.getRecentLogs ).toHaveBeenCalledWith( 1, 3, 'app', 500 ); expect( console.log ).toHaveBeenCalledTimes( 1 ); expect( console.log ).toHaveBeenCalledWith( - '2021-11-05T20:18:36.234041811Z My container message 1\n2021-11-09T20:47:07.301221112Z My container message 2' + '┌────────────────────────────────┬────────────────────────┐\n' + + '│ Timestamp │ Message │\n' + + '├────────────────────────────────┼────────────────────────┤\n' + + '│ 2021-11-05T20:18:36.234041811Z │ My container message 1 │\n' + + '├────────────────────────────────┼────────────────────────┤\n' + + '│ 2021-11-09T20:47:07.301221112Z │ My container message 2 │\n' + + '└────────────────────────────────┴────────────────────────┘' ); const trackingParams = {