Bug 9594 – std.format.tableFormat

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-26T04:31:58Z
Last change time
2024-12-01T16:16:38Z
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=3248
Moved to GitHub: phobos#9955 →

Comments

Comment #0 by bearophile_hugs — 2013-02-26T04:31:58Z
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."
Comment #1 by robert.schadek — 2024-12-01T16:16:38Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9955 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB