diff --git a/package-lock.json b/package-lock.json index c2aa3e2f..cb37f536 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@ethersphere/swarm-cli", - "version": "3.3.0", + "version": "3.4.0", "license": "BSD-3-Clause", "dependencies": { "@ethereumjs/wallet": "^2.0.4", diff --git a/src/command/history/list.ts b/src/command/history/list.ts index 52297a3d..0a3881e2 100644 --- a/src/command/history/list.ts +++ b/src/command/history/list.ts @@ -22,10 +22,14 @@ export class List extends HistoryCommand implements LeafCommand { return } + + const useColors = Boolean(process.stdout.isTTY) + const table = new Table({ head: ['Index', 'Timestamp', 'Reference', 'Postage stamp batch ID', 'File path', 'Upload type'], style: { - head: ['green', 'bold'], + head: useColors ? ['green', 'bold'] : [], + border: useColors ? ['grey'] : [], }, wordWrap: true, }) diff --git a/test/command/history.spec.ts b/test/command/history.spec.ts index 4e52f433..d5fba4d8 100644 --- a/test/command/history.spec.ts +++ b/test/command/history.spec.ts @@ -2,6 +2,9 @@ import chalk from 'chalk' import { randomUUID } from 'crypto' import { describeCommand, invokeTestCli } from '../utility' import { getStampOption } from '../utility/stamp' +import colors from '@colors/colors/safe' + +const ANSI_PATTERN = /\u001B\[\d+m/ //adding this for testing the ansi disable part async function uploadTestFile() { const uploadFilePath = `${__dirname}/../testpage/images/swarm.png` @@ -124,6 +127,43 @@ describeCommand( await invokeTestCli(['history', 'disable', '--yes']) }) }) + + describe('ansi: list colors', () => { + const originalIsTTY = process.stdout.isTTY + const colorsWereEnabled = colors.enabled + + beforeAll(() => { + colors.enable() + }) + + afterAll(() => { + if (!colorsWereEnabled) { + colors.disable() + } + }) + + afterEach(() => { + process.stdout.isTTY = originalIsTTY + invokeTestCli(['history', 'disable', '--yes']) + }) + + it('should not use colors when stdout is not a TTY', async () => { + await invokeTestCli(['history', 'enable']) + process.stdout.isTTY = false + await invokeTestCli(['history', 'list']) + + expect(consoleMessages[1]).not.toMatch(ANSI_PATTERN) + expect(consoleMessages[1]).toContain('Timestamp') + }) + + it('should use colors when stdout is a TTY', async () => { + await invokeTestCli(['history', 'enable']) + process.stdout.isTTY = true + await invokeTestCli(['history', 'list']) + + expect(consoleMessages[1]).toMatch(ANSI_PATTERN) + }) + }) }, { configFileName: 'history' }, )