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

refactor: Refactoring dump input option #3566

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
121 changes: 99 additions & 22 deletions src/coreComponents/common/format/table/TableFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,68 @@ void TableTextFormatter::setLinks( std::vector< TableLayout::Column > & columns
}
}

std::string_view ltrim( std::string_view s )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be constexpr.

{
std::size_t const first = s.find_first_not_of( " " );
if( first != std::string::npos )
{
return s.substr( first, ( s.size() - first ) );
}
return {};
}

bool formatLinesWithMaxSize( std::vector< std::string > & cellLines, size_t maxLength )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those two functions can be helpful! You could generalize a bit (erase table/cell namings) and add them to StringUtilities.xpp :)

Also, this function produces incorrect results for { "Les shorts c'est pratique, ça laisse les jambes bien au frais.", 8} (obviously, also for maxLength=6).

{
std::vector< std::string > formattedDescription;
bool returnLineSet = false;
for( auto & line : cellLines )
{
size_t startIdx = 0;
size_t endIdx = 0;
size_t captureLength = 0;
size_t delimIdx = 0;
while( endIdx < line.size())
{
endIdx = line.find( ' ', delimIdx );
if( endIdx == std::string::npos )
{
std::string remaining = line.substr( startIdx );
// Check if the remaining substring exceeds maxLength
if( remaining.size() > maxLength )
{
returnLineSet = true;
formattedDescription.push_back( std::string( ltrim( line.substr( startIdx, captureLength ))));
if( captureLength < remaining.size())
{
formattedDescription.push_back( std::string( ltrim( line.substr( startIdx + captureLength ))) );
}
}
else
{
formattedDescription.push_back( std::string( ltrim( line.substr( startIdx, endIdx ))));
}
}
else
{
size_t wordsLength = endIdx - startIdx;
// Check if the capture exceeds maxLength
if( wordsLength > maxLength )
{
returnLineSet = true;
formattedDescription.push_back( std::string( ltrim( line.substr( startIdx, captureLength ))));
startIdx = delimIdx;
captureLength = 0;
}

delimIdx = endIdx + 1;
captureLength = wordsLength;
}
}
}
cellLines = formattedDescription;
return returnLineSet;
}

void TableTextFormatter::populateHeaderCellsLayout( TableLayout & tableLayout,
CellLayoutRows & cellsHeaderLayout ) const
{
Expand All @@ -263,7 +325,6 @@ void TableTextFormatter::populateHeaderCellsLayout( TableLayout & tableLayout,
cellsHeaderLayout[idxRow + 1].push_back( emptyCell );
}
}
currentCell.m_cellWidth = it->m_header.m_cellWidth;

if( it->hasParent() )
{
Expand Down Expand Up @@ -291,6 +352,14 @@ void TableTextFormatter::populateHeaderCellsLayout( TableLayout & tableLayout,
cellsHeaderLayout[currentLayer].push_back( mergingCell );
}

if( tableLayout.isMaxColumnWidthSet())
{
formatLinesWithMaxSize( currentCell.m_lines, tableLayout.getMaxWidth() );
sublineHeaderCounts[currentLayer] = std::max( sublineHeaderCounts[currentLayer],
currentCell.m_lines.size() );

}

cellsHeaderLayout[currentLayer].push_back( currentCell );
}

Expand Down Expand Up @@ -343,7 +412,12 @@ void TableTextFormatter::populateDataCellsLayout( TableLayout & tableLayout,
cell.value = m_horizontalLine;
}

cellsDataLayout[idxRow][idxColumn] = TableLayout::CellLayout( cell.type, cell.value, alignement );
TableLayout::CellLayout dataToCell( cell.type, cell.value, alignement );
if( tableLayout.isMaxColumnWidthSet())
{
formatLinesWithMaxSize( dataToCell.m_lines, tableLayout.getMaxWidth() );
}
cellsDataLayout[idxRow][idxColumn] = dataToCell;
maxLinesPerRow = std::max( maxLinesPerRow, cellsDataLayout[idxRow][idxColumn].m_lines.size() );
idxColumn++;
}
Expand Down Expand Up @@ -396,31 +470,35 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout,
};

