From 03c63cdf909af4c9bb907dab9f1b25469fa1cb4a Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 1 Dec 2023 15:03:47 -0500 Subject: [PATCH] formatting broken --- include/blt/std/format.h | 4 ++-- src/blt/std/format.cpp | 27 ++++++++++++++++++++------- tests/src/utility_test.cpp | 14 +++++++++++++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/blt/std/format.h b/include/blt/std/format.h index 69942f3..bc09abf 100755 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -348,9 +348,9 @@ namespace blt::string private: TreeFormat format; - Node* root; + Node* root = nullptr; public: - explicit BinaryTreeFormatter(std::string rootData, TreeFormat format = {}): format(format), root(new Node(std::move(rootData))) + explicit BinaryTreeFormatter(std::string rootData, TreeFormat format = {}): format(std::move(format)), root(new Node(std::move(rootData))) {} std::vector generateBox(Node* node) const; diff --git a/src/blt/std/format.cpp b/src/blt/std/format.cpp index 13ad443..0d1b8b6 100755 --- a/src/blt/std/format.cpp +++ b/src/blt/std/format.cpp @@ -149,8 +149,9 @@ struct node_data blt::string::BinaryTreeFormatter::Node* node; std::vector box; size_t level; + bool hasHomosexuality = true; - node_data(blt::string::BinaryTreeFormatter::Node* node, size_t level): node(node), level(level) + node_data(blt::string::BinaryTreeFormatter::Node* node, size_t level, bool homoness): node(node), level(level), hasHomosexuality(homoness) {} }; @@ -166,7 +167,7 @@ std::vector blt::string::BinaryTreeFormatter::construct() { std::stack levels; std::queue bfs; - bfs.emplace(root, 0); + bfs.emplace(root, 0, false); // construct a stack of the highest node -> the lowest node. level_data currentLevel; currentLevel.depth = 0; @@ -179,6 +180,7 @@ std::vector blt::string::BinaryTreeFormatter::construct() levels.push(currentLevel); currentLevel = {}; } + bool isAHomo = false; currentLevel.count++; if (n.node != nullptr) { @@ -192,6 +194,7 @@ std::vector blt::string::BinaryTreeFormatter::construct() replacement = "#"; if (replacement[0] == '$' && n.node->right != nullptr) replacement = "@"; + isAHomo = n.node->left && !n.node->right; blt::string::replaceAll(box.front(), "%", replacement); n.box = std::move(box); } @@ -202,17 +205,18 @@ std::vector blt::string::BinaryTreeFormatter::construct() if (n.node == nullptr) continue; if (n.node->left != nullptr) - bfs.emplace(n.node->left, n.level + 1); + bfs.emplace(n.node->left, n.level + 1, isAHomo); else - bfs.emplace(nullptr, n.level + 1); + bfs.emplace(nullptr, n.level + 1, isAHomo); if (n.node->right != nullptr) - bfs.emplace(n.node->right, n.level + 1); + bfs.emplace(n.node->right, n.level + 1, isAHomo); else - bfs.emplace(nullptr, n.level + 1); + bfs.emplace(nullptr, n.level + 1, isAHomo); } std::vector lines; size_t lineLength = 0; + size_t lastLineLength = 0; const size_t lineHeight = format.verticalPadding * 2 + 3; //std::cout << levels.size() << "\n"; const size_t verticalSpacing = format.verticalSpacing % 2 == 0 ? format.verticalSpacing + 1 : format.verticalSpacing; @@ -239,11 +243,19 @@ std::vector blt::string::BinaryTreeFormatter::construct() for (size_t i = 0; i < currentLines.size(); i++) { currentLines[i] += createPadding(format.horizontalSpacing); + //currentLines[i] += createPadding(format.horizontalSpacing + static_cast(lineLength / (n.level.size() + 1))); currentLines[i] += box[i]; } } } - std::int64_t padLength = (static_cast(lineLength) - static_cast(currentLines[0].length())) / 2; + // TODO: + //std::int64_t padLength = 0; + //if (n.level.size() == 1) + // padLength = ((static_cast(lineLength) - static_cast(currentLines[0].length())) / 2); + //else + // padLength = ((static_cast(lineLength) - static_cast(lineLength / (n.level.size()))) / 2); + std::int64_t padLength = ((static_cast(lineLength) - static_cast(currentLines[0].length())) / 2); + //std::int64_t padLength = ((static_cast(lineLength) - static_cast(lineLength / (n.level.size()))) / 2); if (padLength < 0) padLength = 0; for (const auto& v : currentLines) @@ -251,6 +263,7 @@ std::vector blt::string::BinaryTreeFormatter::construct() lineLength = std::max(lineLength, v.length()); lines.push_back(createPadding(padLength) + v); maxLineLength = std::max(maxLineLength, lines.back().length()); + lastLineLength = lines.back().length(); } levels.pop(); if (!levels.empty()) diff --git a/tests/src/utility_test.cpp b/tests/src/utility_test.cpp index 2232abc..e5be4cb 100644 --- a/tests/src/utility_test.cpp +++ b/tests/src/utility_test.cpp @@ -71,5 +71,17 @@ void blt::test::utility::run() ->with(nullptr, new string::BinaryTreeFormatter::Node("Child1"))); printLines(treeFormatter.construct()); - + blt::string::BinaryTreeFormatter assign1("price > 50", format); + assign1.getRoot()->with( + // left + (new string::BinaryTreeFormatter::Node("member")) + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.15")) + ->with((new string::BinaryTreeFormatter::Node("total > 500"))->with(new string::BinaryTreeFormatter::Node("total -= 25"))), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))), + // right + (new string::BinaryTreeFormatter::Node("quality")) + ->with((new string::BinaryTreeFormatter::Node("total -= total * 0.02")), + (new string::BinaryTreeFormatter::Node("total -= total * 0.05"))) + ); + printLines(assign1.construct()); }