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

Support color ansi code sequences in custom help #2251

Draft
wants to merge 33 commits into
base: develop
Choose a base branch
from

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Sep 27, 2024

Pull Request

Feedback welcome at this stage.

Problem

People would like to add color to the Help: #301 (opened 10 years ago!).

Part of the intent of #1365 was to allow adding colour, but due to interactions with padding and wrapping it was too hard.

Solution

Make it easy to add colour and styling. Treat ansi code sequences as zero width when padding and wrapping.

There is a new layer of style hooks which are just passed a string. There is not an assumption that only the style hooks do styling. So for example either of optionTerm() or styleOptionTerm() could add styling.

Build in support for common patterns for controlling colour (like NO_COLOR), and check output stream for color support.

Strip escape sequences if necessary before displaying. So author does not need to check whether to use color when styling strings.

Refactor complex wrap() into more focused formatItem() and boxWrap().

Now easy to do some previously messy help customisations (examples added):

  • man-style help with item descriptions on new lines
  • use util:styleText to add color to help
  • use Chalk for color and padding and wrapping support and completely replace default implementation

Changes from previous implementation

Wrapping of a very long single "word" is now a soft wrap rather than a hard wrap. Simpler implementation, and probably rare.

Dropped the partial wrapping support for Zero Width Space (ZWSP) as not displayed as zero width in Mac Terminal (among others), not consistent with general treatment of ansi escape sequences in this PR, and unlikely to be used by authors. For interest, another similar special character is the the soft hyphen.

ToDo:

  • tests
  • README/docs

ChangeLog

Add

  • support for adding custom color and style to Help output
  • simple hooks for styling Help using Command.configureHelp()
    • styleTitle()
    • styleUsage() and styleCommandText
    • styleItemDescription(), styleCommandDescription(), and styleDescriptionText()
    • styleOptionTerm(), styleSubcommandTerm(), and styleArgumentTerm()
    • styleOptionText(), styleSubcommandText(), and styleArgumentText()
  • new Help class methods to support color and allow easier customisation
    • minWidthToWrap property
    • prepareContext()
    • displayWidth()
    • boxWrap()
    • formatItem()
    • preformatted()
  • Command.configureOutput() now includes getOutHasColors(), getErrHasColours(), and stripAnsi()

Deleted

  • Help.wrap() (refactored into formatItem and boxWrap)

@shadowspawn shadowspawn changed the base branch from master to develop September 27, 2024 04:53
@shadowspawn
Copy link
Collaborator Author

shadowspawn commented Oct 9, 2024

I added a whole bunch of simple style hooks (from #2262). Some notes on current state of play.

I am not planning to add full JSDoc typing for all the style routines, keep the code compact. Most of them are one line of code.

The implementation of styleUsage is not super robust. But it does allow author to have matching colours for options and subcommands and arguments in the usage and the relevant section in help with just 3 simple style hooks.

It would reduce some boilerplate code if optionTerm called styleOptionTerm (and all the other similar pairs!), but I currently prefer the clean separation.

It is currently fine for authors to put color into the methods like optionTerm, they don't have to use styleOptionTerm.

The style routines get called even if color will ultimately be suppressed. The author does not need to know the rules. The current implementation strips colours later if color is not appropriate.

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

Successfully merging this pull request may close these issues.

1 participant