Module: EverydayCliUtils::Format

Defined in:
lib/everyday-cli-utils/safe/format.rb

Defined Under Namespace

Classes: ColorProfile

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

Class Method Details

.bold(text, fgcolor = nil, bgcolor = nil) ⇒ Object



95
96
97
# File 'lib/everyday-cli-utils/safe/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/safe/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/safe/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/safe/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

.color_profile(id, options = {}) ⇒ Object



139
140
141
142
# File 'lib/everyday-cli-utils/safe/format.rb', line 139

def self.color_profile(id, options = {})
  @color_profiles     ||= {}
  @color_profiles[id] = ColorProfile.new(options[:bold], options[:underline], options[:fgcolor] || nil, options[:bgcolor] || nil)
end

.color_profile_string(id) ⇒ Object



144
145
146
147
148
# File 'lib/everyday-cli-utils/safe/format.rb', line 144

def self.color_profile_string(id)
  @color_profiles ||= {}
  profile         = @color_profiles[id]
  profile.nil? ? nil : self::build_string(profile.bold, profile.underline, profile.fgcolor, profile.bgcolor)
end

.colorize(text, fgcolor = nil, bgcolor = nil) ⇒ Object



91
92
93
# File 'lib/everyday-cli-utils/safe/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/safe/format.rb', line 24

def self::format(text, format_code)
  (format_code.nil? || format_code == '') ? text : "\e[#{format_code}m#{text}\e[0m"
end

.format_all(text) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/everyday-cli-utils/safe/format.rb', line 107

def self::format_all(text)
  colors    = 'bk|rd|gr|yw|bl|pu|cy|wh|no'
  color_map = { 'bk' => :black, 'rd' => :red, 'gr' => :green, 'yw' => :yellow, 'bl' => :blue, 'pu' => :purple, 'cy' => :cyan, 'wh' => :white, 'no' => :none }
  regex     = /\{(.+?)\}\((bd)?(ul)?(?:f(#{colors}))?(?:b(#{colors}))?\)/
  regex2    = /\{(.+?)\}\(:(.+?)\)/
  text.gsub(regex) { |_|
    txt       = $1
    bold      = !$2.nil?
    underline = !$3.nil?
    fg        = $4.nil? ? nil : color_map[$4]
    bg        = $5.nil? ? nil : color_map[$5]
    format(txt, build_string(bold, underline, fg, bg))
  }.gsub(regex2) { |_| format($1, color_profile_string($2.to_sym)) }
end

.handle_bg_color(bgcolor, hit, str) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/everyday-cli-utils/safe/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/safe/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/safe/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/safe/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

.mycenter(str, len, char = ' ') ⇒ Object



129
130
131
132
133
134
135
# File 'lib/everyday-cli-utils/safe/format.rb', line 129

def self.mycenter(str, len, char = ' ')
  tlen = str.gsub(%r{\e\[.*?m}, '').length
  return str if tlen >= len
  b = ((len - tlen) / 2.0).floor
  a = len - tlen - b
  "#{char * b}#{str}#{char * a}"
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/safe/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

.remove_format(text) ⇒ Object



122
123
124
125
126
127
# File 'lib/everyday-cli-utils/safe/format.rb', line 122

def self::remove_format(text)
  colors = 'bk|rd|gr|yw|bl|pu|cy|wh|no'
  regex  = /\{(.+?)\}\((bd)?(ul)?(?:f(#{colors}))?(?:b(#{colors}))?\)/
  regex2 = /\{(.+?)\}\(:(.+?)\)/
  text.gsub(regex, '\1').gsub(regex2, '\1') {}
end

.underline(text, fgcolor = nil, bgcolor = nil) ⇒ Object



99
100
101
# File 'lib/everyday-cli-utils/safe/format.rb', line 99

def self::underline(text, fgcolor = nil, bgcolor = nil)
  self::format(text, self::build_string(false, true, fgcolor, bgcolor))
end