Module: Wpxf::Cli::Output

Included in:
Console
Defined in:
lib/wpxf/cli/output.rb

Overview

Methods for handling output to the screen.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



106
107
108
# File 'lib/wpxf/cli/output.rb', line 106

def indent
  @indent
end

#indent_levelObject

Returns the value of attribute indent_level.



107
108
109
# File 'lib/wpxf/cli/output.rb', line 107

def indent_level
  @indent_level
end

Instance Method Details

#calculate_col_widths(data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wpxf/cli/output.rb', line 77

def calculate_col_widths(data)
  widths = {}
  data.each do |row|
    row.keys.each do |col|
      if widths[col].nil? || row[col].to_s.length > widths[col]
        widths[col] = row[col].to_s.length
      end
    end
  end
  widths
end

#indent_cursor(level = 1) ⇒ Object



13
14
15
16
17
# File 'lib/wpxf/cli/output.rb', line 13

def indent_cursor(level = 1)
  self.indent_level += level
  yield
  self.indent_level -= level
end

#indent_without_wrap(value) ⇒ Object



28
29
30
# File 'lib/wpxf/cli/output.rb', line 28

def indent_without_wrap(value)
  value.gsub(/\n/, "\n#{indent * indent_level}")
end

#initializeObject



7
8
9
10
11
# File 'lib/wpxf/cli/output.rb', line 7

def initialize
  super
  self.indent = '  '
  self.indent_level = 1
end


46
47
48
49
# File 'lib/wpxf/cli/output.rb', line 46

def print_bad(msg)
  print "#{indent * indent_level}[!] ".red
  puts wrap_text(msg, 4, 90)
end


41
42
43
44
# File 'lib/wpxf/cli/output.rb', line 41

def print_good(msg)
  print "#{indent * indent_level}[+] ".green
  puts wrap_text(msg, 4, 90)
end


89
90
91
92
93
94
95
96
# File 'lib/wpxf/cli/output.rb', line 89

def print_header_separator(widths)
  separators = {}
  widths.keys.each do |col|
    separators[col] = '-' * widths[col]
  end

  print_table_row(separators, widths)
end


36
37
38
39
# File 'lib/wpxf/cli/output.rb', line 36

def print_info(msg)
  print "#{indent * indent_level}[-] ".light_blue
  puts wrap_text(msg, 4, 90)
end


32
33
34
# File 'lib/wpxf/cli/output.rb', line 32

def print_std(msg)
  puts "#{indent * indent_level}#{msg}"
end


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wpxf/cli/output.rb', line 56

def print_table(data, pad_with_new_lines = false)
  puts if pad_with_new_lines

  col_widths = calculate_col_widths(data)
  print_table_header data, col_widths

  (1..data.length - 1).each do |i|
    print_table_row(data[i], col_widths)
    puts
  end

  puts if pad_with_new_lines
end


70
71
72
73
74
75
# File 'lib/wpxf/cli/output.rb', line 70

def print_table_header(data, col_widths)
  print_table_row data[0], col_widths
  puts
  print_header_separator col_widths
  puts
end


98
99
100
101
102
103
104
# File 'lib/wpxf/cli/output.rb', line 98

def print_table_row(data, widths)
  print indent * indent_level
  data.keys.each do |col|
    padding = widths[col] - data[col].to_s.length
    print "#{data[col]}#{' ' * padding}   "
  end
end


51
52
53
54
# File 'lib/wpxf/cli/output.rb', line 51

def print_warning(msg)
  print "#{indent * indent_level}[!] ".yellow
  puts wrap_text(msg, 4, 90)
end

#remove_new_lines_and_wrap_text(value, padding = 0, width = 78) ⇒ Object



19
20
21
# File 'lib/wpxf/cli/output.rb', line 19

def remove_new_lines_and_wrap_text(value, padding = 0, width = 78)
  wrap_text(value.tr("\n", ''), padding, width)
end

#wrap_text(value, padding = 0, width = 78) ⇒ Object



23
24
25
26
# File 'lib/wpxf/cli/output.rb', line 23

def wrap_text(value, padding = 0, width = 78)
  value.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n#{indent * indent_level}#{' ' * padding}").chomp
       .gsub(/\s+$/, '')
end