From af0b0fbb6c6af33069c4644a649159394b3e3601 Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Sun, 24 Sep 2023 17:32:24 -0400 Subject: [PATCH] Refactor (Core): wrap utility around getting the readline width (internal only) --- packages/core/src/lib/screen-manager.mts | 5 ++--- packages/core/src/lib/use-pagination.mts | 7 ++----- packages/core/src/lib/utils.mts | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/screen-manager.mts b/packages/core/src/lib/screen-manager.mts index 11891261b..5687d17c0 100644 --- a/packages/core/src/lib/screen-manager.mts +++ b/packages/core/src/lib/screen-manager.mts @@ -1,8 +1,7 @@ import type { CursorPos } from 'node:readline'; -import cliWidth from 'cli-width'; import stripAnsi from 'strip-ansi'; import ansiEscapes from 'ansi-escapes'; -import { breakLines } from './utils.mjs'; +import { breakLines, readlineWidth } from './utils.mjs'; import type { InquirerReadline } from './read-line.type.mjs'; const height = (content: string): number => content.split('\n').length; @@ -43,7 +42,7 @@ export default class ScreenManager { // SetPrompt will change cursor position, now we can get correct value this.cursorPos = this.rl.getCursorPos(); - const width = cliWidth({ defaultWidth: 80, output: this.rl.output }); + const width = readlineWidth(); content = breakLines(content, width); bottomContent = breakLines(bottomContent, width); diff --git a/packages/core/src/lib/use-pagination.mts b/packages/core/src/lib/use-pagination.mts index 96eb3a474..d9e3a7bc3 100644 --- a/packages/core/src/lib/use-pagination.mts +++ b/packages/core/src/lib/use-pagination.mts @@ -1,7 +1,5 @@ import chalk from 'chalk'; -import cliWidth from 'cli-width'; -import { breakLines } from './utils.mjs'; -import { readline } from './hook-engine.mjs'; +import { breakLines, readlineWidth } from './utils.mjs'; import { useRef } from './use-ref.mjs'; export function usePagination( @@ -14,13 +12,12 @@ export function usePagination( pageSize?: number; }, ) { - const rl = readline(); const state = useRef({ pointer: 0, lastIndex: 0, }); - const width = cliWidth({ defaultWidth: 80, output: rl.output }); + const width = readlineWidth(); const lines = breakLines(output, width).split('\n'); // Make sure there's enough lines to paginate diff --git a/packages/core/src/lib/utils.mts b/packages/core/src/lib/utils.mts index 2db8f7aaf..a065d774a 100644 --- a/packages/core/src/lib/utils.mts +++ b/packages/core/src/lib/utils.mts @@ -1,4 +1,6 @@ +import cliWidth from 'cli-width'; import wrapAnsi from 'wrap-ansi'; +import { readline } from './hook-engine.mjs'; /** * Force line returns at specific width. This function is ANSI code friendly and it'll @@ -7,8 +9,8 @@ import wrapAnsi from 'wrap-ansi'; * @param {number} width * @return {string} */ -export const breakLines = (content: string, width: number): string => - content +export function breakLines(content: string, width: number): string { + return content .split('\n') .flatMap((line) => wrapAnsi(line, width, { trim: false, hard: true }) @@ -16,3 +18,12 @@ export const breakLines = (content: string, width: number): string => .map((str) => str.trimEnd()), ) .join('\n'); +} + +/** + * Returns the width of the active readline, or 80 as default value. + * @returns {number} + */ +export function readlineWidth(): number { + return cliWidth({ defaultWidth: 80, output: readline().output }); +}