diff --git a/src/coreComponents/common/format/table/TableData.cpp b/src/coreComponents/common/format/table/TableData.cpp index a3bc93a273b..dac3e995be3 100644 --- a/src/coreComponents/common/format/table/TableData.cpp +++ b/src/coreComponents/common/format/table/TableData.cpp @@ -23,18 +23,19 @@ namespace geos { -void TableData::addRow( std::vector< TableData::CellData > & cells ) +void TableData::addRow( std::vector< TableData::CellData > const & rows ) { - if( m_rows.size() != 0 && cells.size() != m_rows[m_rows.size() - 1].size() ) + if( m_rows.size() != 0 && rows.size() != m_rows[m_rows.size() - 1].size() ) { string msg = "Remarks : some cells may be missing"; + std::cout << '\n'; if( std::find( m_errorsMsg.begin(), m_errorsMsg.end(), msg ) == m_errorsMsg.end()) { m_errorsMsg.push_back( msg ); } } - m_rows.push_back( cells ); + m_rows.push_back( rows ); } void TableData::addSeparator() diff --git a/src/coreComponents/common/format/table/TableData.hpp b/src/coreComponents/common/format/table/TableData.hpp index 53ae1e5fb37..25a36bb5a09 100644 --- a/src/coreComponents/common/format/table/TableData.hpp +++ b/src/coreComponents/common/format/table/TableData.hpp @@ -38,7 +38,7 @@ class TableData struct CellData { CellType type; - string value; + string value = ""; }; /** @@ -53,7 +53,7 @@ class TableData * @brief Add a row to the table * @param row A vector of string representing a row */ - void addRow( std::vector< CellData > & row ); + void addRow( std::vector< CellData > const & row ); /** * @brief Add a line separator to the table @@ -175,11 +175,11 @@ void TableData::addRow( Args const &... args ) if constexpr (std::is_same_v< Args, CellType >) { if( args == CellType::Separator ) { - cells.push_back( {CellType::Separator, "-"} ); + cells.push_back( {CellType::Separator} ); } else { - cells.push_back( {CellType::MergeNext, " "} ); + cells.push_back( {CellType::MergeNext} ); } } else diff --git a/src/coreComponents/common/format/table/TableFormatter.cpp b/src/coreComponents/common/format/table/TableFormatter.cpp index 895244acdd8..4166aca44b8 100644 --- a/src/coreComponents/common/format/table/TableFormatter.cpp +++ b/src/coreComponents/common/format/table/TableFormatter.cpp @@ -102,7 +102,7 @@ string TableCSVFormatter::dataToString( TableData const & tableData ) const result.append( stringutilities::join( rowConverted.cbegin(), rowConverted.cend(), "," )); result.append( "\n" ); } - + return result; } @@ -256,21 +256,19 @@ void TableTextFormatter::populateHeaderCellsLayout( TableLayout & tableLayout, cellsHeaderLayout[idxRow + 1].push_back( emptyCell ); } } - currentCell.m_maxDataLength = it->getMaxStringSize(); + if( it->hasParent() ) { it->m_parent->m_cellWidth += it->m_cellWidth == 0 ? 1 : it->m_cellWidth; } - - sublineHeaderCounts[currentLayer] = std::max( sublineHeaderCounts[currentLayer], - currentCell.m_lines.size() ); - if( it->m_cellWidth > 1 ) { it->m_cellWidth--; } + sublineHeaderCounts[currentLayer] = std::max( sublineHeaderCounts[currentLayer], + currentCell.m_lines.size() ); for( size_t idxColumn = 0; idxColumn < it->m_cellWidth; idxColumn++ ) { TableLayout::CellLayout mergingCell{ CellType::MergeNext, "", TableLayout::Alignment::center }; @@ -324,10 +322,13 @@ void TableTextFormatter::populateDataCellsLayout( TableLayout & tableLayout, { cell.type = it->m_columName.m_cellType; } + if( it->m_columName.m_cellType == CellType::Separator ) + { + cell.value = m_horizontalLine; + } cellsDataLayout[idxRow][idxColumn] = TableLayout::CellLayout( cell.type, cell.value, alignement ); maxLinesPerRow = std::max( maxLinesPerRow, cellsDataLayout[idxRow][idxColumn].m_lines.size() ); idxColumn++; - } } subDataLineCounts.push_back( { maxLinesPerRow } ); @@ -378,26 +379,70 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout, }; size_t const numColumns = cellsHeaderLayout[0].size(); + //each idx per row + std::vector< size_t > accMaxStringColumn( 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 ); 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_maxDataLength = maxColumnSize; } + // update maxColumnSize for data cell for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx ) { TableLayout::CellLayout & dataCell = cellsDataLayout[rowIdx][idxColumn]; TableLayout::CellLayout * previousDataCell = (idxColumn > 0) ? &cellsDataLayout[rowIdx][idxColumn - 1] : nullptr; + if( dataCell.m_cellType == CellType::MergeNext ) + { + accMaxStringColumn[rowIdx] += cellsHeaderLayout[0][idxColumn].m_maxDataLength + tableLayout.getColumnMargin(); + } + + if( idxColumn > 0 && + previousDataCell->m_cellType == CellType::MergeNext && dataCell.m_cellType != CellType::MergeNext ) + { + size_t sumOfMergingCell = accMaxStringColumn[rowIdx] + cellsHeaderLayout[0][idxColumn].m_maxDataLength; + if( sumOfMergingCell < dataCell.m_maxDataLength ) + { + maxColumnSize += dataCell.m_maxDataLength - sumOfMergingCell; + for( size_t rowIdx2 = 0; rowIdx2 < rowIdx; rowIdx2++ ) + { + TableLayout::CellLayout * previousDataCellTemp = (idxColumn > 0) ? &cellsDataLayout[rowIdx2][idxColumn - 1] : nullptr; + if( previousDataCellTemp ) + { + previousDataCellTemp->m_maxDataLength = cellsHeaderLayout[0][idxColumn - 1].m_maxDataLength; + } + updateCellMaxLength( cellsDataLayout[rowIdx2][idxColumn], maxColumnSize, previousDataCellTemp ); + if( cellsDataLayout[rowIdx2][idxColumn].m_cellType == CellType::Separator ) + { + size_t const additionnalMargin = (idxColumn == numColumns - 1) ? + tableLayout.getBorderMargin() * 2 + 2 : + tableLayout.getColumnMargin(); + cellsDataLayout[rowIdx2][idxColumn].m_lines[0] = std::string( maxColumnSize + additionnalMargin, '-' ); + } + } + } + + accMaxStringColumn[rowIdx] = 0; + } + else + { + size_t const cellDataLength = getMaxLineLength( cellsDataLayout[rowIdx][idxColumn].m_lines ); + maxColumnSize = std::max( {maxColumnSize, cellDataLength} ); + } + updateCellMaxLength( dataCell, maxColumnSize, previousDataCell ); if( dataCell.m_cellType == CellType::Separator ) @@ -411,6 +456,7 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout, } } + // last pass for updating cells header for( size_t rowIdx = 0; rowIdx < cellsHeaderLayout.size(); ++rowIdx ) { TableLayout::CellLayout & headerCell = cellsHeaderLayout[rowIdx][idxColumn]; diff --git a/src/coreComponents/common/format/table/TableLayout.hpp b/src/coreComponents/common/format/table/TableLayout.hpp index 3da14a85429..0a9a7b1907c 100644 --- a/src/coreComponents/common/format/table/TableLayout.hpp +++ b/src/coreComponents/common/format/table/TableLayout.hpp @@ -67,12 +67,11 @@ class TableLayout /** * @struct CellLayout - * @brief Structure representing a cell in a table. - * This structure contains information about the cell such as its type, alignment, maximum data length, and width. + * @brief Structure grouping the cell information to display it in a table (content, type, alignment, ...). */ struct CellLayout { - /// vector containing the cell name separated by a '\n'. + /// vector containing each cell content, separated by lines. std::vector< string > m_lines; /// The type of the cell (Header,Value, Merge, ...). CellType m_cellType; diff --git a/src/coreComponents/common/format/table/unitTests/testTable.cpp b/src/coreComponents/common/format/table/unitTests/testTable.cpp index e8bd7934c4a..3b7c4660246 100644 --- a/src/coreComponents/common/format/table/unitTests/testTable.cpp +++ b/src/coreComponents/common/format/table/unitTests/testTable.cpp @@ -87,39 +87,35 @@ TEST( testTable, tableClassic ) ); } -TEST( testTable, tableColumnParamClassic ) //TODO +TEST( testTable, tableColumnParamClassic ) { - TableLayout tableLayout( { + TableLayout const tableLayout( { TableLayout::Column() - .setName( "Cras egestas" ) - .setHeaderAlignment( TableLayout::Alignment::left ), + .setName( "Cras egestas" ), TableLayout::Column() - .setName( "CoordX" ) - .setHeaderAlignment( TableLayout::Alignment::left ), + .setName( "CoordX" ), TableLayout::Column() - .setName( "C" ) - .setHeaderAlignment( TableLayout::Alignment::left ), + .setName( "C" ), TableLayout::Column() - .setName( "CoordZ" ) - .setHeaderAlignment( TableLayout::Alignment::left ), + .setName( "CoordZ" ), TableLayout::Column() - .setName( "Prev\nelement" ) - .setHeaderAlignment( TableLayout::Alignment::right ), + .setName( "Prev\nelement" ), TableLayout::Column() - .setName( "Next\nelement" ) - .setHeaderAlignment( TableLayout::Alignment::right )} ); + .setName( "Next\nelement" )} ); TableData tableData; - tableData.addRow( "value1", "", "3.0", 3.0129877, 2.0f, 1 ); + tableData.addRow( "value1", "gaz\nwater", "3.0\n42.0", "3.0129877\n0.0123456", "2\n3", "1\n4" ); tableData.addRow( "val1", "v", "[3.045,42.02,89.25]", 3.0, 10.0f, 3 ); 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" "| | | | | element | element |\n" "-------------------------------------------------------------------------------------------\n" - "| value1 | | 3.0 | 3.0129877 | 2 | 1 |\n" + "| value1 | gaz | 3.0 | 3.0129877 | 2 | 1 |\n" + "| | water | 42.0 | 0.0123456 | 3 | 4 |\n" "| val1 | v | [3.045,42.02,89.25] | 3 | 10 | 3 |\n" "-------------------------------------------------------------------------------------------\n\n" ); @@ -357,7 +353,7 @@ TEST( testTable, variadicTest ) std::cout << std::endl; 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 ) ; + tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 ); TableTextFormatter log( layoutTest ); EXPECT_EQ( log.toString( tableData ), "\n--------------------------------------------------------------------------------------------------------------------------------------\n" @@ -415,8 +411,13 @@ TEST( testTable, testCellMerging ) TableData tableData; tableData.addRow( "ProductA", 1234, 40, "ProductName", 5678, 60 ); + tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 ); tableData.addSeparator(); - tableData.addRow( "P1\nP2\nP3", "2002\n2003\n2004", CellType::MergeNext, 3003, 4004, CellType::MergeNext ); + tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 ); + tableData.addRow( 3.14f, 2.718f, CellType::MergeNext, 1.618f, 0.577f, CellType::MergeNext ); + tableData.addRow( "P1\nP2\nP3", "2002\n2003\n2004", CellType::MergeNext, "1212121245452145454545", 4004, CellType::MergeNext ); + tableData.addRow( "Long product size", 54, 4564575, "long size value", 5454554512, 60 ); + tableData.addRow( "ProductAfdggfd", 5445, 4565, "PrName", 5454512, 64650 ); tableData.addRow( 3.14f, 2.718f, CellType::MergeNext, 1.618f, 0.577f, CellType::MergeNext ); tableData.addSeparator(); tableData.addRow( CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, "Item2" ); @@ -424,10 +425,15 @@ TEST( testTable, testCellMerging ) tableData.addRow( 1500, 2500, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext ); tableData.addSeparator(); tableData.addRow( 1.23f, 4.56f, CellType::MergeNext, 7.89f, 0.12f, 40 ); + tableData.addRow( "Long product size", 54, 4564575, "long size value", 5454554512, 60 ); + tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 ); + tableData.addSeparator(); + tableData.addRow( "P1", "2002", CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, "1212121245452145454545" ); tableData.addSeparator(); tableData.addRow( "Alpha", 1001, 8, "Beta\nwater", "2002\n1.0", CellType::MergeNext ); TableTextFormatter const tableText( tableLayout ); + std::cout <getMassUnit() ); + std::vector< string > phaseCompName; + phaseCompName.reserve( numPhases*numComps ); + std::vector< string > massValues; + phaseCompName.reserve( numPhases*numComps ); + + ConstitutiveManager const & constitutiveManager = this->getGroupByPath< ConstitutiveManager >( "/Problem/domain/Constitutive" ); + MultiFluidBase const & baseFluid = constitutiveManager.getGroup< MultiFluidBase >( m_solver->referenceFluidModelName() ); + auto const phaseNames = baseFluid.phaseNames(); + auto const componentNames = baseFluid.componentNames(); + for( integer ip = 0; ip < numPhases; ++ip ) + { + for( integer ic = 0; ic < numComps; ++ic ) + { + std::stringstream ss; + ss << phaseNames[ip]<< ", " <