Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Totten committed Dec 20, 2012
0 parents commit 5861fae
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
blib
Makefile
7 changes: 7 additions & 0 deletions META.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name" : "Text::Table::List",
"version" : "*",
"description" : "Build simple text-based tables for field listings.",
"depends" : [ ],
"source-url" : "git://github.com/supernovus/perl6-text-table-list.git"
}
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Text::Table::List

## Introduction

A library to build simple text-based tables for use on a command line
interface. We're using Unicode box drawing characters by default, so please
ensure your terminal supports them. If not, never fear, you can use the
Text::Table::List::ASCII varient which uses standard ASCII characters, or
define your own set of characters entirely.

## Usage

Currently we only support a very minimal table-like structure, here is one
example:

```perl
my $t1 = Text::Table::List.new(:length(40)).start;
$t1.label("A Test Table");
$t1.line;
$t1.field("Hello:", "World");
$t1.field("Goodbye:", "Universe");
$t1.line;
$t1.label("And now for some numbers.");
$t1.blank;
$t1.field("Pi:", pi.base(16));
$t1.field("The Answer:", 42);
$t1.field("Nonsense:", "31.34892");
```

The above would output something that looks like:

```
╔══════════════════════════════════════╗
║ A Test Table ║
╟──────────────────────────────────────╢
║ Hello: World ║
║ Goodbye: Universe ║
╟──────────────────────────────────────╢
║ And now for some numbers. ║
║ ║
║ Pi: 3.243F6A ║
║ The Answer: 42 ║
║ Nonsense: 31.34892 ║
╚══════════════════════════════════════╝
```

You can also pass multiple named paramters to the field() method and each
one will be used as a name/value pair.

For more, see the tests and examples, in the 't' and 'example' folders
respectively.

## Future

This module is pretty simple, and supports only a very small subset of
the functionality expected from a table. The most obvious is that it does
not support any form of real columns. It has separator lines, blank lines,
labels and fields (which consist of a name and a value.) That's it.

So, for a future project, I'd like to build another Text::Table::* module
that allows you to build text-based tables with full column support.

It will be it's own project, as I want to keep this one as simple as possible.

## Author

Timothy Totten, supernovus on #perl6, https://github.com/supernovus/

## License

