module ConsoleViewHelper
Public Class Methods
align(text, size, direction = :left, append = ' ')
click to toggle source
Align text
# 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)
click to toggle source
Asterisk n times
# File lib/console_view_helper.rb, line 20 def astk(n = 1) '*' * n end
bar(n = 1)
click to toggle source
Bar n times
# File lib/console_view_helper.rb, line 40 def bar(n = 1) '|' * n end
colorize(text, status = :normal)
click to toggle source
Highlight text with color
# 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)
click to toggle source
Explain the action being performed
# 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
hyphen(n = 1)
click to toggle source
Hyphen n times
# File lib/console_view_helper.rb, line 30 def hyphen(n = 1) '-' * n end
idt(n = 1)
click to toggle source
Indent n times
# File lib/console_view_helper.rb, line 15 def idt(n = 1) "\t" * n end
input(label = '>>', n = 0)
click to toggle source
User input
# File lib/console_view_helper.rb, line 194 def input(label = '>>', n = 0) printi label + whites, n gets.strip.chomp end
list(items, opts = {})
click to toggle source
List (unordered)
# 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
Also aliased as: ulist
loading_effect(n = 3, opts = {})
click to toggle source
Display a fake loading effect
# 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
nl(n = 1)
click to toggle source
New n lines
# File lib/console_view_helper.rb, line 25 def nl(n = 1) "\n" * n end
olist(items, opts = {})
click to toggle source
Ordered List
# File lib/console_view_helper.rb, line 188 def olist(items, opts = {}) list items, opts.merge(ordered: true) end
Also aliased as: menu
printi(text, n = 1)
click to toggle source
‘print’ text indented n times
# File lib/console_view_helper.rb, line 67 def printi(text, n = 1) print idt(n) + text end
putsi(text, n = 1)
click to toggle source
‘put’ text indented n times
# File lib/console_view_helper.rb, line 72 def putsi(text, n = 1) puts idt(n) + text end
table(columns, opts = {})
click to toggle source
Table
# 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
underscore(n = 1)
click to toggle source
Underscore n times
# File lib/console_view_helper.rb, line 35 def underscore(n = 1) '_' * n end
whites(n = 1)
click to toggle source
Whitespace n times
# File lib/console_view_helper.rb, line 45 def whites(n = 1) ' ' * n end
Public Instance Methods
cls()
click to toggle source
Clear console screen
# File lib/console_view_helper.rb, line 50 def cls (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) ? system('cls') : system('clear') end
Private Instance Methods
align(text, size, direction = :left, append = ' ')
click to toggle source
Align text
# 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)
click to toggle source
Asterisk n times
# File lib/console_view_helper.rb, line 20 def astk(n = 1) '*' * n end
bar(n = 1)
click to toggle source
Bar n times
# File lib/console_view_helper.rb, line 40 def bar(n = 1) '|' * n end
colorize(text, status = :normal)
click to toggle source
Highlight text with color
# 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)
click to toggle source
Explain the action being performed
# 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
hyphen(n = 1)
click to toggle source
Hyphen n times
# File lib/console_view_helper.rb, line 30 def hyphen(n = 1) '-' * n end
idt(n = 1)
click to toggle source
Indent n times
# File lib/console_view_helper.rb, line 15 def idt(n = 1) "\t" * n end
input(label = '>>', n = 0)
click to toggle source
User input
# File lib/console_view_helper.rb, line 194 def input(label = '>>', n = 0) printi label + whites, n gets.strip.chomp end
list(items, opts = {})
click to toggle source
List (unordered)
# 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
Also aliased as: ulist
loading_effect(n = 3, opts = {})
click to toggle source
Display a fake loading effect
# 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
nl(n = 1)
click to toggle source
New n lines
# File lib/console_view_helper.rb, line 25 def nl(n = 1) "\n" * n end
olist(items, opts = {})
click to toggle source
Ordered List
# File lib/console_view_helper.rb, line 188 def olist(items, opts = {}) list items, opts.merge(ordered: true) end
Also aliased as: menu
printi(text, n = 1)
click to toggle source
‘print’ text indented n times
# File lib/console_view_helper.rb, line 67 def printi(text, n = 1) print idt(n) + text end
putsi(text, n = 1)
click to toggle source
‘put’ text indented n times
# File lib/console_view_helper.rb, line 72 def putsi(text, n = 1) puts idt(n) + text end
table(columns, opts = {})
click to toggle source
Table
# 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
underscore(n = 1)
click to toggle source
Underscore n times
# File lib/console_view_helper.rb, line 35 def underscore(n = 1) '_' * n end
whites(n = 1)
click to toggle source
Whitespace n times
# File lib/console_view_helper.rb, line 45 def whites(n = 1) ' ' * n end