dry-rb / dry-cli

General purpose Command Line Interface (CLI) framework for Ruby
https://dry-rb.org/gems/dry-cli
MIT License
327 stars 41 forks source link

Feature request: colorized output #121

Open cllns opened 2 years ago

cllns commented 2 years ago

This is a feature I started working on long ago for Hanami: https://github.com/hanami/hanami/pull/906

There could be two parts to this:

e.g.

say "create", color: :red

This should probably be like print rather than puts (no trailing newline), since people should be able to print in several colors on one line.

The concrete use-case for this could be like hanami/hanami#906, being able to colorize the output of file generation for Hanami.

Is this something we'd be interested in adding to dry-cli? Any thoughts on the API?

cllns commented 2 years ago

Another idea for an API could be something like:

class MyGenerator
  include Dry::CLI::Colors

  def call
    puts Red["create"], " ", Green["lib/foo.rb"], " ", RGB["Main file", 55, 66, 77]
  end
end

This avoids the issue of needing to have two different method (one with a trailing newline and another without), or needing a special output method at all.

May want to add TextColors and BackgroundColors if that's desired, but I feel like that could be deferred.

cllns commented 2 years ago

We may also want to add bold, underline, etc: https://stackoverflow.com/a/42449998

Those could be extracted as Dry::CLI::Styles (either separately from Colors, or Styles could have them all together)

RomanTurner commented 1 year ago

@cllns Any updates with this? I think this would be a great feature. I know they have something like this in Thor if there is needed inspiration Thor:Color