Skip to content

Commit

Permalink
Refactor (Core): wrap utility around getting the readline width (inte…
Browse files Browse the repository at this point in the history
…rnal only)
  • Loading branch information
SBoudrias committed Sep 24, 2023
1 parent 3a9adcf commit af0b0fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/lib/screen-manager.mts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/lib/use-pagination.mts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/lib/utils.mts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,12 +9,21 @@ 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 })
.split('\n')
.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 });
}

0 comments on commit af0b0fb

Please sign in to comment.