-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Colors
module to help users set text colors
- Loading branch information
1 parent
04947f2
commit 4e70dd8
Showing
5 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |