Sha256: 3a94afd10a2d182de4f020478ccbf5f8b2bad90735c2e248764ee4e441eaef8b

Contents?: true

Size: 1.81 KB

Versions: 19

Compression:

Stored size: 1.81 KB

Contents

= ANSI::Columns

The +Columns+ class makes it easy to create nice looking text columns,
sorted from top to bottom, right to left (as opposed to the other way
around).

  require 'ansi/columns'

  list = %w{a b c d e f g h i j k l}

  columns = ANSI::Columns.new(list)

  columns.to_s(4)

The output will be:

  a d g j
  b e h k
  c f i l

Besides an array of elements, Columns.new can take a string in which
the elements are divided by newlines characters. The default column
size can also be given to the initializer.

  list = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl"

  columns = ANSI::Columns.new(list, :columns=>6)

  columns.to_s

The output will be:

  a c e g i k
  b d f h j l

If the column count is +nil+, then the number of columns will be calculated
as a best fit for the current terminal window.

== Padding

Columns can adjust the padding between cells.

  list = %w{a b c d e f g h i j k l}

  columns = ANSI::Columns.new(list, :padding=>2)

  columns.to_s(4)

The output will be:

  a  d  g  j
  b  e  h  k
  c  f  i  l

== Alignment

Columns can also be aligned either left or right.

  list = %w{xx xx xx yy y yy z zz z}

  columns = ANSI::Columns.new(list, :align=>:right)

  columns.to_s(3)

The output will be:

  xx yy  z
  xx  y zz
  xx yy  z

== Format

Lastly, columns can be augmented with ANSI codes. This is done through
a formatting block. The block can take up to three parameters, the cell
content, the column and row numbers, or the cell and the column and row
numbers.

  list = %w{a b c d e f g h i j k l}

  columns = ANSI::Columns.new(list){ |c,r| r % 2 == 0 ? :red : :blue }

  out = columns.to_s(4)

  out.assert == (
    "\e[31ma \e[0m\e[31md \e[0m\e[31mg \e[0m\e[31mj \e[0m\n" +
    "\e[34mb \e[0m\e[34me \e[0m\e[34mh \e[0m\e[34mk \e[0m\n" +
    "\e[31mc \e[0m\e[31mf \e[0m\e[31mi \e[0m\e[31ml \e[0m\n"
  )

QED.

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
arcabouco-0.2.13 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-1.0.0 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
swipe-rails-0.0.5 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-1.0.0.beta3 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-1.0.0.beta2 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-1.0.0.beta vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-0.9.1.beta.3 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-0.9.1.beta vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-0.9.0 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
challah-0.8.3 vendor/bundle/gems/ansi-1.4.3/demo/07_columns.rdoc
ansi-1.4.3 demo/07_columns.rdoc
challah-0.6.1 vendor/bundle/gems/ansi-1.4.2/qed/07_columns.rdoc
challah-0.6.0 vendor/bundle/gems/ansi-1.4.2/qed/07_columns.rdoc
ansi-1.4.2 qed/07_columns.rdoc
ansi-1.4.1 qed/07_columns.rdoc
ansi-1.4.0 qed/07_columns.rdoc
ansi-1.3.0 qed/07_columns.rdoc
ansi-1.2.5 qed/07_columns.rdoc