-
Notifications
You must be signed in to change notification settings - Fork 0
Trying to make table manipulation less painful
License
spudtrooper/easytable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The idea is to have an easy way to have an updateable table to print
out to the terminal. Learn by example:
require 'rubygems'
require 'easytable'
include EasyTable
t = Table.new # Create a table
Use << to add rows, such as:
t << ['One','Two','Three'] # Add the header row
t << [10,20,30] # Add row one
t << [100,200,300] # Add another row
You can print to specified out stream:
t.out = STDOUT # Default
t.print # Print it out to STDOUT
The output is:
> One Two Three
> 10 20 30
> 100 200 300
You can update values like the following (and print them again):
t[1][1].value = 1111
t[2][2].value = 123123
t.print
The output is:
> One Two Three
> 10 1111 30
> 100 200 123123
You can change the look of cells, like the following:
t[1][1].color = Color::BLUE
t[1][1].background = Color::BLUE
t[1][1].bold = true
t[1][1].underline = true
t[1][1].blink = true
A text file won't show these differences.
To have 'print' statements update the table in place (i.e. instead of
just printing another copy), set the 'inplace' attribute:
t.inplace = true
You can set the attributes of complete rows, like (for row 3):
t[2].background = Color::BLUE
You can add mnemonics to the rows and columns, so that you aren't
indexing the values with integers. For example, take the following
table:
t = Table.from_array([[ 'Name', 'Age', 'Favorite Donut'],
[ 'Joe', 19, 'Glazed'],
['Suzy Q', 45, 'Strawberry-filled'],
[ 'Bob', 25, 'I hate donuts']])
Printing this we have
> Name | Age | Favorite Donut
> ------ | --- | -----------------
> Joe | 19 | Glazed
> Suzy Q | 45 | Strawberry-filled
> Bob | 25 | I hate donuts
By using mnemonics, such as:
t.col_mnemonics = [:name,:age,:donut]
t.row_mnemonics = [:head,:joe,:suzy_q,:bob]
We can then refer to the table cells in the following ways now (all
mean the sams):
t[1][2].value = 'Chocolate'
t[:joe][:donut].value = 'Chocolate'
t.joe.donut.value = 'Chocolate'
> Name | Age | Favorite Donut
> ------ | --- | -----------------
> Joe | 19 | Chocolate
> Suzy Q | 45 | Strawberry-filled
> Bob | 25 | I hate donuts
About
Trying to make table manipulation less painful
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published