formatting broken

v1
Brett 2023-12-01 15:03:47 -05:00
parent 34451b5b86
commit 03c63cdf90
3 changed files with 35 additions and 10 deletions

View File

@ -348,9 +348,9 @@ namespace blt::string
private: private:
TreeFormat format; TreeFormat format;
Node* root; Node* root = nullptr;
public: 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<std::string> generateBox(Node* node) const; std::vector<std::string> generateBox(Node* node) const;

View File

@ -149,8 +149,9 @@ struct node_data
blt::string::BinaryTreeFormatter::Node* node; blt::string::BinaryTreeFormatter::Node* node;
std::vector<std::string> box; std::vector<std::string> box;
size_t level; 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<std::string> blt::string::BinaryTreeFormatter::construct()
{ {
std::stack<level_data> levels; std::stack<level_data> levels;
std::queue<node_data> bfs; std::queue<node_data> bfs;
bfs.emplace(root, 0); bfs.emplace(root, 0, false);
// construct a stack of the highest node -> the lowest node. // construct a stack of the highest node -> the lowest node.
level_data currentLevel; level_data currentLevel;
currentLevel.depth = 0; currentLevel.depth = 0;
@ -179,6 +180,7 @@ std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
levels.push(currentLevel); levels.push(currentLevel);
currentLevel = {}; currentLevel = {};
} }
bool isAHomo = false;
currentLevel.count++; currentLevel.count++;
if (n.node != nullptr) if (n.node != nullptr)
{ {
@ -192,6 +194,7 @@ std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
replacement = "#"; replacement = "#";
if (replacement[0] == '$' && n.node->right != nullptr) if (replacement[0] == '$' && n.node->right != nullptr)
replacement = "@"; replacement = "@";
isAHomo = n.node->left && !n.node->right;
blt::string::replaceAll(box.front(), "%", replacement); blt::string::replaceAll(box.front(), "%", replacement);
n.box = std::move(box); n.box = std::move(box);
} }
@ -202,17 +205,18 @@ std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
if (n.node == nullptr) if (n.node == nullptr)
continue; continue;
if (n.node->left != nullptr) if (n.node->left != nullptr)
bfs.emplace(n.node->left, n.level + 1); bfs.emplace(n.node->left, n.level + 1, isAHomo);
else else
bfs.emplace(nullptr, n.level + 1); bfs.emplace(nullptr, n.level + 1, isAHomo);
if (n.node->right != nullptr) if (n.node->right != nullptr)
bfs.emplace(n.node->right, n.level + 1); bfs.emplace(n.node->right, n.level + 1, isAHomo);
else else
bfs.emplace(nullptr, n.level + 1); bfs.emplace(nullptr, n.level + 1, isAHomo);
} }
std::vector<std::string> lines; std::vector<std::string> lines;
size_t lineLength = 0; size_t lineLength = 0;
size_t lastLineLength = 0;
const size_t lineHeight = format.verticalPadding * 2 + 3; const size_t lineHeight = format.verticalPadding * 2 + 3;
//std::cout << levels.size() << "\n"; //std::cout << levels.size() << "\n";
const size_t verticalSpacing = format.verticalSpacing % 2 == 0 ? format.verticalSpacing + 1 : format.verticalSpacing; const size_t verticalSpacing = format.verticalSpacing % 2 == 0 ? format.verticalSpacing + 1 : format.verticalSpacing;
@ -239,11 +243,19 @@ std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
for (size_t i = 0; i < currentLines.size(); i++) for (size_t i = 0; i < currentLines.size(); i++)
{ {
currentLines[i] += createPadding(format.horizontalSpacing); currentLines[i] += createPadding(format.horizontalSpacing);
//currentLines[i] += createPadding(format.horizontalSpacing + static_cast<std::int64_t>(lineLength / (n.level.size() + 1)));
currentLines[i] += box[i]; currentLines[i] += box[i];
} }
} }
} }
std::int64_t padLength = (static_cast<std::int64_t>(lineLength) - static_cast<std::int64_t>(currentLines[0].length())) / 2; // TODO:
//std::int64_t padLength = 0;
//if (n.level.size() == 1)
// padLength = ((static_cast<std::int64_t>(lineLength) - static_cast<std::int64_t>(currentLines[0].length())) / 2);
//else
// padLength = ((static_cast<std::int64_t>(lineLength) - static_cast<std::int64_t>(lineLength / (n.level.size()))) / 2);
std::int64_t padLength = ((static_cast<std::int64_t>(lineLength) - static_cast<std::int64_t>(currentLines[0].length())) / 2);
//std::int64_t padLength = ((static_cast<std::int64_t>(lineLength) - static_cast<std::int64_t>(lineLength / (n.level.size()))) / 2);
if (padLength < 0) if (padLength < 0)
padLength = 0; padLength = 0;
for (const auto& v : currentLines) for (const auto& v : currentLines)
@ -251,6 +263,7 @@ std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
lineLength = std::max(lineLength, v.length()); lineLength = std::max(lineLength, v.length());
lines.push_back(createPadding(padLength) + v); lines.push_back(createPadding(padLength) + v);
maxLineLength = std::max(maxLineLength, lines.back().length()); maxLineLength = std::max(maxLineLength, lines.back().length());
lastLineLength = lines.back().length();
} }
levels.pop(); levels.pop();
if (!levels.empty()) if (!levels.empty())

View File

@ -71,5 +71,17 @@ void blt::test::utility::run()
->with(nullptr, new string::BinaryTreeFormatter::Node("Child1"))); ->with(nullptr, new string::BinaryTreeFormatter::Node("Child1")));
printLines(treeFormatter.construct()); 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());
} }