Module: ConsoleViewHelper
- Defined in:
- lib/console_view_helper.rb
Class Method Summary collapse
-
.align(text, size, direction = :left, append = ' ') ⇒ Object
Align text.
-
.astk(n = 1) ⇒ Object
Asterisk n times.
-
.banner(title, opts = {}) ⇒ Object
Banner.
-
.bar(n = 1) ⇒ Object
Bar n times.
-
.colorize(text, status = :normal) ⇒ Object
Highlight text with color.
-
.explain(doing_txt, done_txt = '', n = 1, &block) ⇒ Object
Explain the action being performed.
-
.hidden_input(label = '>>', n = 0) ⇒ Object
User input, but hidden.
-
.hyphen(n = 1) ⇒ Object
Hyphen n times.
-
.idt(n = 1) ⇒ Object
Indent n times.
-
.input(label = '>>', n = 0) ⇒ Object
User input.
-
.list(items, opts = {}) ⇒ Object
List (unordered).
-
.loading_effect(n = 3, opts = {}) ⇒ Object
Display a fake loading effect.
-
.menu ⇒ Object
Ordered List.
-
.nl(n = 1) ⇒ Object
New n lines.
-
.olist(items, opts = {}) ⇒ Object
Ordered List.
-
.printi(text, n = 1) ⇒ Object
‘print’ text indented n times.
-
.putsi(text, n = 1) ⇒ Object
‘put’ text indented n times.
-
.table(columns, opts = {}) ⇒ Object
Table.
-
.ulist ⇒ Object
List (unordered).
-
.underscore(n = 1) ⇒ Object
Underscore n times.
-
.whites(n = 1) ⇒ Object
Whitespace n times.
Class Method Details
.align(text, size, direction = :left, append = ' ') ⇒ Object
Align text
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/console_view_helper.rb', line 49 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
19 20 21 |
# File 'lib/console_view_helper.rb', line 19 def astk(n = 1) '*' * n end |
.banner(title, opts = {}) ⇒ Object
Banner
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/console_view_helper.rb', line 111 def (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 = idt(n) + (symbol * (width + 2)) + nl << idt(n) + symbol + whites(width) + symbol + nl << idt(n) + symbol + align(title, width, :center) + symbol + nl << idt(n) + symbol + align(subtitle, width, :center) + symbol + nl if subtitle << idt(n) + symbol + whites(width) + symbol + nl << idt(n) + (symbol * (width + 2)) + nl end |
.bar(n = 1) ⇒ Object
Bar n times
39 40 41 |
# File 'lib/console_view_helper.rb', line 39 def (n = 1) '|' * n end |
.colorize(text, status = :normal) ⇒ Object
Highlight text with color
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/console_view_helper.rb', line 91 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
82 83 84 85 86 87 88 |
# File 'lib/console_view_helper.rb', line 82 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
194 195 196 197 |
# File 'lib/console_view_helper.rb', line 194 def hidden_input(label = '>>', n = 0) printi label + whites, n STDIN.noecho(&:gets).strip.chomp end |
.hyphen(n = 1) ⇒ Object
Hyphen n times
29 30 31 |
# File 'lib/console_view_helper.rb', line 29 def hyphen(n = 1) '-' * n end |
.idt(n = 1) ⇒ Object
Indent n times
14 15 16 |
# File 'lib/console_view_helper.rb', line 14 def idt(n = 1) "\t" * n end |
.input(label = '>>', n = 0) ⇒ Object
User input
188 189 190 191 |
# File 'lib/console_view_helper.rb', line 188 def input(label = '>>', n = 0) printi label + whites, n gets.strip.chomp end |
.list(items, opts = {}) ⇒ Object
List (unordered)
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/console_view_helper.rb', line 167 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
71 72 73 74 75 76 77 78 79 |
# File 'lib/console_view_helper.rb', line 71 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 |
.menu ⇒ Object
Ordered List
185 186 187 |
# File 'lib/console_view_helper.rb', line 185 def olist(items, opts = {}) list items, opts.merge(ordered: true) end |
.nl(n = 1) ⇒ Object
New n lines
24 25 26 |
# File 'lib/console_view_helper.rb', line 24 def nl(n = 1) "\n" * n end |
.olist(items, opts = {}) ⇒ Object
Ordered List
182 183 184 |
# File 'lib/console_view_helper.rb', line 182 def olist(items, opts = {}) list items, opts.merge(ordered: true) end |
.printi(text, n = 1) ⇒ Object
‘print’ text indented n times
61 62 63 |
# File 'lib/console_view_helper.rb', line 61 def printi(text, n = 1) print idt(n) + text end |
.putsi(text, n = 1) ⇒ Object
‘put’ text indented n times
66 67 68 |
# File 'lib/console_view_helper.rb', line 66 def putsi(text, n = 1) puts idt(n) + text end |
.table(columns, opts = {}) ⇒ Object
Table
127 128 129 130 131 132 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 |
# File 'lib/console_view_helper.rb', line 127 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] || 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 |
.ulist ⇒ Object
List (unordered)
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/console_view_helper.rb', line 179 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
34 35 36 |
# File 'lib/console_view_helper.rb', line 34 def underscore(n = 1) '_' * n end |
.whites(n = 1) ⇒ Object
Whitespace n times
44 45 46 |
# File 'lib/console_view_helper.rb', line 44 def whites(n = 1) ' ' * n end |