Skip to content
Myr

Rename columns to helpWidth (#1396)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by GitHub · parent 4555881 · Browse files · 6 files+58 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+58 added−58 removed

  • tests:+37 added−37 removed
  • other:+21 added−21 removed

6 files+58 added−58 removed

modified:tests/command.configureOutput.test.js+32 added-32 removed
105 unmodified lines
106 expect(customWrite).toHaveBeenCalledTimes(1);107 writeSpy.mockRestore();108});109110test('when default getOutColumns then help columns from stdout', () => {110test('when default getOutHelpWidth then help helpWidth from stdout', () => {111 const expectedColumns = 123;112 const holdIsTTY = process.stdout.isTTY;113 const holdColumns = process.stdout.columns;114 let helpColumns;114 let helpWidth;115116 process.stderr.isTTY = true;117 process.stdout.columns = expectedColumns;118 process.stdout.isTTY = true;119 const program = new commander.Command();120 program121 .configureHelp({122 formatHelp: (cmd, helper) => {123 helpColumns = helper.columns;123 helpWidth = helper.helpWidth;124 return '';125 }126 });127 program.outputHelp();128129 expect(helpColumns).toBe(expectedColumns);129 expect(helpWidth).toBe(expectedColumns);130 process.stdout.columns = holdColumns;131 process.stdout.isTTY = holdIsTTY;132});133134test('when custom getOutColumns then help columns custom', () => {134test('when custom getOutHelpWidth then help helpWidth custom', () => {135 const expectedColumns = 123;136 let helpColumns;136 let helpWidth;137138 const program = new commander.Command();139 program140 .configureHelp({141 formatHelp: (cmd, helper) => {142 helpColumns = helper.columns;142 helpWidth = helper.helpWidth;143 return '';144 }145 }).configureOutput({146 getOutColumns: () => expectedColumns146 getOutHelpWidth: () => expectedColumns147 });148 program.outputHelp();149150 expect(helpColumns).toBe(expectedColumns);150 expect(helpWidth).toBe(expectedColumns);151});152153test('when default getErrColumns then help error columns from stderr', () => {153test('when default getErrHelpWidth then help error helpWidth from stderr', () => {154 const expectedColumns = 123;155 const holdIsTTY = process.stderr.isTTY;156 const holdColumns = process.stderr.columns;157 let helpColumns;157 let helpWidth;158159 process.stderr.isTTY = true;160 process.stderr.columns = expectedColumns;161 const program = new commander.Command();162 program163 .configureHelp({164 formatHelp: (cmd, helper) => {165 helpColumns = helper.columns;165 helpWidth = helper.helpWidth;166 return '';167 }168 });169 program.outputHelp({ error: true });170171 expect(helpColumns).toBe(expectedColumns);171 expect(helpWidth).toBe(expectedColumns);172 process.stderr.isTTY = holdIsTTY;173 process.stderr.columns = holdColumns;174});175176test('when custom getErrColumns then help error columns custom', () => {176test('when custom getErrHelpWidth then help error helpWidth custom', () => {177 const expectedColumns = 123;178 let helpColumns;178 let helpWidth;179180 const program = new commander.Command();181 program182 .configureHelp({183 formatHelp: (cmd, helper) => {184 helpColumns = helper.columns;184 helpWidth = helper.helpWidth;185 return '';186 }187 }).configureOutput({188 getErrColumns: () => expectedColumns188 getErrHelpWidth: () => expectedColumns189 });190 program.outputHelp({ error: true });191192 expect(helpColumns).toBe(expectedColumns);192 expect(helpWidth).toBe(expectedColumns);193});194195test('when custom getOutColumns and configureHelp:columns then help columns from configureHelp', () => {195test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => {196 const expectedColumns = 123;197 let helpColumns;197 let helpWidth;198199 const program = new commander.Command();200 program201 .configureHelp({202 formatHelp: (cmd, helper) => {203 helpColumns = helper.columns;203 helpWidth = helper.helpWidth;204 return '';205 },206 columns: expectedColumns206 helpWidth: expectedColumns207 }).configureOutput({208 getOutColumns: () => 999208 getOutHelpWidth: () => 999209 });210 program.outputHelp();211212 expect(helpColumns).toBe(expectedColumns);212 expect(helpWidth).toBe(expectedColumns);213});214215test('when custom getErrColumns and configureHelp:columns then help error columns from configureHelp', () => {215test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => {216 const expectedColumns = 123;217 let helpColumns;217 let helpWidth;218219 const program = new commander.Command();220 program221 .configureHelp({222 formatHelp: (cmd, helper) => {223 helpColumns = helper.columns;223 helpWidth = helper.helpWidth;224 return '';225 },226 columns: expectedColumns226 helpWidth: expectedColumns227 }).configureOutput({228 getErrColumns: () => 999228 getErrHelpWidth: () => 999229 });230 program.outputHelp({ error: true });231232 expect(helpColumns).toBe(expectedColumns);232 expect(helpWidth).toBe(expectedColumns);233});234235test('when set configureOutput then get configureOutput', () => {236 const outputOptions = {237 writeOut: jest.fn(),238 writeErr: jest.fn(),239 getOutColumns: jest.fn(),240 getErrColumns: jest.fn(),239 getOutHelpWidth: jest.fn(),240 getErrHelpWidth: jest.fn(),241 outputError: jest.fn()242 };243 const program = new commander.Command();244 program.configureOutput(outputOptions);
31 unmodified lines
modified:tests/help.wrap.test.js+5 added-5 removed
modified:typings/commander-tests.ts+3 added-3 removed
268 unmodified lines
269270program.configureOutput({271 writeOut: (str: string) => { },272 writeErr: (str: string) => { },273 getOutColumns: () => 80,274 getErrColumns: () => 80,273 getOutHelpWidth: () => 80,274 getErrHelpWidth: () => 80,275 outputError: (str: string, write: (str: string) => void) => { }276});277278// Help279const helper = new commander.Help();280const helperCommand = new commander.Command();281const helperOption = new commander.Option('-a, --all');282283helper.columns = 3;283helper.helpWidth = 3;284helper.sortSubcommands = true;285helper.sortOptions = false;286287const subcommandTermStr: string = helper.subcommandTerm(helperCommand);
53 unmodified lines
modified:typings/index.d.ts+7 added-7 removed
74 unmodified lines
75 }76 type OptionConstructor = new (flags: string, description?: string) => Option;7778 interface Help {79 /** output columns, long lines are wrapped to fit */80 columns?: number;79 /** output helpWidth, long lines are wrapped to fit */80 helpWidth?: number;81 sortSubcommands: boolean;82 sortOptions: boolean;8384 /** Get the command term to show in the list of subcommands. */
50 unmodified lines
135 }136 interface OutputConfiguration {137 writeOut?(str: string): void;138 writeErr?(str: string): void;139 getOutColumns?(): number;140 getErrColumns?(): number;139 getOutHelpWidth?(): number;140 getErrHelpWidth?(): number;141 outputError?(str: string, write: (str: string) => void): void;142143 }144
119 unmodified lines
264 *265 * // functions to change where being written, stdout and stderr266 * writeOut(str)267 * writeErr(str)268 * // matching functions to specify columns for wrapping help269 * getOutColumns()270 * getErrColumns()268 * // matching functions to specify width for wrapping help269 * getOutHelpWidth()270 * getErrHelpWidth()271 * // functions based on what is being written out272 * outputError(str, write) // used for displaying errors, and not used for displaying help273 */274 configureOutput(configuration: OutputConfiguration): this;
314 unmodified lines
modified:index.js+10 added-10 removed
10 unmodified lines
1112// Although this is a class, methods are static in style to allow override using subclass or just functions.13class Help {14 constructor() {15 this.columns = undefined;15 this.helpWidth = undefined;16 this.sortSubcommands = false;17 this.sortOptions = false;18 }19
222 unmodified lines
242 */243244 formatHelp(cmd, helper) {245 const termWidth = helper.padWidth(cmd, helper);246 const columns = helper.columns || 80;246 const helpWidth = helper.helpWidth || 80;247 const itemIndentWidth = 2;248 const itemSeparatorWidth = 2; // between term and description249 function formatItem(term, description) {250 if (description) {251 const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;252 return helper.wrap(fullText, columns - itemIndentWidth, termWidth + itemSeparatorWidth);252 return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);253 }254 return term;255 };256 function formatList(textArray) {
292 unmodified lines
549 // see .configureOutput() for docs550 this._outputConfiguration = {551 writeOut: (str) => process.stdout.write(str),552 writeErr: (str) => process.stderr.write(str),553 getOutColumns: () => process.stdout.isTTY ? process.stdout.columns : undefined,554 getErrColumns: () => process.stderr.isTTY ? process.stderr.columns : undefined,553 getOutHelpWidth: () => process.stdout.isTTY ? process.stdout.columns : undefined,554 getErrHelpWidth: () => process.stderr.isTTY ? process.stderr.columns : undefined,555 outputError: (str, write) => write(str)556 };557558 this._hidden = false;
124 unmodified lines
683 *684 * // functions to change where being written, stdout and stderr685 * writeOut(str)686 * writeErr(str)687 * // matching functions to specify columns for wrapping help688 * getOutColumns()689 * getErrColumns()687 * // matching functions to specify width for wrapping help688 * getOutHelpWidth()689 * getErrHelpWidth()690 * // functions based on what is being written out691 * outputError(str, write) // used for displaying errors, and not used for displaying help692 *693 * @param {Object} [configuration] - configuration options
1194 unmodified lines
1888 */18891890 helpInformation(contextOptions) {1891 const helper = this.createHelp();1892 if (helper.columns === undefined) {1893 helper.columns = (contextOptions && contextOptions.error) ? this._outputConfiguration.getErrColumns() : this._outputConfiguration.getOutColumns();1892 if (helper.helpWidth === undefined) {1893 helper.helpWidth = (contextOptions && contextOptions.error) ? this._outputConfiguration.getErrHelpWidth() : this._outputConfiguration.getOutHelpWidth();1894 }1895 return helper.formatHelp(this, helper);1896 };1897
265 unmodified lines
modified:Readme.md+1 added-1 removed
630 unmodified lines
631You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.632633The data properties are:634635- `columns`: specify the wrap width, useful for unit tests635- `helpWidth`: specify the wrap width, useful for unit tests636- `sortSubcommands`: sort the subcommands alphabetically637- `sortOptions`: sort the options alphabetically638639There are methods getting the visible lists of arguments, options, and subcommands. There are methods for formatting the items in the lists, with each item having a _term_ and _description_. Take a look at `.formatHelp()` to see how they are used.
235 unmodified lines