Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.format.tableFormat #9955

Open
dlangBugzillaToGithub opened this issue Feb 26, 2013 · 0 comments
Open

std.format.tableFormat #9955

dlangBugzillaToGithub opened this issue Feb 26, 2013 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-02-26T04:31:58Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=9594

CC List

  • justin

Description

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 %)
%)", m);
    writeln();
    writefln("[%([%(%d, %)],
 %)]]", m);
    writeln();
    writefln("[%([%(%3d, %)],
 %)]]", 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."
@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants