Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Drawing a table

by hotshot (Prior)
on Jun 29, 2005 at 09:18 UTC ( #470935=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a command line interface Perl application which I'm running on a Linux machine. I want to create a command that display it's output in table format. is there a Perl module that can help me with that. Currently I'm drawing my table as follows:
# print table header # fields space allocation: underline, Path, Name, status, desc, reset +underline printf "\n\t%s%-20s| %-10s%-12s| %-20s%s\n", &colorParse('%u'), 'Path' +, 'Name', '', 'Description', &colorParse('%n'); # header with some c +olor characters printf "\t%-20s| %-10s%-12s| %-20s\n", '', '', '', ''; # empty line u +nder the header while (my($key, $val) = each(%{$shares})) { .... # extract $path, $name, $status, $desc # color constants should be assigned with separate space in printf + (their space is deducted when we print) # fields space allocation: pathColor, path, pathReset, shareName, +statusColor, status, statusReset, desc printf "\t%s%-20s%s| %-10s%s%-12s%s| %-20s\n", $pathColor, &trunca +teStr($path, 20), $pathReset, &truncateStr($name, 10), $statusColor, +$status, $statusReset, &truncateStr($desc, 20) || ''; }
parseColor() parses color control chars in the printed string, trancateStr() trancates a long string according to the given size.
This works fine most of the times, but as you can see it's quite clumzy and hard to maintain. The output looks something like this:
Path | Name | Description ------------------------------------------------------------------ | | /exadmin | testing | lalala / | test | /test1 | test1 (inactive) | hello RED - Directory is not accessible
Is there a better way?


Replies are listed 'Best First'.
Re: Drawing a table
by blazar (Canon) on Jun 29, 2005 at 09:20 UTC
    I've found Text::Table to be exceptionally useful for these purposes...
Re: Drawing a table
by marto (Cardinal) on Jun 29, 2005 at 09:22 UTC

    You may want to take a look at Text::Table.

    Hope this helps,

Re: Drawing a table
by tomhukins (Curate) on Jun 29, 2005 at 09:41 UTC

    I often use Template::Toolkit for this type of problem.

    You may find it overkill to include such a large module with several dependencies if you're only writing a small script that you want to distribute widely. If I write something for personal or proprietary commercial use, I know that any system I use will already have TT installed.

    For outputting a text table, see Template::Plugin::Filter's format method, and maybe the length virtual method if you want to resize the table according to the text displayed, or the truncate filter if you don't.

Re: Drawing a table
by aukjan (Friar) on Jun 29, 2005 at 09:51 UTC
    You could also look into format, which is standard in Perl to create preformatted text.. Chapter 7 in Programming Perl...
Re: Drawing a table
by fmerges (Chaplain) on Jun 29, 2005 at 09:34 UTC


    Besides, take a look at perlform


Re: Drawing a table
by japhy (Canon) on Jun 29, 2005 at 12:08 UTC
    You don't need to create a string-truncating function; printf() and sprintf() can truncate for you. The format "%20.20s" means "a 20 character right-justified string truncated at 20 characters".

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      True, but my function truncates 3 additional chars and put '...' instead so the user will know there's more.
Re: Drawing a table
by Fletch (Bishop) on Jun 29, 2005 at 13:33 UTC

    You could go old school and pipe your output through tbl and nroff (see the respective manual pages for more info).

    We're looking for people in ATL

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://470935]
Approved by tomhukins
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-03-26 22:54 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.