Module: Common::Color
- Defined in:
- lib/common/color.rb
Overview
Module which allows to color the text in teh bash screen
Instance Method Summary collapse
-
#align_right(length, console_length) ⇒ Object
Align a message (OK, FAIL, WARN) to the right.
-
#color_blue(text, bold = false) ⇒ Object
Colorize the text in blue.
-
#color_green(text, bold = false) ⇒ Object
Colorize the text in green.
-
#color_normal(text, bold = false) ⇒ Object
Colorize the text in normal color.
-
#color_red(text, bold = false) ⇒ Object
Colorize the text in red.
-
#color_white(text, bold = false) ⇒ Object
Colorize the text in white.
-
#color_yellow(text, bold = false) ⇒ Object
Colorize the text in yellow.
-
#colorize_background(text, color) ⇒ Object
Colorize the text in backgroung.
-
#colorize_text(text, color, bold) ⇒ Object
Colorize the text.
-
#echo_fail(stdout = STDOUT) ⇒ Object
Echo FAIL in the bash screen.
-
#echo_ok(stdout = STDOUT) ⇒ Object
Echo OK in the bash screen.
-
#echo_warn(stdout = STDOUT) ⇒ Object
Echo FAIL in the bash screen.
-
#format_configs(confs, prefix = "\t") ⇒ Object
Format the config.
-
#format_state(state) ⇒ Object
Format the state.
-
#print_log(msg_type, msg, stdout = STDOUT) ⇒ Object
Print a message in log.
-
#printf_blue(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in blue.
-
#printf_color(text, color, bold, stdout = STDOUT) ⇒ Object
Print a color text.
-
#printf_green(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in green.
-
#printf_normal(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in normal color.
-
#printf_red(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in red.
-
#printf_white(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in white.
-
#printf_yellow(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text yellow.
-
#which_color(state) ⇒ Object
Determines the color depending on the state.
Instance Method Details
#align_right(length, console_length) ⇒ Object
Align a message (OK, FAIL, WARN) to the right.
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/common/color.rb', line 209 def align_right(length, console_length) nb_spaces_to_print=console_length.to_i - length - 1 i=0 str="" while i < nb_spaces_to_print do str+=" " i+=1 end return str end |
#color_blue(text, bold = false) ⇒ Object
Colorize the text in blue.
97 98 99 |
# File 'lib/common/color.rb', line 97 def color_blue(text, bold=false) return colorize_text(text, "1;34", bold) end |
#color_green(text, bold = false) ⇒ Object
Colorize the text in green.
80 81 82 |
# File 'lib/common/color.rb', line 80 def color_green(text, bold=false) return colorize_text(text, 32, bold) end |
#color_normal(text, bold = false) ⇒ Object
Colorize the text in normal color.
131 132 133 |
# File 'lib/common/color.rb', line 131 def color_normal(text, bold=false) return colorize_text(text, 0, bold) end |
#color_red(text, bold = false) ⇒ Object
Colorize the text in red.
63 64 65 |
# File 'lib/common/color.rb', line 63 def color_red(text, bold=false) return colorize_text(text, 31, bold) end |
#color_white(text, bold = false) ⇒ Object
Colorize the text in white.
114 115 116 |
# File 'lib/common/color.rb', line 114 def color_white(text, bold=false) return colorize_background(colorize_text(text, 37, bold), 16) end |
#color_yellow(text, bold = false) ⇒ Object
Colorize the text in yellow.
148 149 150 |
# File 'lib/common/color.rb', line 148 def color_yellow(text, bold=false) return colorize_text(text, 33, bold) end |
#colorize_background(text, color) ⇒ Object
Colorize the text in backgroung.
31 32 33 |
# File 'lib/common/color.rb', line 31 def colorize_background(text, color) return "\033[48;5;#{color}m#{text}\033[0m" end |
#colorize_text(text, color, bold) ⇒ Object
Colorize the text.
40 41 42 43 44 45 46 47 48 |
# File 'lib/common/color.rb', line 40 def colorize_text(text, color, bold) # set bold if bold == nil || bold == false || bold == 0 bold=0 else bold=1 end return "\033[#{bold};#{color}m#{text}\033[0m" end |
#echo_fail(stdout = STDOUT) ⇒ Object
Echo FAIL in the bash screen.
162 163 164 |
# File 'lib/common/color.rb', line 162 def echo_fail(stdout = STDOUT) stdout.print color_white("[ ", true) + color_red("FAIL") + color_white(" ]\n", true) end |
#echo_ok(stdout = STDOUT) ⇒ Object
Echo OK in the bash screen.
155 156 157 |
# File 'lib/common/color.rb', line 155 def echo_ok(stdout = STDOUT) stdout.print color_white("[ ", true) + color_green("OK") + color_white(" ]\n", true) end |
#echo_warn(stdout = STDOUT) ⇒ Object
Echo FAIL in the bash screen.
169 170 171 |
# File 'lib/common/color.rb', line 169 def echo_warn(stdout = STDOUT) stdout.print color_white("[ ", true) + color_yellow("WARN") + color_white(" ]\n", true) end |
#format_configs(confs, prefix = "\t") ⇒ Object
Format the config.
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/common/color.rb', line 253 def format_configs(confs, prefix="\t") if confs.length > 0 output = "\n" confs.each do |confName, confTag| output += prefix + confName.to_s output += "(tag=" + confTag['tag'].to_s + ")" unless confTag['tag'].nil? output += "\n" end else output = prefix + "None\n" end return output end |
#format_state(state) ⇒ Object
Format the state.
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/common/color.rb', line 238 def format_state(state) case which_color(state) when "green" return color_green(state) when "red" return color_red(state) else return color_yellow(state) end end |
#print_log(msg_type, msg, stdout = STDOUT) ⇒ Object
Print a message in log.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/common/color.rb', line 178 def print_log(msg_type, msg, stdout = STDOUT) date = "[" + Time.now.strftime("%Y-%m-%d %H:%M:%S") + "] " msg_length = date.length + msg.length msg = date + msg right_margin = 5 max_consolelength = 80 console_length=`tput cols` if console_length.to_i > max_consolelength console_length = max_consolelength end msg_length = 8 + right_margin + msg_length % console_length.to_i stdout.printf(color_white(msg, 1)) printf_normal(align_right(msg_length, console_length), stdout) case msg_type when "OK", "ok" echo_ok when "FAIL", "fail" echo_fail when "WARN", "warn" echo_warn end end |
#printf_blue(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in blue.
89 90 91 |
# File 'lib/common/color.rb', line 89 def printf_blue(text, bold=false, stdout = STDOUT) printf_color(text, "1;34", bold, stdout) end |
#printf_color(text, color, bold, stdout = STDOUT) ⇒ Object
Print a color text.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/common/color.rb', line 16 def printf_color(text, color, bold, stdout = STDOUT) # set bold if bold == nil || bold == false || bold == 0 bold=0 else bold=1 end stdout.printf("\033[%d;%dm%s\033[0m", bold, color, text) end |
#printf_green(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in green.
72 73 74 |
# File 'lib/common/color.rb', line 72 def printf_green(text, bold=false, stdout = STDOUT) printf_color(text, 32, bold, stdout) end |
#printf_normal(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in normal color.
123 124 125 |
# File 'lib/common/color.rb', line 123 def printf_normal(text, bold=false, stdout = STDOUT) printf_color(text, 0, bold, stdout) end |
#printf_red(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in red.
55 56 57 |
# File 'lib/common/color.rb', line 55 def printf_red(text, bold=false, stdout = STDOUT) printf_color(text, 31, bold, stdout) end |
#printf_white(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text in white.
106 107 108 |
# File 'lib/common/color.rb', line 106 def printf_white(text, bold=false, stdout = STDOUT) printf_color(text, 37, bold, stdout) end |
#printf_yellow(text, bold = false, stdout = STDOUT) ⇒ Object
Print the text yellow.
140 141 142 |
# File 'lib/common/color.rb', line 140 def printf_yellow(text, bold=false, stdout = STDOUT) printf_color(text, 33, bold, stdout) end |
#which_color(state) ⇒ Object
Determines the color depending on the state.
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/common/color.rb', line 224 def which_color(state) case state when "STARTED", "INSTALLED", "COMPLETED" return "green" when "STOPPED", "INSTALL_FAILED", "ABORTED", "FAILED" return "red" else return "yellow" end end |