size_t const numColumns = cellsHeaderLayout[0].size();
//each idx per row
std::vector< size_t > accMaxStringColumn( cellsDataLayout.size(), 0 );

// Accumulates column lengths for each line based on "MergeNext" tag.
std::vector< size_t > linesLength( cellsDataLayout.size(), 0 );
for( size_t idxColumn = 0; idxColumn < numColumns; ++idxColumn )
{
size_t maxColumnSize = 1;

// init header column max length
for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx )
{
size_t const cellDataLength = getMaxLineLength( cellsDataLayout[rowIdx][idxColumn].m_lines );
if( idxColumn == 0 ||
(idxColumn > 0 && cellsDataLayout[rowIdx][idxColumn - 1].m_cellType != CellType::MergeNext))

{ // retrieves the maximum length of the current column
// and set each header column to maxColumnSize
for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx )
{
maxColumnSize = std::max( maxColumnSize, cellDataLength );
size_t const cellDataLength = getMaxLineLength( cellsDataLayout[rowIdx][idxColumn].m_lines );
if( idxColumn == 0 ||
(idxColumn > 0 && cellsDataLayout[rowIdx][idxColumn - 1].m_cellType != CellType::MergeNext))
{
maxColumnSize = std::max( maxColumnSize, cellDataLength );
}
}
}

for( size_t rowIdx = 0; rowIdx < cellsHeaderLayout.size(); ++rowIdx )
{
size_t const cellHeaderLength = getMaxLineLength( cellsHeaderLayout[rowIdx][idxColumn].m_lines );
maxColumnSize = std::max( {maxColumnSize, cellHeaderLength} );
cellsHeaderLayout[rowIdx][idxColumn].m_cellWidth = maxColumnSize;
for( size_t rowIdx = 0; rowIdx < cellsHeaderLayout.size(); ++rowIdx )
{
size_t const cellHeaderLength = getMaxLineLength( cellsHeaderLayout[rowIdx][idxColumn].m_lines );
maxColumnSize = std::max( {maxColumnSize, cellHeaderLength} );
cellsHeaderLayout[rowIdx][idxColumn].m_cellWidth = maxColumnSize;
}
}

// update maxColumnSize for data cell
// updates the maximum cell size for the current column
for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx )
{
TableLayout::CellLayout & dataCell = cellsDataLayout[rowIdx][idxColumn];
Expand All @@ -430,14 +508,13 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout,

if( dataCell.m_cellType == CellType::MergeNext )
{
accMaxStringColumn[rowIdx] += cellsHeaderLayout[0][idxColumn].m_cellWidth + tableLayout.getColumnMargin();
linesLength[rowIdx] += cellsHeaderLayout[0][idxColumn].m_cellWidth + tableLayout.getColumnMargin();
}

if( idxColumn > 0 &&
previousDataCell->m_cellType == CellType::MergeNext && dataCell.m_cellType != CellType::MergeNext )
{
// root header cells know the maximum string size in the column
size_t const sumOfMergingCell = accMaxStringColumn[rowIdx] + cellsHeaderLayout[0][idxColumn].m_cellWidth;
size_t const sumOfMergingCell = linesLength[rowIdx] + cellsHeaderLayout[0][idxColumn].m_cellWidth;
if( sumOfMergingCell < dataCell.m_cellWidth )
{
maxColumnSize -= dataCell.m_cellWidth - sumOfMergingCell;
Expand All @@ -454,7 +531,7 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout,
}
}

accMaxStringColumn[rowIdx] = 0;
linesLength[rowIdx] = 0;
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions src/coreComponents/common/format/table/TableLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TableLayout & TableLayout::setTitle( string_view title )

TableLayout & TableLayout::enableLineBreak( bool value )
{
m_wrapLine = value;
m_lineBreakAtBegin = value;
return *this;
}

Expand All @@ -62,8 +62,15 @@ TableLayout & TableLayout::setMargin( MarginValue marginValue )
return *this;
}

