Skip to content

Instantly share code, notes, and snippets.

@brand-it
Created March 12, 2022 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brand-it/d4af312ec6a301645471578c6361cc8c to your computer and use it in GitHub Desktop.
Save brand-it/d4af312ec6a301645471578c6361cc8c to your computer and use it in GitHub Desktop.
I don't know who created this origionaly I but I know some one did. I did some googling to find the original but could not.
# PrettyString.new('something').blue
# PrettyString.new('something').red
# PrettyString.new('something').green
# PrettyString.new('something').yellow
# PrettyString.new('something').magenta
# PrettyString.new('something').white
class PrettyString < String
# https://no-color.org/
NO_COLOR = ENV.key?('NO_COLOR') || `tput colors`.chomp.to_i < 8
ANSI_COLORS = {
white: 0,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35
}.freeze
ANSI_COLORS.each do |name, code|
define_method(name) { NO_COLOR ? self : "\e[#{code}m#{self}\e[0m" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment