Module: EverydayCliUtils::Format
- Defined in:
- lib/everyday-cli-utils/format.rb
Constant Summary collapse
- FORMAT_TO_CODE =
{ :bold => '1', :underline => '4', }
- FG_COLOR_TO_CODE =
build_format_hash('3')
- BG_COLOR_TO_CODE =
build_format_hash('4')
Class Method Summary collapse
- .bold(text, fgcolor = nil, bgcolor = nil) ⇒ Object
- .boldunderline(text, fgcolor = nil, bgcolor = nil) ⇒ Object
- .build_format_hash(first_chr) ⇒ Object
- .build_string(bold, underline, fgcolor, bgcolor) ⇒ Object
- .colorize(text, fgcolor = nil, bgcolor = nil) ⇒ Object
- .format(text, format_code) ⇒ Object
- .handle_bg_color(bgcolor, hit, str) ⇒ Object
- .handle_bold(bold, hit, str) ⇒ Object
- .handle_fg_color(fgcolor, hit, str) ⇒ Object
- .handle_underline(hit, str, underline) ⇒ Object
- .parse_format(str) ⇒ Object
- .underline(text, fgcolor = nil, bgcolor = nil) ⇒ Object
Class Method Details
.bold(text, fgcolor = nil, bgcolor = nil) ⇒ Object
95 96 97 |
# File 'lib/everyday-cli-utils/format.rb', line 95 def self::bold(text, fgcolor = nil, bgcolor = nil) self::format(text, self::build_string(true, false, fgcolor, bgcolor)) end |
.boldunderline(text, fgcolor = nil, bgcolor = nil) ⇒ Object
103 104 105 |
# File 'lib/everyday-cli-utils/format.rb', line 103 def self::boldunderline(text, fgcolor = nil, bgcolor = nil) self::format(text, self::build_string(true, true, fgcolor, bgcolor)) end |
.build_format_hash(first_chr) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/everyday-cli-utils/format.rb', line 3 def self.build_format_hash(first_chr) { :black => "#{first_chr}0", :red => "#{first_chr}1", :green => "#{first_chr}2", :yellow => "#{first_chr}3", :blue => "#{first_chr}4", :purple => "#{first_chr}5", :cyan => "#{first_chr}6", :white => "#{first_chr}7", :none => nil, } end |
.build_string(bold, underline, fgcolor, bgcolor) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/everyday-cli-utils/format.rb', line 28 def self::build_string(bold, underline, fgcolor, bgcolor) str = '' hit = false hit, str = handle_bold(bold, hit, str) hit, str = handle_underline(hit, str, underline) hit, str = handle_fg_color(fgcolor, hit, str) handle_bg_color(bgcolor, hit, str) end |
.colorize(text, fgcolor = nil, bgcolor = nil) ⇒ Object
91 92 93 |
# File 'lib/everyday-cli-utils/format.rb', line 91 def self::colorize(text, fgcolor = nil, bgcolor = nil) self::format(text, self::build_string(false, false, fgcolor, bgcolor)) end |
.format(text, format_code) ⇒ Object
24 25 26 |
# File 'lib/everyday-cli-utils/format.rb', line 24 def self::format(text, format_code) (format_code.nil? || format_code == '') ? text : "\e[#{format_code}m#{text}\e[0m" end |
.handle_bg_color(bgcolor, hit, str) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/everyday-cli-utils/format.rb', line 63 def self.handle_bg_color(bgcolor, hit, str) unless bgcolor.nil? || BG_COLOR_TO_CODE[bgcolor].nil? str += ';' if hit str += BG_COLOR_TO_CODE[bgcolor] end str end |
.handle_bold(bold, hit, str) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/everyday-cli-utils/format.rb', line 37 def self.handle_bold(bold, hit, str) if bold hit = true str = FORMAT_TO_CODE[:bold] end return hit, str end |
.handle_fg_color(fgcolor, hit, str) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/everyday-cli-utils/format.rb', line 54 def self.handle_fg_color(fgcolor, hit, str) unless fgcolor.nil? || FG_COLOR_TO_CODE[fgcolor].nil? str += ';' if hit hit = true str += FG_COLOR_TO_CODE[fgcolor] end return hit, str end |
.handle_underline(hit, str, underline) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/everyday-cli-utils/format.rb', line 45 def self.handle_underline(hit, str, underline) if underline str += ';' if hit hit = true str += FORMAT_TO_CODE[:underline] end return hit, str end |
.parse_format(str) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/everyday-cli-utils/format.rb', line 71 def self::parse_format(str) parts = str.split(';') bold = false underline = false fgcolor = :none bgcolor = :none parts.each { |v| if v == FORMAT_TO_CODE[:bold] bold = true elsif v == FORMAT_TO_CODE[:underline] underline = true elsif v[0] == '3' fgcolor = FG_COLOR_TO_CODE.invert[v] elsif v[0] == '4' bgcolor = BG_COLOR_TO_CODE.invert[v] end } return bold, underline, fgcolor, bgcolor end |
.underline(text, fgcolor = nil, bgcolor = nil) ⇒ Object
99 100 101 |
# File 'lib/everyday-cli-utils/format.rb', line 99 def self::underline(text, fgcolor = nil, bgcolor = nil) self::format(text, self::build_string(false, true, fgcolor, bgcolor)) end |