TableLayout & TableLayout::setMaxColumnWidth( size_t width )
{
m_hasMaxColumnWidthSet = true;
m_tableColumnMaxWidth = width;
return *this;
}

bool TableLayout::isLineBreakEnabled() const
{ return m_wrapLine; }
{ return m_lineBreakAtBegin; }

size_t TableLayout::getMaxDepth() const
{
Expand Down
29 changes: 27 additions & 2 deletions src/coreComponents/common/format/table/TableLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ class TableLayout
*/
TableLayout & setMargin( MarginValue marginValue );

/**
* @brief Set the maximal width for each column
* @param width The max column width
* @return The tableLayout reference
*/
TableLayout & setMaxColumnWidth( size_t width );

/**
* @brief check if a column max width has been set
* @return Truef a column max width has been set, otherwise false
*/
bool isMaxColumnWidthSet()
{ return m_hasMaxColumnWidthSet; }

/**
* @return check if the line break at the end & beginning is activated
*/
Expand Down Expand Up @@ -485,6 +499,12 @@ class TableLayout
integer const & getMarginTitle() const
{ return m_titleMargin; }

/**
* @return The margin title
*/
size_t const & getMaxWidth() const
{ return m_tableColumnMaxWidth; }

/**
* @brief Get the Nb Rows object
* @return std::vector< integer >&
Expand Down Expand Up @@ -552,9 +572,14 @@ class TableLayout
std::vector< size_t > m_sublineHeaderCounts;
/// Contains the subdivision (line) counts for each line in data.
std::vector< size_t > m_sublineDataCounts;
bool m_wrapLine = true;

// Indicate if we have a line break a the beginning of the table
bool m_lineBreakAtBegin = true;
// Contain the table tible
string m_tableTitle;
// Contain the max width for each column
size_t m_tableColumnMaxWidth = std::numeric_limits< size_t >::max();
// Indicate if a max column width has been set
bool m_hasMaxColumnWidthSet = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could only keep one attribute here:

using NoColumnMaxWidth = std::numeric_limits< size_t >::max();
size_t m_tableColumnMaxWidth = NoColumnMaxWidth;

And in isMaxColumnWidthSet() : return m_tableColumnMaxWidth == NoColumnMaxWidth;


integer m_borderMargin;
integer m_columnMargin;
Expand Down
65 changes: 53 additions & 12 deletions src/coreComponents/common/format/table/unitTests/testTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ TEST( testTable, subColumns )
TEST( testTable, variadicTest )
{
{
TableLayout const layoutTest( "Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient montes, nascetur ridiculus mus. Duis nascetur ridiculus mus",
TableLayout const layoutTest( "Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient monte",
{
"Rank",
TableLayout::Column()
Expand All @@ -382,16 +382,58 @@ TEST( testTable, variadicTest )
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 );
TableTextFormatter log( layoutTest );
EXPECT_EQ( log.toString( tableData ),
"\n--------------------------------------------------------------------------------------------------------------------------------------\n"
"| Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient montes, nascetur ridiculus mus. Duis nascetur ridiculus mus |\n"
"--------------------------------------------------------------------------------------------------------------------------------------\n"
"| Rank | Nodes | Edge | Faces | Elems |\n"
"--------------------------------------------------------------------------------------------------------------------------------------\n"
"| | Locales | Ghost | | Locales | Ghost | Locales | Ghost |\n"
"--------------------------------------------------------------------------------------------------------------------------------------\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"--------------------------------------------------------------------------------------------------------------------------------------\n"
"\n-------------------------------------------------------------------------------------------------\n"
"| Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient monte |\n"
"-------------------------------------------------------------------------------------------------\n"
"| Rank | Nodes | Edge | Faces | Elems |\n"
"-------------------------------------------------------------------------------------------------\n"
"| | Locales | Ghost | | Locales | Ghost | Locales | Ghost |\n"
"-------------------------------------------------------------------------------------------------\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"-------------------------------------------------------------------------------------------------\n"
);
}
}


TEST( testTable, maxWidth )
{
{
TableLayout layoutTest( "Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient monte, egestas ipsum a nisl",
{
"Rank",
TableLayout::Column()
.setName( "Nodes" )
.addSubColumns( {"Count local active\nelemnt n", "Count ghost elemnt n" } ),
"Vivamus variu dolor utsisicdis",
TableLayout::Column()
.setName( "Faces" )
.addSubColumns( {"Locales", "Ghost" } ),
TableLayout::Column()
.setName( "Elems" )
.addSubColumns( {"Locales", "egestas ipsum a nisl"} ),
} );
layoutTest.setMaxColumnWidth( 16 );
TableData tableData;
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 );
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 );
TableTextFormatter log( layoutTest );

EXPECT_EQ( log.toString( tableData ),
"\n---------------------------------------------------------------------------------------------------------------------------------\n"
"| Cras egestas ipsum a nisl. Vivamus variu dolor utsisicdis parturient monte, egestas ipsum a nisl |\n"
"---------------------------------------------------------------------------------------------------------------------------------\n"
"| Rank | Nodes | Vivamus variu | Faces | Elems |\n"
"| | | dolor utsisicdis | | |\n"
"---------------------------------------------------------------------------------------------------------------------------------\n"
"| | Count local | Count ghost | | Locales | Ghost | Locales | egestas ipsum a |\n"
"| | active | elemnt n | | | | | nisl |\n"
"| | elemnt n | | | | | | |\n"
"---------------------------------------------------------------------------------------------------------------------------------\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"| min(local/total) | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n"
"---------------------------------------------------------------------------------------------------------------------------------\n"
);
}
}
Expand Down Expand Up @@ -459,7 +501,6 @@ TEST( testTable, testCellMerging )
tableData.addRow( "Alpha", 1001, 8, "Beta\nwater", "2002\n1.0", CellType::MergeNext );

TableTextFormatter const tableText( tableLayout );
std::cout << tableText.toString( tableData ) << std::endl;
EXPECT_EQ( tableText.toString( tableData ),
"\n-----------------------------------------------------------------------------------------------\n"
"| Cras egestas | CoordX | C | CoordZ | Prev | Next |\n"
Expand Down
25 changes: 22 additions & 3 deletions src/coreComponents/dataRepository/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "Group.hpp"
#include "ConduitRestart.hpp"
#include "common/format/StringUtilities.hpp"
#include "common/format/table/TableData.hpp"
#include "common/format/table/TableFormatter.hpp"
#include "common/format/table/TableLayout.hpp"
#include "codingUtilities/Utilities.hpp"
#include "common/TimingMacros.hpp"
#include "GroupContext.hpp"
Expand Down Expand Up @@ -302,13 +305,29 @@ string Group::dumpInputOptions() const
{
string rval;

bool writeHeader = true;
TableLayout logLayout( "", {TableLayout::Column()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TableLayout logLayout( "", {TableLayout::Column()
TableLayout const logLayout( "", {TableLayout::Column()

.setName( "name" )
.setValuesAlignment( TableLayout::Alignment::left ),
"flag",
TableLayout::Column()
.setName( "Description" )
.setValuesAlignment( TableLayout::Alignment::left ) } );
logLayout.setMaxColumnWidth( 80 );
TableData logData;
for( auto const & wrapper : m_wrappers )
{
rval.append( wrapper.second->dumpInputOptions( writeHeader ) );
writeHeader = false;
if( wrapper.second->getInputFlag() == InputFlags::OPTIONAL ||
wrapper.second->getInputFlag() == InputFlags::REQUIRED )
{
logData.addRow( wrapper.second->getName(),
InputFlagToString( wrapper.second->getInputFlag() ),
wrapper.second->getDescription() );
}
}

TableTextFormatter logFormatter( logLayout );
rval.append( logFormatter.toString( logData ));

return rval;
}

Expand Down
Loading
Loading