Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to wrap description text by terminal width? #1785

Closed
cobbzilla opened this issue Aug 30, 2022 · 4 comments
Closed

Best way to wrap description text by terminal width? #1785

cobbzilla opened this issue Aug 30, 2022 · 4 comments
Milestone

Comments

@cobbzilla
Copy link

cobbzilla commented Aug 30, 2022

this is of minor priority

When I put in long lines in my description, they get chopped mid-word on "max 80-char width" displays; this looks bad, it's hard to read.

So I limit my description text to max 80 chars per line, but it's weird then to do -h and see the options take advantage of the much wider terminal, while the description is hard-coded to "max 80".

How can I display descriptions according to terminal width? Ideally I only put newlines where they make semantic sense, and let another layer handle the wrapping (on words, though, not chars).

I'm hoping there is already a way to do this that I haven't found, but I haven't found it :)

@shadowspawn
Copy link
Collaborator

shadowspawn commented Aug 30, 2022

The command description is not currently being word-wrapped. (I thought it was!)
I think it probably could be.

(The wrapping got developed in #956 and was focussed on option and subcommand and argument descriptions.)

@shadowspawn
Copy link
Collaborator

This is doable now by subclassing the Help class. Perhaps more than you are willing to do, but I wanted to check it was possible!

A trap to be aware of is that no wrapping is done if the text includes a line-feed followed by whitespace, in case the text was manually formatted already. A single line-feed followed by text is fine.

const { Command, Help } = require('commander');

class MyHelp extends Help {
  commandDescription(cmd) {
    const description = cmd.description();
    if (!description) return '';
    const helpWidth = this.helpWidth || 80;
    return this.wrap(description, helpWidth, 0);
  }
}

class MyCommand extends Command {
  createCommand(name) {
    return new MyCommand(name);
  }
  createHelp() {
    return new MyHelp;
  }
}

const program = new MyCommand();

program
  .description(`. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
this long line will be wrapped without breaking words across lines if there is enough room to make it worthwhile
and this text is on another line`);

program.parse();
$ node index.js -h
Usage: index [options]

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
this long line will be wrapped without breaking words across
lines if there is enough room to make it worthwhile
and this text is on another line

Options:
  -h, --help  display help for command

@shadowspawn
Copy link
Collaborator

Adding wrapping for (full) command description in #1804

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Oct 2, 2022
@shadowspawn shadowspawn added this to the v10.0.0 milestone Dec 8, 2022
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jan 14, 2023
@shadowspawn
Copy link
Collaborator

Released in Commander v10.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants