module Mimi::Console::Colors

Constants

SEQUENCE

Public Instance Methods

esc_blue(text) click to toggle source

Returns ANSI escaped string for the blue colored text.

# File lib/mimi/console.rb, line 42
def esc_blue(text)
  esc_color 34, text
end
esc_bold(text) click to toggle source

Returns ANSI escaped string for the bold text.

# File lib/mimi/console.rb, line 54
def esc_bold(text)
  esc_string "\e[01;37m", text
end
esc_color(color_code, text) click to toggle source

Returns ANSI escaped string for the colored text.

# File lib/mimi/console.rb, line 18
def esc_color(color_code, text)
  esc_string "\e[#{color_code}m", text
end
esc_format(*args) click to toggle source

Returns string with formatted and padded fields Each arg is an Array:

<text>, [padding_num], [color]
# File lib/mimi/console.rb, line 75
def esc_format(*args)
  out = []
  args.each do |arg|
    out << esc_pad(arg[0], arg[1] || 0, arg[2])
  end
  out.join(' ')
end
esc_format_table(rows, params = {}) click to toggle source

Returns rows, as arrays strings, formatted as a table

@param rows [Array<Array<String>>]

@return [String]

# File lib/mimi/console.rb, line 89
def esc_format_table(rows, params = {})
  return nil if rows.empty?
  params = {
    delimiter: '  '
  }.merge(params)
  columns_count = rows.map(&:size).max
  columns = columns_count.times.map do |c|
    rows.map { |r| unesc_string(r[c].to_s).size }.max # width of the column
  end
  rows.map do |row|
    row.map.with_index do |t, i|
      esc_pad(t, columns[i])
    end.join(params[:delimiter])
  end.join("\n")
end
esc_gray(text) click to toggle source
# File lib/mimi/console.rb, line 48
def esc_gray(text)
  esc_color '30;1', text
end
esc_green(text) click to toggle source

Returns ANSI escaped string for the green colored text.

# File lib/mimi/console.rb, line 30
def esc_green(text)
  esc_color 32, text
end
esc_pad(text, num, color = nil) click to toggle source

Returns string padded to given number of characters, text can be escaped.

# File lib/mimi/console.rb, line 60
def esc_pad(text, num, color = nil)
  text = text.to_s
  esc_text = text
  case color
  when :red, :yellow, :green
    esc_text = send :"esc_#{color}", text
  end
  pad = esc_text.size - unesc_string(text).size
  num > 0 ? ("%-#{num + pad}s" % esc_text) : esc_text
end
esc_red(text) click to toggle source

Returns ANSI escaped string for the red colored text.

# File lib/mimi/console.rb, line 24
def esc_red(text)
  esc_color 31, text
end
esc_string(esc, text) click to toggle source

Returns ANSI escaped string.

# File lib/mimi/console.rb, line 8
def esc_string(esc, text)
  esc + text.to_s + "\e[0m"
end
esc_yellow(text) click to toggle source

Returns ANSI escaped string for the yellow colored text.

# File lib/mimi/console.rb, line 36
def esc_yellow(text)
  esc_color 33, text
end
unesc_string(text) click to toggle source
# File lib/mimi/console.rb, line 12
def unesc_string(text)
  text.gsub(/\e[^m]+m/, '')
end