Skip to content

Commit

Permalink
Add Colors module to help users set text colors
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavothecoder committed Dec 2, 2024
1 parent 04947f2 commit 4e70dd8
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 1 deletion.
106 changes: 106 additions & 0 deletions lib/dry/cli/colors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

module Dry
class CLI
# Collection of functions to colorize text.
#
# @since 1.3.0
module Colors
# since 1.3.0
def bold(text)
ensure_clean_sequence("\e[1m#{text}")
end

# since 1.3.0
def black(text)
ensure_clean_sequence("\e[30m#{text}")
end

# since 1.3.0
def red(text)
ensure_clean_sequence("\e[31m#{text}")
end

# since 1.3.0
def green(text)
ensure_clean_sequence("\e[32m#{text}")
end

# since 1.3.0
def yellow(text)
ensure_clean_sequence("\e[33m#{text}")
end

# since 1.3.0
def blue(text)
ensure_clean_sequence("\e[34m#{text}")
end

# since 1.3.0
def magenta(text)
ensure_clean_sequence("\e[35m#{text}")
end

# since 1.3.0
def cyan(text)
ensure_clean_sequence("\e[36m#{text}")
end

# since 1.3.0
def white(text)
ensure_clean_sequence("\e[37m#{text}")
end

# since 1.3.0
def on_black(text)
ensure_clean_sequence("\e[40m#{text}")
end

# since 1.3.0
def on_red(text)
ensure_clean_sequence("\e[41m#{text}")
end

# since 1.3.0
def on_green(text)
ensure_clean_sequence("\e[42m#{text}")
end

# since 1.3.0
def on_yellow(text)
ensure_clean_sequence("\e[43m#{text}")
end

# since 1.3.0
def on_blue(text)
ensure_clean_sequence("\e[44m#{text}")
end

# since 1.3.0
def on_magenta(text)
ensure_clean_sequence("\e[45m#{text}")
end

# since 1.3.0
def on_cyan(text)
ensure_clean_sequence("\e[46m#{text}")
end

# since 1.3.0
def on_white(text)
ensure_clean_sequence("\e[47m#{text}")
end

private

# @since 1.3.0
# @api private
def ensure_clean_sequence(text)
clen_text = text
clear = "\e[0m"
clen_text += clear unless text.end_with?(clear)
clen_text
end
end
end
end
3 changes: 3 additions & 0 deletions lib/dry/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

require "forwardable"
require "dry/cli/option"
require "dry/cli/colors"

module Dry
class CLI
# Base class for commands
#
# @since 0.1.0
class Command
include Colors

# @since 0.1.0
# @api private
def self.inherited(base)
Expand Down
49 changes: 49 additions & 0 deletions spec/support/fixtures/colors
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift "#{__dir__}/../../../lib"
require "dry/cli"

module ColorsDemo
extend Dry::CLI::Registry

class Print < Dry::CLI::Command
desc "Demonstrate all available colors"

# rubocop:disable Metrics/AbcSize
def call
demo = <<~DEMO
# Style ###############################################################
Bold: #{bold("This is bold")}
# Foreground ##########################################################
Black: #{black("This is black")}
Red: #{red("This is red")}
Green: #{green("This is green")}
Yellow: #{yellow("This is yellow")}
Blue: #{blue("This is blue")}
Magenta: #{magenta("This is magenta")}
Cyan: #{cyan("This is cyan")}
White: #{white("This is white")}
# Background ##########################################################
Black: #{on_black("This is black")}
Red: #{on_red("This is red")}
Green: #{on_green("This is green")}
Yellow: #{on_yellow("This is yellow")}
Blue: #{on_blue("This is blue")}
Magenta: #{on_magenta("This is magenta")}
Cyan: #{on_cyan("This is cyan")}
White: #{on_white("This is white")}
# Combinations ########################################################
Bold+Foreground: #{bold(red("This is bold red"))}
Bold+Background: #{bold(on_green("This is bold on green"))}
Bold+Foreground+Background: #{bold(red(on_green("This is bold red on green")))}
DEMO
puts demo
end
# rubocop:enable Metrics/AbcSize
end

register "print", Print
end

Dry.CLI(ColorsDemo).call
2 changes: 1 addition & 1 deletion spec/support/fixtures/foo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Foo
]

def call(engine: nil, **)
puts "console - engine: #{engine}"
puts blue(bold("console - engine: ")), magenta(engine.to_s)
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/unit/dry/cli/colors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

RSpec.describe "Colors" do
module Dummy
module_function

include Dry::CLI::Colors

def only_bold
bold("one")
end

def bold_and_black
black(bold("two"))
end
end

it "doesn't duplicate clean escape sequences" do
only_bold = Dummy.only_bold
expect(only_bold.end_with?("\e[0m")).to eq(true)
expect(only_bold.end_with?("\e[0m\e[0m")).to eq(false)
bold_and_black = Dummy.bold_and_black
expect(bold_and_black.end_with?("\e[0m")).to eq(true)
expect(bold_and_black.end_with?("\e[0m\e[0m")).to eq(false)
end
end

0 comments on commit 4e70dd8

Please sign in to comment.