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

Helpers::Text: Add .colorize(color, text, **opts) #2150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 38 additions & 13 deletions lib/pry/helpers/text.rb
Expand Up @@ -8,37 +8,62 @@ module Text
extend self

COLORS = {
"black" => 0,
"red" => 1,
"green" => 2,
"yellow" => 3,
"blue" => 4,
"purple" => 5,
"magenta" => 5,
"cyan" => 6,
"white" => 7
"black" => 30,
"red" => 31,
"green" => 32,
"yellow" => 33,
"blue" => 34,
"purple" => 35,
"magenta" => 35,
"cyan" => 36,
"white" => 37
}.freeze

COLORS.each_pair do |color, value|
define_method color do |text|
"\033[0;#{30 + value}m#{text}\033[0m"
"\033[0;#{value}m#{text}\033[0m"
end

define_method "bright_#{color}" do |text|
"\033[1;#{30 + value}m#{text}\033[0m"
"\033[1;#{value}m#{text}\033[0m"
end

COLORS.each_pair do |bg_color, bg_value|
define_method "#{color}_on_#{bg_color}" do |text|
"\033[0;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
"\033[0;#{value};#{bg_value+10}m#{text}\033[0m"
end

define_method "bright_#{color}_on_#{bg_color}" do |text|
"\033[1;#{30 + value};#{40 + bg_value}m#{text}\033[0m"
"\033[1;#{value};#{bg_value+10}m#{text}\033[0m"
end
end
end

# Apply _color_ or style to _text_
#
# @param text [String]
# @param color [nil, Symbol] Color selected from `COLORS`
# @option bold [nil, true, false]
# @option faded [nil, true, false]
# @return [String] Text
def colorize(text, color = nil, bold: false, faded: false)
b = []
b << 1 if bold
b << 2 if faded
b << 0 if b.empty?

escape(text, b, COLORS[color.to_s])
end

# Escape _text_ with SGR escape _codes_
#
# @param text [String]
# @param *codes [Integer] SGR code
def escape(text, *codes)
seq = codes.compact.join(";")
"\e[#{seq}m#{text}\e[0m"
end

# Remove any color codes from _text_.
#
# @param [String, #to_s] text
Expand Down
47 changes: 47 additions & 0 deletions spec/helpers/text_spec.rb
@@ -0,0 +1,47 @@
# frozen_string_literal: true

describe Pry::Helpers::Text do
describe ".colorize" do
include Pry::Helpers::Text

it "with color" do
expect(
colorize("Pry", :blue)
).to eq(
"\e[0;34mPry\e[0m"
)
end

it "with bold" do
expect(
colorize("Pry", bold: true)
).to eq(
"\e[1mPry\e[0m"
)
end

it "with color and bold" do
expect(
colorize("Pry", :blue, bold: true)
).to eq(
"\e[1;34mPry\e[0m"
)
end

it "with color, faded, and bold" do
expect(
colorize("Pry", :blue, bold: true, faded: true)
).to eq(
"\e[1;2;34mPry\e[0m"
)
end

it "unchanged" do
expect(
colorize("Pry", nil)
).to eq(
"\e[0mPry\e[0m"
)
end
end
end