From ab5298ab8097d7fd259fd5d9708f273e23f9927d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:40:58 -0300 Subject: [PATCH] chore(CLI): improve hr --- src/helpers/format.ts | 3 +++ src/helpers/hr.ts | 19 +++++++++++++++---- src/services/run-tests.ts | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/helpers/format.ts b/src/helpers/format.ts index 5ab09038..3d27600e 100644 --- a/src/helpers/format.ts +++ b/src/helpers/format.ts @@ -12,3 +12,6 @@ export const format = { success: (value: string) => `\x1b[32m${value}\x1b[0m`, fail: (value: string) => `\x1b[31m${value}\x1b[0m`, }; + +export const getLargestStringLength = (arr: string[]): number => + arr.reduce((max, current) => Math.max(max, current.length), 0); diff --git a/src/helpers/hr.ts b/src/helpers/hr.ts index c2acebec..bdd1fb4c 100644 --- a/src/helpers/hr.ts +++ b/src/helpers/hr.ts @@ -1,9 +1,20 @@ import { EOL } from 'node:os'; import process from 'node:process'; -export const hr = () => { - const columns = process.stdout.columns; - const line = '⎯'.repeat(columns - 10 || 30); +let lastLenght: number = 0; - console.log(`\x1b[2m${line}\x1b[0m${EOL}`); +export const hr = (size?: number) => { + const pad = 10; + const limit = process.stdout.columns - pad; + const fileLenght = typeof size === 'number' ? Math.floor(size / 2) + pad : 0; + const columns = + fileLenght > 0 && fileLenght <= limit + ? fileLenght + : lastLenght > 0 + ? lastLenght + : limit; + const line = '⎯'.repeat(columns); + lastLenght = columns; + + console.log(`${EOL}\x1b[2m${line}\x1b[0m${EOL}`); }; diff --git a/src/services/run-tests.ts b/src/services/run-tests.ts index 967fc3cc..bfa1c100 100644 --- a/src/services/run-tests.ts +++ b/src/services/run-tests.ts @@ -5,7 +5,7 @@ import { runner } from '../helpers/runner.js'; import { indentation } from '../helpers/indentation.js'; import { listFiles } from '../modules/list-files.js'; import { hr } from '../helpers/hr.js'; -import { format } from '../helpers/format.js'; +import { format, getLargestStringLength } from '../helpers/format.js'; import { runTestFile } from './run-test-file.js'; import { Configs } from '../@types/poku.js'; import { isQuiet } from '../helpers/logs.js'; @@ -24,7 +24,7 @@ export const runTests = async ( let passed = true; if (showLogs) { - hr(); + hr(getLargestStringLength(files)); console.log( `${format.bold('Directory:')} ${format.underline(currentDir)}${EOL}` );