[Artistic License 2.0](http://www.perlfoundation.org/artistic_license_2_0)

33 changes: 33 additions & 0 deletions example/sumcomp.p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env perl6

use v6;

use Text::Table::List;

sub sum1 ($n) { [+] ^$n }
sub sum2 ($n) { return ($n ** 2 - $n) / 2; }

sub timer ($table, $label, &func) {
$table.line;
$table.label("Using $label");
$table.blank;

my $start = now;
$table.field("Result:", func());
my $end = now;
my $duration = $end - $start;
$table.field("Took:", $duration);
}

sub make_test ($num) {
my $table = Text::Table::List.new(:length(40)).start;
$table.label("Testing $num");
timer $table, "sum1", { sum1($num) };
timer $table, "sum2", { sum2($num) };
say ~$table;
}

my @tests = 10, 100, 1000, 10000, 100000, 1000000;

for @tests { make_test($_); }

90 changes: 90 additions & 0 deletions lib/Text/Table/List.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class Text::Table::List;

has @!lines;
has $!text;

has $.length = 80;

### Default drawing characters.
has $.top-left = "";
has $.top-right = "";
has $.top-char = "";
has $.left-char = "";
has $.right-char = "";
has $.sep-left = "";
has $.sep-right = "";
has $.sep-char = "";
has $.bottom-left = "";
has $.bottom-right = "";
has $.bottom-char = "";

method start {
my $line = $.top-left;
my $pad = $.length - $.top-left.chars - $.top-right.chars;
$line ~= $.top-char x $pad;
$line ~= $.top-right;
@!lines.push: $line;
return self;
}

method end {
my $line = $.bottom-left;
my $pad = $.length - $.bottom-left.chars - $.bottom-right.chars;
$line ~= $.bottom-char x $pad;
$line ~= $.bottom-right;
@!lines.push: $line;
return @!lines;
}

method Str {
if $!text.defined { return $!text; }
return $!text = self.end.join("\n");
}

method say {
say self.Str;
}

method print {
print self.Str;
}

method line {
my $line = $.sep-left;
my $pad = $.length - $.sep-left.chars - $.sep-right.chars;
$line ~= $.sep-char x $pad;
$line ~= $.sep-right;
@!lines.push: $line;
}

method blank {
my $line = $.left-char;
my $pad = $.length - $.left-char.chars - $.right-char.chars;
$line ~= ' ' x $pad;
$line ~= $.right-char;
@!lines.push: $line;
}

method label ($label) {
my $line = $.left-char;
my $pad = $.length - $.left-char.chars - $.right-char.chars;
$line ~= sprintf('%-'~$pad~'s', $label);
$line ~= $.right-char;
@!lines.push: $line;
}

multi method field ($name, $value) {
my $line = $.left-char;
my $pad = $.length - $.left-char.chars - $.right-char.chars - $name.chars;
$line ~= $name;
$line ~= sprintf('%'~$pad~'s', $value);
$line ~= $.right-char;
@!lines.push: $line;
}

multi method field (*%fields) {
for %fields.kv -> $name, $value {
self.field($name, $value);
}
}

17 changes: 17 additions & 0 deletions lib/Text/Table/List/ASCII.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use Text::Table::List;

class Text::Table::List::ASCII is Text::Table::List;

### Default drawing characters.
has $.top-left = "#";
has $.top-right = "#";
has $.top-char = "=";
has $.left-char = "| ";
has $.right-char = " |";
has $.sep-left = "+";
has $.sep-right = "+";
has $.sep-char = "-";
has $.bottom-left = "#";
has $.bottom-right = "#";
has $.bottom-char = "=";

59 changes: 59 additions & 0 deletions t/ascii.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env perl6

use v6;

use Text::Table::List::ASCII;
use Test;

plan 2;

my $t1 = Text::Table::List::ASCII.new.start;
$t1.label("A Test Table");
$t1.line;
$t1.field("Hello:", "World");
$t1.field("Goodbye:", "Universe");
$t1.line;
$t1.label("And now for some numbers.");
$t1.blank;
$t1.field("Pi:", pi.base(16));
$t1.field("The Answer:", 42);
$t1.field("Nonsense:", "31.34892");

my $wanted = "#==============================================================================#
| A Test Table |
+------------------------------------------------------------------------------+
| Hello: World |
| Goodbye: Universe |
+------------------------------------------------------------------------------+
| And now for some numbers. |
| |
| Pi: 3.243F6A |
| The Answer: 42 |
| Nonsense: 31.34892 |
#==============================================================================#";

is ~$t1, $wanted, "Table with default length";

my $t2 = Text::Table::List::ASCII.new(:length(40)).start;

$t2.label("Small test");
$t2.line;
my %staff = {
"Susan Smith" => "CEO",
"Kevin Michaels" => "COO",
"Richard Frank" => "Janitor",
"Lisa Dawkins" => "Designer",
};
$t2.field(|%staff);

$wanted = "#======================================#
| Small test |
+--------------------------------------+
| Susan Smith CEO |
| Kevin Michaels COO |
| Richard Frank Janitor |
| Lisa Dawkins Designer |
#======================================#";

is ~$t2, $wanted, "Table with custom length";

58 changes: 58 additions & 0 deletions t/simple.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env perl6

use v6;

use Text::Table::List;
use Test;

plan 2;

my $t1 = Text::Table::List.new.start;
$t1.label("A Test Table");
$t1.line;
$t1.field("Hello:", "World");
$t1.field("Goodbye:", "Universe");
$t1.line;
$t1.label("And now for some numbers.");
$t1.blank;
$t1.field("Pi:", pi.base(16));
$t1.field("The Answer:", 42);
$t1.field("Nonsense:", "31.34892");

my $wanted = "╔══════════════════════════════════════════════════════════════════════════════╗
║ A Test Table ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ Hello: World ║
║ Goodbye: Universe ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ And now for some numbers. ║
║ ║
║ Pi: 3.243F6A ║
║ The Answer: 42 ║
║ Nonsense: 31.34892 ║
╚══════════════════════════════════════════════════════════════════════════════╝";

is ~$t1, $wanted, "Table with default length";

my $t2 = Text::Table::List.new(:length(40)).start;

$t2.label("Small test");
$t2.line;
my %staff = {
"Susan Smith" => "CEO",
"Kevin Michaels" => "COO",
"Richard Frank" => "Janitor",
"Lisa Dawkins" => "Designer",
};
$t2.field(|%staff);

$wanted = "╔══════════════════════════════════════╗
║ Small test ║
╟──────────────────────────────────────╢
║ Susan Smith CEO ║
║ Kevin Michaels COO ║
║ Richard Frank Janitor ║
║ Lisa Dawkins Designer ║
╚══════════════════════════════════════╝";

is ~$t2, $wanted, "Table with custom length";

0 comments on commit 5861fae

Please sign in to comment.