Class: Bovem::Console
- Inherits:
-
Object
- Object
- Bovem::Console
- Defined in:
- lib/bovem/console.rb
Overview
This is a text utility wrapper console I/O.
Instance Attribute Summary (collapse)
-
- (Object) indentation
Current indentation width.
-
- (Object) indentation_string
The string used for indentation.
-
- (Object) line_width
The line width.
-
- (Object) screen_width
The current screen width.
Class Method Summary (collapse)
-
+ (String) execute(command)
Executes a command and returns its output.
-
+ (Console) instance
Returns a unique instance for Console.
-
+ (Fixnum) min_banner_length
Returns the minimum length of a banner, not including brackets and leading spaces.
-
+ (String) parse_style(style)
Parse a style and returns terminal codes.
-
+ (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
Instance Method Summary (collapse)
-
- (Object) begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a green banner.
-
- (Object) debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a magenta banner.
-
- (Object) error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a red banner.
-
- (Object) fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1,, print = true)
Writes a message prepending a red banner and then quits the application.
-
- (String) format(message, suffix = "\n", indent = true, wrap = true, plain = false)
Formats a message.
-
- (String) format_right(message, width = true, go_up = true, plain = false)
Formats a message to be written right-aligned.
-
- (String) get_banner(label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"])
Gets a banner for the messages.
-
- (Fixnum) get_screen_width
Gets the current screen width.
-
- (String) indent(message, width = true, newline_separator = "\n")
Indents a message.
-
- (Object) info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a cyan banner.
-
- (Console) initialize
constructor
Initializes a new Console.
-
- (Object) read(prompt = true, default_value = nil, validator = nil, echo = true)
Reads a string from the console.
-
- (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
-
- (Fixnum) reset_indentation
Resets indentation width to
0
. -
- (Fixnum) set_indentation(width, is_absolute = false)
Sets the new indentation width.
-
- (Array) status(status, plain = false, go_up = true, right = true, print = true)
Writes a status to the output.
-
- (Symbol) task(message = nil, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, block_indentation = 2, block_indentation_absolute = false)
Executes a block of code in a indentation region and then prints out and ending status message.
-
- (Object) warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a yellow banner.
-
- (Fixnum) with_indentation(width = 3, is_absolute = false)
Starts a indented region of text.
-
- (String) wrap(message, width = nil)
Wraps a message in fixed line width.
-
- (String) write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
Writes a message.
-
- (String) write_banner_aligned(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
Writes a message, aligning to a call with an empty banner.
Constructor Details
- (Console) initialize
Initializes a new Console.
148 149 150 151 152 |
# File 'lib/bovem/console.rb', line 148 def initialize @line_width = self.get_screen_width @indentation = 0 @indentation_string = " " end |
Instance Attribute Details
- (Object) indentation
Current indentation width.
41 42 43 |
# File 'lib/bovem/console.rb', line 41 def indentation @indentation end |
- (Object) indentation_string
The string used for indentation.
44 45 46 |
# File 'lib/bovem/console.rb', line 44 def indentation_string @indentation_string end |
- (Object) line_width
The line width. Default to 80
.
35 36 37 |
# File 'lib/bovem/console.rb', line 35 def line_width @line_width end |
- (Object) screen_width
The current screen width.
38 39 40 |
# File 'lib/bovem/console.rb', line 38 def screen_width @screen_width end |
Class Method Details
+ (String) execute(command)
Executes a command and returns its output.
143 144 145 |
# File 'lib/bovem/console.rb', line 143 def self.execute(command) %x{#{command}} end |
+ (Console) instance
Returns a unique instance for Console.
58 59 60 |
# File 'lib/bovem/console.rb', line 58 def self.instance @instance ||= ::Bovem::Console.new end |
+ (Fixnum) min_banner_length
Returns the minimum length of a banner, not including brackets and leading spaces.
135 136 137 |
# File 'lib/bovem/console.rb', line 135 def self. 1 end |
+ (String) parse_style(style)
Parse a style and returns terminal codes.
Supported styles and colors are those in TERM_COLORS and TERM_EFFECTS. You can also prefix colors with bg_
(like bg_red
) for background colors.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bovem/console.rb', line 68 def self.parse_style(style) rv = "" style = style.ensure_string.strip.parameterize if style.present? && style !~ /^[,-]$/ then style = style.ensure_string sym = style.to_sym if ::Bovem::TERM_EFFECTS.include?(sym) then rv = "\e[#{Bovem::TERM_EFFECTS[sym]}m" elsif style.index("bg_") == 0 then sym = style[3, style.length].to_sym rv = "\e[#{40 + ::Bovem::TERM_COLORS[sym]}m" if ::Bovem::TERM_COLORS.include?(sym) elsif style != "reset" then rv = "\e[#{30 + ::Bovem::TERM_COLORS[sym]}m" if ::Bovem::TERM_COLORS.include?(sym) end end rv end |
+ (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
You can specify markers by enclosing in {mark=[style]}
and {/mark}
tags. Separate styles with spaces, dashes or commas. Nesting markers is supported.
Example:
Bovem::Console.new.replace_markers("{mark=bright bg_red}{mark=green}Hello world!{/mark}{/mark}")
# => "\e[1m\e[41m\e[32mHello world!\e[1m\e[41m\e[0m"
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bovem/console.rb', line 104 def self.replace_markers(, plain = false) stack = [] mark_regexp = /((\{mark=([a-z\-_\s,]+)\})|(\{\/mark\}))/mi split_regex = /\s*[\s,-]\s*/ = .ensure_string.gsub(mark_regexp) do tag = $1 styles = $3 replacement = "" if tag == "{/mark}" then # If it is a tag, pop from the latest opened. stack.pop styles = stack.last replacement = plain || stack.blank? ? "" : styles.split(split_regex).collect { |s| self.parse_style(s) }.join("") else replacement = plain ? "" : styles.split(split_regex).collect { |s| self.parse_style(s) }.join("") if replacement.length > 0 then stack << "reset" if stack.blank? stack << styles end end replacement end end |
Instance Method Details
- (Object) begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a green banner.
408 409 410 411 412 |
# File 'lib/bovem/console.rb', line 408 def begin(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, print = true) = self.("*", "bright green") = self.indent(, ? 0 : indent) self.write( + " " + , suffix, ? indent : 0, wrap, plain, print) end |
- (Object) debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a magenta banner.
480 481 482 483 484 |
# File 'lib/bovem/console.rb', line 480 def debug(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, print = true) = self.("D", "bright magenta", full_colored) = self.indent(, ? 0 : indent) self.write( + " " + , suffix, ? indent : 0, wrap, plain, print) end |
- (Object) error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a red banner.
444 445 446 447 448 |
# File 'lib/bovem/console.rb', line 444 def error(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, print = true) = self.("E", "bright red", full_colored) = self.indent(, ? 0 : indent) self.write( + " " + , suffix, ? indent : 0, wrap, plain, print) end |
- (Object) fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1,, print = true)
Writes a message prepending a red banner and then quits the application.
463 464 465 466 |
# File 'lib/bovem/console.rb', line 463 def fatal(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, return_code = -1, print = true) self.error(, suffix, indent, wrap, plain, , full_colored, print) Kernel.exit(return_code.to_integer(-1)) end |
- (String) format(message, suffix = "\n", indent = true, wrap = true, plain = false)
Formats a message.
You can style text by using {mark}
and {/mark}
syntax.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/bovem/console.rb', line 252 def format(, suffix = "\n", indent = true, wrap = true, plain = false) rv = rv = self.replace_markers(rv, plain) # Replace markers # Compute the real width available for the screen, if we both indent and wrap if wrap == true then wrap = @line_width if indent == true then wrap -= self.indentation else indent_i = indent.to_integer wrap -= (indent_i > 0 ? self.indentation : 0) + indent_i end end rv = self.wrap(rv, wrap) # Wrap rv = self.indent(rv, indent) # Indent rv += suffix.ensure_string if suffix # Add the suffix rv end |
- (String) format_right(message, width = true, go_up = true, plain = false)
Formats a message to be written right-aligned.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/bovem/console.rb', line 283 def format_right(, width = true, go_up = true, plain = false) = self.replace_markers(, plain) rv = go_up ? "\e[A" : "" @screen_width ||= self.get_screen_width width = (width == true || width.to_integer < 1 ? @screen_width : width.to_integer) # Get padding padding = width - .to_s.gsub(/(\e\[[0-9]*[a-z]?)|(\\n)/i, "").length # Return rv + "\e[0G\e[#{padding}C" + end |
- (String) get_banner(label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"])
Gets a banner for the messages.
371 372 373 374 375 376 |
# File 'lib/bovem/console.rb', line 371 def (label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"]) label = label.rjust(Bovem::Console., " ") brackets = brackets.ensure_array bracket_color = base_color if full_colored "{mark=%s}%s{mark=%s}%s{/mark}%s{/mark}" % [bracket_color.parameterize, brackets[0], base_color.parameterize, label, brackets[1]] end |
- (Fixnum) get_screen_width
Gets the current screen width.
157 158 159 |
# File 'lib/bovem/console.rb', line 157 def get_screen_width ::Bovem::Console.execute("tput cols").to_integer(80) end |
- (String) indent(message, width = true, newline_separator = "\n")
Indents a message.
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/bovem/console.rb', line 216 def indent(, width = true, newline_separator = "\n") if width.to_integer != 0 then width = (width == true ? 0 : width.to_integer) width = width < 0 ? -width : @indentation + width = .split(newline_separator).collect {|line| (@indentation_string * width) + line }.join(newline_separator) end end |
- (Object) info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a cyan banner.
390 391 392 393 394 |
# File 'lib/bovem/console.rb', line 390 def info(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, print = true) = self.("I", "bright cyan", full_colored) = self.indent(, ? 0 : indent) self.write( + " " + , suffix, ? indent : 0, wrap, plain, print) end |
- (Object) read(prompt = true, default_value = nil, validator = nil, echo = true)
Reads a string from the console.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/bovem/console.rb', line 492 def read(prompt = true, default_value = nil, validator = nil, echo = true) # Write the prompt prompt = "Please insert a value" if prompt == true final_prompt = !prompt.nil? ? prompt.gsub(/:?\s*$/, "") + ": " : nil # Adjust validator validator = validator.ensure_array.collect {|v| v.ensure_string} if validator.present? && !validator.is_a?(::Regexp) # Handle echo stty = ::Bovem::Console.execute("which stty").strip disable_echo = !echo && stty.present? && /-echo\b/mix.match(::Bovem::Console.execute(stty)).nil? # Disable echo ::Bovem::Console.execute("#{stty} -echo") if disable_echo begin catch(:reply) do while true do valid = true if final_prompt then Kernel.print self.format(final_prompt, false, false) $stdout.flush end reply = $stdin.gets.chop reply = default_value if reply.empty? # Match against the validator if validator.present? then if validator.is_a?(Array) then valid = false if validator.length > 0 && !validator.include?(reply) elsif validator.is_a?(Regexp) then valid = false if !validator.match(reply) end end if !valid then self.write("Sorry, your reply was not understood. Please try again.", false, false) else throw(:reply, reply) end end end rescue Interrupt default_value ensure ::Bovem::Console.execute("#{stty} echo") if disable_echo end end |
- (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
236 237 238 |
# File 'lib/bovem/console.rb', line 236 def replace_markers(, plain = false) ::Bovem::Console.replace_markers(, plain) end |
- (Fixnum) reset_indentation
Resets indentation width to 0
.
174 175 176 177 |
# File 'lib/bovem/console.rb', line 174 def reset_indentation self.indentation = 0 self.indentation end |
- (Fixnum) set_indentation(width, is_absolute = false)
Sets the new indentation width.
166 167 168 169 |
# File 'lib/bovem/console.rb', line 166 def set_indentation(width, is_absolute = false) self.indentation = [(!is_absolute ? self.indentation : 0) + width, 0].max.to_i self.indentation end |
- (Array) status(status, plain = false, go_up = true, right = true, print = true)
Writes a status to the output. Valid values are :ok
, :pass
, :fail
, :warn
.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/bovem/console.rb', line 338 def status(status, plain = false, go_up = true, right = true, print = true) statuses = { :ok => {:label => " OK ", :color => "bright green"}, :pass => {:label => "PASS", :color => "bright cyan"}, :warn => {:label => "WARN", :color => "bright yellow"}, :fail => {:label => "FAIL", :color => "bright red"} } statuses.default = statuses[:ok] rv = statuses[status] if print then = self.(rv[:label], rv[:color]) if right then Kernel.puts self.format_right( + " ", true, go_up, plain) else Kernel.puts self.format( + " ", "\n", true, true, plain) end end rv end |
- (Symbol) task(message = nil, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, block_indentation = 2, block_indentation_absolute = false)
Executes a block of code in a indentation region and then prints out and ending status message.
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/bovem/console.rb', line 555 def task( = nil, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, block_indentation = 2, block_indentation_absolute = false) status = nil self.begin(, suffix, indent, wrap, plain, , full_colored) if .present? self.with_indentation(block_indentation, block_indentation_absolute) do rv = block_given? ? yield.ensure_array : [:ok] # Execute block status = rv[0] # Return value if status == :fatal then self.status(:fail, plain) exit(rv.length > 1 ? rv[1].to_integer : -1) else self.status(status, plain) if .present? end end status end |
- (Object) warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
Writes a message prepending a yellow banner.
426 427 428 429 430 |
# File 'lib/bovem/console.rb', line 426 def warn(, suffix = "\n", indent = true, wrap = false, plain = false, = false, full_colored = false, print = true) = self.("W", "bright yellow", full_colored) = self.indent(, ? 0 : indent) self.write( + " " + , suffix, ? indent : 0, wrap, plain, print) end |
- (Fixnum) with_indentation(width = 3, is_absolute = false)
Starts a indented region of text.
184 185 186 187 188 189 190 191 |
# File 'lib/bovem/console.rb', line 184 def with_indentation(width = 3, is_absolute = false) old = self.indentation self.set_indentation(width, is_absolute) yield self.set_indentation(old, true) self.indentation end |
- (String) wrap(message, width = nil)
Wraps a message in fixed line width.
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/bovem/console.rb', line 198 def wrap(, width = nil) if width.to_integer <= 0 then else width = (width == true || width.to_integer < 0 ? self.get_screen_width : width.to_integer) .split("\n").collect { |line| line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line }.join("\n") end end |
- (String) write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
Writes a message.
309 310 311 312 313 |
# File 'lib/bovem/console.rb', line 309 def write(, suffix = "\n", indent = true, wrap = false, plain = false, print = true) rv = self.format(, suffix, indent, wrap, plain) Kernel.puts(rv) if print rv end |
- (String) write_banner_aligned(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
Writes a message, aligning to a call with an empty banner.
326 327 328 |
# File 'lib/bovem/console.rb', line 326 def (, suffix = "\n", indent = true, wrap = false, plain = false, print = true) self.write((" " * (::Bovem::Console. + 3)) + .ensure_string, suffix, indent, wrap, plain, print) end |