From 5d0f1788e13c6e950bc1db0501c97bbea3778a08 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 15 Jul 2019 19:25:22 +0200 Subject: [PATCH] ENH: minor improvements to printTable(List, ..) - return Ostream& - make header separation optional --- .../primitives/strings/word/wordIOList.C | 62 ++++++++++++------- .../primitives/strings/word/wordIOList.H | 21 +++++-- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/OpenFOAM/primitives/strings/word/wordIOList.C b/src/OpenFOAM/primitives/strings/word/wordIOList.C index 3d8ff05616..2ba97e7fb5 100644 --- a/src/OpenFOAM/primitives/strings/word/wordIOList.C +++ b/src/OpenFOAM/primitives/strings/word/wordIOList.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2012-2013 OpenFOAM Foundation @@ -40,40 +40,47 @@ namespace Foam } -void Foam::printTable +Foam::Ostream& Foam::printTable ( - const List& wll, - List& columnWidth, - Ostream& os + const UList& tbl, + List& columnWidths, + Ostream& os, + bool headerSeparator ) { - if (wll.empty()) return; - - // Find the maximum word length for each column - columnWidth.setSize(wll[0].size(), string::size_type(0)); - forAll(columnWidth, coli) + if (tbl.empty()) { - forAll(wll, rowi) + return os; + } + + // Find maximum width for each column + columnWidths.resize(tbl.first().size(), std::string::size_type(0)); + + forAll(columnWidths, coli) + { + auto& colWidth = columnWidths[coli]; + + for (const wordList& tblRow : tbl) { - columnWidth[coli] = + colWidth = std::max ( - columnWidth[coli], - string::size_type(wll[rowi][coli].size()) + colWidth, + string::size_type(tblRow[coli].length()) ); } } // Print the rows adding spacing for the columns - forAll(wll, rowi) + for (const wordList& tblRow : tbl) { - forAll(wll[rowi], coli) + forAll(tblRow, coli) { - os << wll[rowi][coli]; + os << tblRow[coli]; for ( - string::size_type space=0; - space < columnWidth[coli] - wll[rowi][coli].size() + 2; + string::size_type space = 0; + space < columnWidths[coli] - tblRow[coli].length() + 2; ++space ) { @@ -82,15 +89,24 @@ void Foam::printTable } os << nl; - if (!rowi) os << nl; + if (headerSeparator) os << nl; + headerSeparator = false; } + + return os; } -void Foam::printTable(const List& wll, Ostream& os) +Foam::Ostream& Foam::printTable +( + const UList& tbl, + Ostream& os, + bool headerSeparator +) { - List columnWidth; - printTable(wll, columnWidth, os); + List columnWidths; + printTable(tbl, columnWidths, os, headerSeparator); + return os; } diff --git a/src/OpenFOAM/primitives/strings/word/wordIOList.H b/src/OpenFOAM/primitives/strings/word/wordIOList.H index 9063d32f97..610255c8f2 100644 --- a/src/OpenFOAM/primitives/strings/word/wordIOList.H +++ b/src/OpenFOAM/primitives/strings/word/wordIOList.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2012-2013 OpenFOAM Foundation @@ -44,9 +44,22 @@ namespace Foam typedef IOList wordIOList; typedef IOList wordListIOList; - // Print word list list as a table - void printTable(const List&, List&, Ostream&); - void printTable(const List&, Ostream&); + //- Print a List of wordList as a table + Ostream& printTable + ( + const UList& tbl, + List& columnWidths, + Ostream& os, + bool headerSeparator = true + ); + + //- Print a List of wordList as a table + Ostream& printTable + ( + const UList& tbl, + Ostream& os, + bool headerSeparator = true + ); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //