Module: Mimi::Console::Colors

Defined in:
lib/mimi/console.rb

Defined Under Namespace

Modules: Disabled

Constant Summary collapse

SEQUENCE =
%w(31 32 33 34 35 36 37 31;1 32;1 33;1 34;1 35;1 36;1 37;1)

Instance Method Summary collapse

Instance Method Details

#esc_blue(text) ⇒ Object

Returns ANSI escaped string for the blue colored text.



42
43
44
# File 'lib/mimi/console.rb', line 42

def esc_blue(text)
  esc_color 34, text
end

#esc_bold(text) ⇒ Object

Returns ANSI escaped string for the bold text.



54
55
56
# File 'lib/mimi/console.rb', line 54

def esc_bold(text)
  esc_string "\e[01;37m", text
end

#esc_color(color_code, text) ⇒ Object

Returns ANSI escaped string for the colored text.



18
19
20
# File 'lib/mimi/console.rb', line 18

def esc_color(color_code, text)
  esc_string "\e[#{color_code}m", text
end

#esc_format(*args) ⇒ Object

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

<text>, [padding_num], [color]


75
76
77
78
79
80
81
# 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 = {}) ⇒ String

Returns rows, as arrays strings, formatted as a table

Parameters:

  • rows (Array<Array<String>>)

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



48
49
50
# File 'lib/mimi/console.rb', line 48

def esc_gray(text)
  esc_color '30;1', text
end

#esc_green(text) ⇒ Object

Returns ANSI escaped string for the green colored text.



30
31
32
# File 'lib/mimi/console.rb', line 30

def esc_green(text)
  esc_color 32, text
end

#esc_pad(text, num, color = nil) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object

Returns ANSI escaped string for the red colored text.



24
25
26
# File 'lib/mimi/console.rb', line 24

def esc_red(text)
  esc_color 31, text
end

#esc_string(esc, text) ⇒ Object

Returns ANSI escaped string.



8
9
10
# File 'lib/mimi/console.rb', line 8

def esc_string(esc, text)
  esc + text.to_s + "\e[0m"
end

#esc_yellow(text) ⇒ Object

Returns ANSI escaped string for the yellow colored text.



36
37
38
# File 'lib/mimi/console.rb', line 36

def esc_yellow(text)
  esc_color 33, text
end

#unesc_string(text) ⇒ Object



12
13
14
# File 'lib/mimi/console.rb', line 12

def unesc_string(text)
  text.gsub(/\e[^m]+m/, '')
end