diff --git a/include/blt/std/format.h b/include/blt/std/format.h index d6556ab..69942f3 100755 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -303,9 +303,25 @@ namespace blt::string std::vector createTable(bool top = false, bool bottom = false); }; - class TreeFormatter + class BinaryTreeFormatter { public: + // data classes + struct TreeFormat + { + int verticalSpacing; + int horizontalSpacing; + + int verticalPadding; + int horizontalPadding; + + // should we remove preceding spaces? + bool collapse = false; + + TreeFormat(): verticalSpacing(2), horizontalSpacing(4), verticalPadding(1), horizontalPadding(4) + {} + }; + struct Node { std::string data; @@ -330,22 +346,11 @@ namespace blt::string }; private: - // data classes - struct TreeFormat - { - int verticalSpacing; - int horizontalSpacing; - - int verticalPadding; - int horizontalPadding; - - TreeFormat(): verticalSpacing(2), horizontalSpacing(4), verticalPadding(1), horizontalPadding(4) - {} - } format; + TreeFormat format; Node* root; public: - explicit TreeFormatter(std::string rootData, TreeFormat format = {}): format(format), root(new Node(std::move(rootData))) + explicit BinaryTreeFormatter(std::string rootData, TreeFormat format = {}): format(format), root(new Node(std::move(rootData))) {} std::vector generateBox(Node* node) const; @@ -357,7 +362,7 @@ namespace blt::string std::vector construct(); - ~TreeFormatter() + BinaryTreeFormatter() { delete root; } diff --git a/src/blt/std/format.cpp b/src/blt/std/format.cpp index 5b454f1..13ad443 100755 --- a/src/blt/std/format.cpp +++ b/src/blt/std/format.cpp @@ -146,11 +146,11 @@ void blt::string::TableFormatter::updateMaxColumnLengths() struct node_data { - blt::string::TreeFormatter::Node* node; + blt::string::BinaryTreeFormatter::Node* node; std::vector box; size_t level; - node_data(blt::string::TreeFormatter::Node* node, size_t level): node(node), level(level) + node_data(blt::string::BinaryTreeFormatter::Node* node, size_t level): node(node), level(level) {} }; @@ -162,7 +162,7 @@ struct level_data size_t max_horizontal_length = 0; }; -std::vector blt::string::TreeFormatter::construct() +std::vector blt::string::BinaryTreeFormatter::construct() { std::stack levels; std::queue bfs; @@ -265,6 +265,7 @@ std::vector blt::string::TreeFormatter::construct() std::reverse(lines.begin(), lines.end()); size_t index = 1; + size_t minimal = std::numeric_limits::max(); while (index < lines.size()) { if (auto poses = blt::string::containsAll(lines[index], std::unordered_set{'$', '#', '@'})) @@ -318,13 +319,29 @@ std::vector blt::string::TreeFormatter::construct() blt::string::replaceAll(lines[index], "#", "+"); blt::string::replaceAll(lines[index], "@", "+"); blt::string::replaceAll(lines[index], "$", "+"); + if (format.collapse) + { + for (size_t i = 0; i < lines[index].size(); i++) + { + if (lines[index][i] != ' ') + { + minimal = std::min(minimal, i); + break; + } + } + } index++; } blt::string::replaceAll(lines.front(), "%", "-"); + if (format.collapse) + { + for (auto& line : lines) + line = line.substr(minimal); + } return lines; } -std::vector blt::string::TreeFormatter::generateBox(blt::string::TreeFormatter::Node* node) const +std::vector blt::string::BinaryTreeFormatter::generateBox(blt::string::BinaryTreeFormatter::Node* node) const { if (node == nullptr) return {}; @@ -349,9 +366,9 @@ std::vector blt::string::TreeFormatter::generateBox(blt::string::Tr std::string dataStr; dataStr.reserve(totalLength); dataStr += '|'; - dataStr += createPadding(paddingLeft - 1); + dataStr += createPadding(std::max(paddingLeft, 1) - 1); dataStr += data; - dataStr += createPadding(paddingRight - 1); + dataStr += createPadding(std::max(paddingRight, 1) - 1); dataStr += '|'; lines.push_back(hline); diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index 8ecae8e..2232abc 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -59,12 +59,16 @@ void blt::test::utility::run() printLines(tableTest.createTable(true, true)); - - blt::string::TreeFormatter treeFormatter("I love Men"); + blt::string::BinaryTreeFormatter::TreeFormat format; + format.horizontalPadding = 3; + format.verticalPadding = 0; + format.horizontalSpacing = 3; + format.collapse = true; + blt::string::BinaryTreeFormatter treeFormatter("I love Men", format); treeFormatter.getRoot()->with( - (new string::TreeFormatter::Node("Guys"))->with(nullptr, new string::TreeFormatter::Node("Child2")), - (new string::TreeFormatter::Node("Femboys")) - ->with(nullptr, new string::TreeFormatter::Node("Child1"))); + (new string::BinaryTreeFormatter::Node("Guys"))->with(nullptr, new string::BinaryTreeFormatter::Node("Child2")), + (new string::BinaryTreeFormatter::Node("Femboys")) + ->with(nullptr, new string::BinaryTreeFormatter::Node("Child1"))); printLines(treeFormatter.construct());