Module: ConsoleViewHelper

Defined in:
lib/console_view_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.align(text, size, direction = :left, append = ' ') ⇒ Object

Align text



55
56
57
58
59
60
61
62
63
64
# File 'lib/console_view_helper.rb', line 55

def align(text, size, direction = :left, append = ' ')
  case direction
  when :right
    text.rjust(size, append)
  when :center
    text.center(size, append)
  else
    text.ljust(size, append)
  end
end

.astk(n = 1) ⇒ Object

Asterisk n times



20
21
22
# File 'lib/console_view_helper.rb', line 20

def astk(n = 1)
  '*' * n
end

Banner

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/console_view_helper.rb', line 117

def banner(title, opts = {})
  n = opts[:indent] || 0
  symbol = opts[:symbol] || '*'
  subtitle = opts[:subtitle]
  base_width = (subtitle && subtitle.length > title.length ? subtitle.length : title.length) + 4
  width = opts[:width] || base_width
  raise ArgumentError.new("Specified width can't be minor thant #{base_width}. Increase or remove the width value.") if width < base_width
  banner = idt(n) + (symbol * (width + 2)) + nl
  banner << idt(n) + symbol + whites(width) + symbol + nl
  banner << idt(n) + symbol + align(title, width, :center) + symbol + nl
  banner << idt(n) + symbol + align(subtitle, width, :center) + symbol + nl if subtitle
  banner << idt(n) + symbol + whites(width) + symbol + nl
  banner << idt(n) + (symbol * (width + 2)) + nl
end

.bar(n = 1) ⇒ Object

Bar n times



40
41
42
# File 'lib/console_view_helper.rb', line 40

def bar(n = 1)
  '|' * n
end

.colorize(text, status = :normal) ⇒ Object

Highlight text with color



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/console_view_helper.rb', line 97

def colorize(text, status = :normal)
  case status
  when :success
    text.green
  when :error
    text.red
  when :warning
    text.yellow
  when :neutral
    text.blue
  else
    text.white
  end
end

.explain(doing_txt, done_txt = '', n = 1, &block) ⇒ Object

Explain the action being performed



88
89
90
91
92
93
94
# File 'lib/console_view_helper.rb', line 88

def explain(doing_txt, done_txt = '', n = 1, &block)
  printi doing_txt, n
  loading_effect
  result = block.call
  puts done_txt
  result
end

.hidden_input(label = '>>', n = 0) ⇒ Object

User input, but hidden



200
201
202
203
# File 'lib/console_view_helper.rb', line 200

def hidden_input(label = '>>', n = 0)
  printi label + whites, n
  STDIN.noecho(&:gets).strip.chomp
end

.hyphen(n = 1) ⇒ Object

Hyphen n times



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

def hyphen(n = 1)
  '-' * n
end

.idt(n = 1) ⇒ Object

Indent n times



15
16
17
# File 'lib/console_view_helper.rb', line 15

def idt(n = 1)
  "\t" * n
end

.input(label = '>>', n = 0) ⇒ Object

User input



194
195
196
197
# File 'lib/console_view_helper.rb', line 194

def input(label = '>>', n = 0)
  printi label + whites, n
  gets.strip.chomp
end

.list(items, opts = {}) ⇒ Object

List (unordered)

Raises:

  • (ArgumentError)


173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/console_view_helper.rb', line 173

def list(items, opts = {})
  raise ArgumentError.new('Pass list items in an array.') unless items.is_a? Array
  n = opts[:indent] || 0
  li_gap = opts[:li_gap] || 1
  symbol = opts[:symbol] || ''
  list = idt(n)
  items.each_with_index do |li, i|
    symbol = opts[:ordered] ? "#{i + 1}." : symbol
    list << symbol + ' ' + li + nl(li_gap) + idt(n)
  end
  list
end

.loading_effect(n = 3, opts = {}) ⇒ Object

Display a fake loading effect



77
78
79
80
81
82
83
84
85
# File 'lib/console_view_helper.rb', line 77

def loading_effect(n = 3, opts = {})
  delay = opts[:delay] || 0.3
  symbol = opts[:symbol] || '.'
  1.upto(n) do
    print symbol
    sleep delay
  end
  nil
end

Ordered List



191
192
193
# File 'lib/console_view_helper.rb', line 191

def olist(items, opts = {})
  list items, opts.merge(ordered: true)
end

.nl(n = 1) ⇒ Object

New n lines



25
26
27
# File 'lib/console_view_helper.rb', line 25

def nl(n = 1)
  "\n" * n
end

.olist(items, opts = {}) ⇒ Object

Ordered List



188
189
190
# File 'lib/console_view_helper.rb', line 188

def olist(items, opts = {})
  list items, opts.merge(ordered: true)
end

.printi(text, n = 1) ⇒ Object

‘print’ text indented n times



67
68
69
# File 'lib/console_view_helper.rb', line 67

def printi(text, n = 1)
  print idt(n) + text
end

.putsi(text, n = 1) ⇒ Object

‘put’ text indented n times



72
73
74
# File 'lib/console_view_helper.rb', line 72

def putsi(text, n = 1)
  puts idt(n) + text
end

.table(columns, opts = {}) ⇒ Object

Table

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/console_view_helper.rb', line 133

def table(columns, opts = {})
  raise ArgumentError.new('Pass table columns as an array of arrays') unless columns.is_a?(Array) && columns.select { |item| !item.is_a?(Array) }.empty?
  # Set options
  n = opts[:indent] || 0
  cell_width = opts[:cell_width] || 12
  cell_separator = opts[:cell_separator] || bar
  cell_border = opts[:cell_border] || hyphen
  if opts[:header]
    opts[:header].each_with_index do |th, i|
      columns.push [] unless columns[i]
      columns[i].unshift(th)
    end
  end
  td_width = cell_width - 2
  tr_width = (cell_width * columns.length) + columns.length + 1
  
  # Build table
  pos, table = 0, ''
  begin
    tr_empty_elems = 0
    tr_str = idt(n) + (cell_border * tr_width) + nl + cell_separator
    columns.each do |column|
      td = if column[pos]
        column[pos]
      else
        tr_empty_elems += 1
        ''
      end
      td = align(td[0..td_width], cell_width, :center)
      tr_str << td + cell_separator 
    end
    tr_str << nl
    table << tr_str if tr_empty_elems != columns.length
    pos += 1
  end while tr_empty_elems != columns.length
  table << idt(n) + (cell_border * tr_width)
end

.ulistObject

List (unordered)

Raises:

  • (ArgumentError)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/console_view_helper.rb', line 185

def list(items, opts = {})
  raise ArgumentError.new('Pass list items in an array.') unless items.is_a? Array
  n = opts[:indent] || 0
  li_gap = opts[:li_gap] || 1
  symbol = opts[:symbol] || ''
  list = idt(n)
  items.each_with_index do |li, i|
    symbol = opts[:ordered] ? "#{i + 1}." : symbol
    list << symbol + ' ' + li + nl(li_gap) + idt(n)
  end
  list
end

.underscore(n = 1) ⇒ Object

Underscore n times



35
36
37
# File 'lib/console_view_helper.rb', line 35

def underscore(n = 1)
  '_' * n
end

.whites(n = 1) ⇒ Object

Whitespace n times



45
46
47
# File 'lib/console_view_helper.rb', line 45

def whites(n = 1)
  ' ' * n
end

Instance Method Details

#clsObject

Clear console screen



50
51
52
# File 'lib/console_view_helper.rb', line 50

def cls
  (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) ? system('cls') : system('clear')
end