Class: Dracula::UI
- Inherits:
-
Object
- Object
- Dracula::UI
- Defined in:
- lib/dracula/ui.rb
Class Method Summary collapse
- .bold(str) ⇒ Object
- .danger(str) ⇒ Object
- .error(str) ⇒ Object
-
.print_table(array, options = {}) ⇒ Object
Prints a table.
Class Method Details
.bold(str) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/dracula/ui.rb', line 4 def self.bold(str) if $stdout.tty? # colorize only if output is terminal "\e[34m#{str}\e[0m" else str end end |
.danger(str) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/dracula/ui.rb', line 12 def self.danger(str) if $stdout.tty? # colorize only if output is terminal "\e[31m#{str}\e[0m" else str end end |
.error(str) ⇒ Object
20 21 22 |
# File 'lib/dracula/ui.rb', line 20 def self.error(str) danger("[ERROR] ") + str.to_s end |
.print_table(array, options = {}) ⇒ Object
Prints a table. Shamelesly copied from thor.
Parameters
Array[Array[String, String, …]]
Options
- indent<Integer>
-
Indent the first column by indent value.
- colwidth<Integer>
-
Force the first column to colwidth spaces wide.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dracula/ui.rb', line 33 def self.print_table(array, = {}) # rubocop:disable MethodLength return if array.empty? formats = [] indent = [:indent].to_i colwidth = [:colwidth] [:truncate] = terminal_width if [:truncate] == true formats << "%-#{colwidth + 2}s".dup if colwidth start = colwidth ? 1 : 0 colcount = array.max { |a, b| a.size <=> b.size }.size maximas = [] start.upto(colcount - 1) do |index| maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max maximas << maxima formats << if index == colcount - 1 # Don't output 2 trailing spaces when printing the last column "%-s".dup else "%-#{maxima + 2}s".dup end end formats[0] = formats[0].insert(0, " " * indent) formats << "%s" array.each do |row| sentence = "".dup row.each_with_index do |column, index| maxima = maximas[index] f = if column.is_a?(Numeric) if index == row.size - 1 # Don't output 2 trailing spaces when printing the last column "%#{maxima}s" else "%#{maxima}s " end else formats[index] end sentence << f % column.to_s end sentence = truncate(sentence, [:truncate]) if [:truncate] puts sentence end end |