www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9594] New: std.format.tableFormat

http://d.puremagic.com/issues/show_bug.cgi?id=9594

           Summary: std.format.tableFormat
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



writeln() format strings allows to format a 2D table in various ways:


import std.stdio: writeln, writefln;
void main() {
    auto m = [[1, 100, 10], [1, 0, 10], [1, 100, 0]];
    writefln("%(%(%3d %)\n%)", m);
    writeln();
    writefln("[%([%(%d, %)],\n %)]]", m);
    writeln();
    writefln("[%([%(%3d, %)],\n %)]]", m);
}


That output:

  1 100  10
  1   0  10
  1 100   0

[[1, 100, 10],
 [1, 0, 10],
 [1, 100, 0]]

[[  1, 100,  10],
 [  1,   0,  10],
 [  1, 100,   0]]


But if you want some vertical alignment you have to know the width in advance,
and all the table items get formatted with that width, leaving columns of
space.


So I suggest to introduce in std.format a simple function named tableFormat()
that given a range of ranges formats it in a nice 2D table:

[[1, 100, 10]
 [1,   0, 10]
 [1, 100,  0]]


In Mathematica it's named TableForm:
http://reference.wolfram.com/mathematica/ref/TableForm.html

It's handy in printing tables of numbers, but it's also useful for 2D arrays of
text:

auto mat = 
[["Given", "a", "txt", "file", "of", "many", "lines,", "where", "fields",
"within", "a", "line"],
 ["are", "delineated", "by", "a", "single", "'dollar'", "character,", "write",
"a", "program"],
 ["that", "aligns", "each", "column", "of", "fields", "by", "ensuring", "that",
"words", "in", "each"],
 ["column", "are", "separated", "by", "at", "least", "one", "space."],
 ["Further,", "allow", "for", "each", "word", "in", "a", "column", "to", "be",
"either", "left"],
 ["justified,", "right", "justified,", "or", "center", "justified", "within",
"its", "column."]];


tableFormat!"-%"(mat, TableJustify.right) (the second argument defaults to
TableJustify.right) returns the string:

"     Given          a        txt   file     of      many     lines,    where 
fields  within      a line"~
       are delineated         by      a single  'dollar' character,    write   
   a program"~
      that     aligns       each column     of    fields         by ensuring   
that   words     in each"~
    column        are  separated     by     at     least        one   space."~
  Further,      allow        for   each   word        in          a   column   
  to      be either left"~
justified,      right justified,     or center justified     within      its
column."


tableFormat!"-%"(mat, TableJustify.left) returns the string:

"Given      a          txt        file   of     many      lines,     where   
fields  within  a      line"~
are        delineated by         a      single 'dollar'  character, write    a 
     program"~
that       aligns     each       column of     fields    by         ensuring
that    words   in     each"~
column     are        separated  by     at     least     one        space.  "~
Further,   allow      for        each   word   in        a          column   to
     be      either left"~
justified, right      justified, or     center justified within     its     
column."


tableFormat!"-%"(mat, TableJustify.center) returns the string:

"  Given        a         txt      file    of     many      lines,    where  
fields  within    a    line"~
   are     delineated     by       a    single 'dollar'  character,  write     
a    program"~
   that      aligns      each    column   of    fields       by     ensuring 
that    words    in   each"~
  column      are     separated    by     at     least      one      space. "~
 Further,    allow       for      each   word     in         a       column   
to      be    either left"~
justified,   right    justified,   or   center justified   within     its   
column."

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 26 2013