Class: ANSI::Columns
- Inherits:
-
Object
- Object
- ANSI::Columns
- Defined in:
- lib/ansi/columns.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
Alignment to apply to cells.
-
#columns ⇒ Object
Default number of columns to display.
-
#format ⇒ Object
Formating to apply to cells.
-
#list ⇒ Object
List layout into columns.
-
#padding ⇒ Object
Padding size to apply to cells.
Instance Method Summary collapse
-
#ansi_formating(cell, col, row) ⇒ Object
private
Used to apply ANSI formating to each cell.
-
#initialize(list, options = {}, &format) ⇒ Columns
constructor
Create a column-based layout.
-
#template(max, pad) ⇒ Object
private
Aligns the cell left or right.
-
#to_s(cols = nil) ⇒ Object
Return string in column layout.
-
#to_s_columns(columns = nil) ⇒ Object
private
Layout string lines into columns.
Constructor Details
#initialize(list, options = {}, &format) ⇒ Columns
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ansi/columns.rb', line 17 def initialize(list, ={}, &format) self.list = list @columns = [:columns] @padding = [:padding] || 1 @align = [:align] #@ansi = [options[:ansi]].flatten @format = format @columns = nil if @columns == 0 end |
Instance Attribute Details
#align ⇒ Object
Alignment to apply to cells.
50 51 52 |
# File 'lib/ansi/columns.rb', line 50 def align @align end |
#columns ⇒ Object
Default number of columns to display. If nil then the number of coumns is estimated from the size of the terminal.
44 45 46 |
# File 'lib/ansi/columns.rb', line 44 def columns @columns end |
#format ⇒ Object
Formating to apply to cells.
53 54 55 |
# File 'lib/ansi/columns.rb', line 53 def format @format end |
#list ⇒ Object
List layout into columns. Each new line is taken to be a row-column cell.
31 32 33 |
# File 'lib/ansi/columns.rb', line 31 def list @list end |
#padding ⇒ Object
Padding size to apply to cells.
47 48 49 |
# File 'lib/ansi/columns.rb', line 47 def padding @padding end |
Instance Method Details
#ansi_formating(cell, col, row) ⇒ Object (private)
Used to apply ANSI formating to each cell.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ansi/columns.rb', line 117 def ansi_formating(cell, col, row) if @format case @format.arity when 0 f = @format[] when 1 f = @format[cell] when 2 f = @format[col, row] else f = @format[cell, col, row] end else f = nil end [f].flatten.compact end |
#template(max, pad) ⇒ Object (private)
Aligns the cell left or right.
TODO: Handle centered alignment.
107 108 109 110 111 112 113 114 |
# File 'lib/ansi/columns.rb', line 107 def template(max, pad) case align when :right, 'right' "%#{max}s#{pad}" else "%-#{max}s#{pad}" end end |
#to_s(cols = nil) ⇒ Object
Return string in column layout. The number of columns is determined by the ‘columns` property or overriden by cols
argument. – TODO: Allow #to_s to take options and formating block? ++
60 61 62 |
# File 'lib/ansi/columns.rb', line 60 def to_s(cols=nil) to_s_columns(cols || columns) end |
#to_s_columns(columns = nil) ⇒ Object (private)
Layout string lines into columns.
TODO: put in empty strings for blank cells
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ansi/columns.rb', line 69 def to_s_columns(columns=nil) lines = list.to_a count = lines.size max = lines.map{ |l| l.size }.max if columns.nil? width = Terminal.terminal_width columns = (width / (max + padding)).to_i end rows = [] mod = (count / columns.to_f).to_i mod += 1 if count % columns != 0 lines.each_with_index do |line, index| (rows[index % mod] ||=[]) << line.strip end pad = " " * padding tmp = template(max, pad) str = "" rows.each_with_index do |row, ri| row.each_with_index do |cell, ci| ansi_codes = ansi_formating(cell, ci, ri) if ansi_codes.empty? str << (tmp % cell) else str << (tmp % cell).ansi(*ansi_codes) end end str.rstrip! str << "\n" end str end |