test.cpp
tablegenerator/test.cpp · 3.4 KB
From the archive tablegenerator-1.1.1.tar.gz
Download the archive « Back to the archive contents
/*
* Copyright (c) 2008 Kai Braaten
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <list>
#include "tablegenerator.hpp"
struct Person
{
Person( const std::string&, int a, const std::string& );
~Person();
std::string name;
int age;
std::string hobby;
};
Person::Person( const std::string &n, int a, const std::string &h )
: name( n ), age( a ), hobby( h )
{
}
Person::~Person()
{
}
TableRow &operator<<( TableRow &row, const Person &p )
{
row << p.name << p.age << p.hobby;
return row;
}
std::list< Person > personList;
void fillList();
int main()
{
fillList();
SimpleTable stbl;
TableGenerator table( &stbl );
TableRow header;
header << "Name:" << "Age:" << "Hobby:";
table << header;
for( std::list< Person >::iterator i = personList.begin();
i != personList.end(); ++i )
{
const Person &p = *i;
TableRow row;
//row << p.name << p.age << p.hobby;
row << p; // Use the lovely insertion operator defined above
table << row;
}
// A totally plain table
std::cout << table << '\n';
// Standard framed table
FramedTable ftbl( 1, 1 );
table.changeStrategy( &ftbl );
std::cout << '\n' << table << '\n';
// With custom frame graphics
FramedTable ftblcust( 1, 1, "", "", "", "#=+" );
table.changeStrategy( &ftblcust );
std::cout << '\n' << table << '\n';
// Plain table, but stacked horizontally
// Note that the SimpleTable strategy doesn't use headers,
// So our headers from above becomes data instead here.
SimpleTable st2( 1, 2, 2 );
table.changeStrategy( &st2 );
std::cout << '\n' << table << '\n';
// Fancy table with drop shadow
ShadowTable shadow( "Shadow Table",
"\033[1;33m", // text
"\033[44m", // back fill
"\033[46m", // shadow
"\033[40m", // stop colour (black)
1, // side padding
3, // column padding
0 ); // indentation
table.changeStrategy( &shadow );
std::cout << '\n' << table << '\n';
std::cout << "\033[0m"; // reset colour
}
void fillList()
{
personList.push_back( Person( "Billy", 25, "Swimming in the lake." ) );
personList.push_back( Person( "Bob", 65, "Puffing my pipe." ) );
personList.push_back( Person( "Donald Duck", 70, "Quack, quack!" ) );
personList.push_back( Person( "Jane", 24, "Screaming for help." ) );
personList.push_back( Person( "Tarzan", 35, "Saving Jane" ) );
}