Skip to content

Commit

Permalink
Resolve review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dsogari committed Mar 13, 2024
1 parent 38941c7 commit 0fe4242
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/tsargp/lib/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class ErrorMessage extends Error {
* @param emitStyles True if styles should be emitted
* @returns The message to be printed on a terminal
*/
wrap(width = process.stderr.columns, emitStyles = !omitStyles(width)): string {
wrap(width = process.stderr.columns ?? 0, emitStyles = !omitStyles(width)): string {
const result = new Array<string>();
this.str.wrapToWidth(result, 0, width, emitStyles);
if (emitStyles) {
Expand All @@ -488,7 +488,7 @@ class HelpMessage extends Array<TerminalString> {
* @param emitStyles True if styles should be emitted
* @returns The message to be printed on a terminal
*/
wrap(width = process.stdout.columns, emitStyles = !omitStyles(width)): string {
wrap(width = process.stdout.columns ?? 0, emitStyles = !omitStyles(width)): string {
const result = new Array<string>();
let column = 0;
for (const str of this) {
Expand Down
55 changes: 27 additions & 28 deletions packages/tsargp/test/styles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,6 @@ describe('TerminalString', () => {
expect(result).toEqual(['abc', ' def']);
});

it('should omit styles', () => {
const str = new TerminalString();
const result = new Array<string>();
str.splitText(`abc${style(tf.clear)}def`).wrapToWidth(result, 0, 0, false);
expect(result).toEqual(['abcdef']);
});

it('should emit styles', () => {
const str = new TerminalString();
const result = new Array<string>();
str.splitText(`abc${style(tf.clear)}def`).wrapToWidth(result, 0, 0, true);
expect(result).toEqual(['abc' + style(tf.clear) + 'def']);
});

it('should preserve indentation', () => {
const str = new TerminalString(2);
const result = new Array<string>();
Expand Down Expand Up @@ -234,31 +220,26 @@ describe('TerminalString', () => {
expect(result).toEqual(['abc', '\n\n', 'def']);
});

it('should remove styles', () => {
it('should omit styles', () => {
const str = new TerminalString(0);
const result = new Array<string>();
str
.splitText(`abc${style(tf.clear)} ${style(tf.clear)} def`)
.wrapToWidth(result, 0, 0, false);
expect(result).toEqual(['abc', ' def']);
});
});

describe('when a width is provided', () => {
it('should omit styles', () => {
const str = new TerminalString();
const result = new Array<string>();
str.splitText(`abc${style(tf.clear)}def`).wrapToWidth(result, 0, 10, false);
expect(result).toEqual(['abcdef']);
});

it('should emit styles', () => {
const str = new TerminalString();
const result = new Array<string>();
str.splitText(`abc${style(tf.clear)}def`).wrapToWidth(result, 0, 10, true);
expect(result).toEqual(['abc' + style(tf.clear) + 'def']);
str
.splitText(`abc${style(tf.clear)} ${style(tf.clear)} def`)
.wrapToWidth(result, 0, 0, true);
expect(result).toEqual(['abc' + style(tf.clear), style(tf.clear), ' def']);
});
});

describe('when a width is provided', () => {
it('should wrap relative to the beginning when the largest word does not fit the width (1)', () => {
const str = new TerminalString(1);
const result = new Array<string>();
Expand Down Expand Up @@ -342,6 +323,24 @@ describe('TerminalString', () => {
str.addBreaks(1).splitText('abc largest').wrapToWidth(result, 1, 15, false);
expect(result).toEqual(['\n', `${move(3, mv.cha)}abc`, ' largest']);
});

it('should omit styles', () => {
const str = new TerminalString(0);
const result = new Array<string>();
str
.splitText(`abc${style(tf.clear)} ${style(tf.clear)} def`)
.wrapToWidth(result, 0, 10, false);
expect(result).toEqual(['abc', ' def']);
});

it('should emit styles', () => {
const str = new TerminalString();
const result = new Array<string>();
str
.splitText(`abc${style(tf.clear)} ${style(tf.clear)} def`)
.wrapToWidth(result, 0, 10, true);
expect(result).toEqual(['abc' + style(tf.clear), style(tf.clear), ' def']);
});
});
});
});
Expand All @@ -355,7 +354,7 @@ describe('ErrorMessage', () => {
expect(err.wrap(0, false)).toEqual('type script');
expect(err.wrap(0, true)).toEqual('type script' + style(tf.clear));
expect(err.wrap(11, false)).toEqual('type script');
expect(err.wrap(11)).toEqual('type script' + style(tf.clear));
expect(err.wrap(11, true)).toEqual('type script' + style(tf.clear));
});

it('should be thrown and caught', () => {
Expand All @@ -378,6 +377,6 @@ describe('HelpMessage', () => {
expect(help.wrap(0, false)).toEqual('type script');
expect(help.wrap(0, true)).toEqual('type script' + style(tf.clear));
expect(help.wrap(11, false)).toEqual('type script');
expect(help.wrap(11)).toEqual('type script' + style(tf.clear));
expect(help.wrap(11, true)).toEqual('type script' + style(tf.clear));
});
});

0 comments on commit 0fe4242

Please sign in to comment.