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

Add color support for help option #301

Open
hcodes opened this issue Dec 8, 2014 · 15 comments
Open

Add color support for help option #301

hcodes opened this issue Dec 8, 2014 · 15 comments

Comments

@hcodes
Copy link

hcodes commented Dec 8, 2014

No description provided.

@hcodes
Copy link
Author

hcodes commented Dec 8, 2014

As example:
cli_color

@tenorok

This comment was marked as outdated.

1 similar comment
@arikon

This comment was marked as outdated.

@jd327

This comment was marked as outdated.

@kevyworks

This comment was marked as outdated.

@shadowspawn
Copy link
Collaborator

This has gathered a few likes over the years, so not closing quite yet. I have some problems with choosing colors:

  • color choice is quite personal
  • color support in some Windows shells is poor
  • light and dark themed shells makes color choice harder

I suspect it is too much trouble to build in directly, but perhaps commander can make it easier for people to customise the help output.

@shadowspawn

This comment was marked as outdated.

@trusktr
Copy link

trusktr commented Apr 15, 2020

Sywac isn't well documented, but it has a way to specify colors like this:

// See http://sywac.io
const cli = require('sywac')

cli
  .string('-c, --cloudEnv <env>', {
    description: `The cloud env to use.`,
    required: true,
  })
  .version('-v, --version')
  .help('-h, --help')
  .showHelpByDefault()
  // Add colors to the help output!
  .style({
    usagePrefix: str => chalk().yellow(str.slice(0, 6)) + ' ' + chalk().bold(str.slice(7)),
    usageCommandPlaceholder: str => chalk().bold(str),
    usagePositionals: str => chalk().bold(str),
    usageArgsPlaceholder: str => chalk().bold(str),
    usageOptionsPlaceholder: str => chalk().bold(str),
    group: str => chalk().yellow(str),
    flags: str => chalk().bold(str),
    desc: str => chalk().cyan(str),
    hints: str => chalk().gray.dim(str),
    groupError: str => chalk().red.bold(str),
    flagsError: str => chalk().red.bold(str),
    descError: str => chalk().red.bold(str),
    hintsError: str => chalk().red(str),
    messages: str => chalk().red.bold(str), // these are error messages
  })
  .parseAndExit()
  .then(main)
  .catch(e => {
    console.error(e)
    process.exit(1)
  })

let c
function chalk() {
  // lazily load chalk, only used if help text displayed
  if (!c) c = require('chalk')
  return c
}

async function main(opts) {
  const {cloudEnv} = opts
  console.log(cloudEnv)
}

Example output with color:

color

It'd be neat to have some sort of color configurability for Commander.

@ivands
Copy link

ivands commented Nov 30, 2020

It would still be nice to have a more simple api to change colors & styles

@shadowspawn

This comment was marked as outdated.

@onury
Copy link

onury commented Aug 9, 2024

IMO, color support is crucial for a CLI tool.
I understand the "no dependency" policy. commander should not do it itself,
but at least provide a legal & easy way for the user.

// user code:
import { Command } from 'commander';
import c from 'chalk';

const formatter = {
  helpWidth: 120,
  indent: 2,
  titles: str => c.yellow.bold(str),
  usage: str => c.gray(str),
  option: flags => c.green(flags),
  command: (name, args) => c.green(name) + '  ' + c.gray(args),
  description: desc => c.white(desc),
  default: val => `[default: ${c.gray(val)}}`
};

const program = new Command();
program
  .name('colorful-cli')
  .usage('<command> [options]')
  .version(pkg.version)
  .format(formatter)            // <—— pass formatter object to commander
  .option('--debug', 'output extra logs')
  // ... more commands & options...

Pls re-open this issue.

@shadowspawn

This comment was marked as outdated.

@shadowspawn shadowspawn reopened this Sep 17, 2024
@shadowspawn
Copy link
Collaborator

This old issue has been getting recent upvotes, and I have started looking at what is needed to make it easier for authors to provide custom colours in the help. I'll reopen at least while I am working on it.

@shadowspawn

This comment was marked as outdated.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Oct 8, 2024

Open draft PR in #2251. Added a style layer of simple hooks so don't need to subclass to add colour:

program.configureHelp({
  styleTitle: (str) => styleText('bold', str),
  styleCommandText: (str) => styleText('cyan', str),
  styleCommandDescription: (str) => styleText('magenta', str),
  styleItemDescription: (str) => styleText('italic', str),
  styleOptionText: (str) => styleText('green', str),
  styleArgumentText: (str) => styleText('yellow', str),
  styleSubcommandText: (str) => styleText('blue', str),
});
Screenshot 2024-10-12 at 14 33 20

@shadowspawn shadowspawn pinned this issue Oct 12, 2024
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

9 participants