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_formatting(cell, col, row) ⇒ Object
private
Used to apply ANSI formatting to each cell.
-
#initialize(list, options = {}, &format) ⇒ Columns
constructor
Create a column-based layout.
- #inspect ⇒ Object
- #join(cols = nil) ⇒ Object
-
#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
Create a column-based layout.
The format
block MUST return ANSI codes.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ansi/columns.rb', line 27 def initialize(list, ={}, &format) self.list = list self.columns = [:columns] || [:cols] self.padding = [:padding] || 1 self.align = [:align] || :left #self.ansi = options[:ansi] self.format = format #@columns = nil if @columns == 0 end |
Instance Attribute Details
#align ⇒ Object
Alignment to apply to cells.
83 84 85 |
# File 'lib/ansi/columns.rb', line 83 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.
59 60 61 |
# File 'lib/ansi/columns.rb', line 59 def columns @columns end |
#format ⇒ Object
Formating to apply to cells.
96 97 98 |
# File 'lib/ansi/columns.rb', line 96 def format @format end |
#list ⇒ Object
List layout into columns. Each new line is taken to be a row-column cell.
46 47 48 |
# File 'lib/ansi/columns.rb', line 46 def list @list end |
#padding ⇒ Object
Padding size to apply to cells.
70 71 72 |
# File 'lib/ansi/columns.rb', line 70 def padding @padding end |
Instance Method Details
#ansi_formatting(cell, col, row) ⇒ Object (private)
Used to apply ANSI formatting to each cell.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ansi/columns.rb', line 175 def ansi_formatting(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 |
#inspect ⇒ Object
40 41 42 |
# File 'lib/ansi/columns.rb', line 40 def inspect "#<#{self.class}:#{object_id} #{list.inspect} x #{columns}>" end |
#join(cols = nil) ⇒ Object
114 115 116 |
# File 'lib/ansi/columns.rb', line 114 def join(cols=nil) to_s_columns(cols || columns) end |
#template(max, pad) ⇒ Object (private)
Aligns the cell left or right.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ansi/columns.rb', line 162 def template(max, pad) case align when :center, 'center' offset = " " * (max / 2) "#{offset}%#{max}s#{offset}#{pad}" 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.
109 110 111 |
# File 'lib/ansi/columns.rb', line 109 def to_s(cols=nil) to_s_columns(cols || columns) end |
#to_s_columns(columns = nil) ⇒ Object (private)
Put in empty strings for blank cells.
Centering look like it’s off by one to the right.
Layout string lines into columns.
125 126 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 |
# File 'lib/ansi/columns.rb', line 125 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.size)).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_formatting(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 |