please make all men gay -brett (to santa, xmas forever list)

v1
Brett 2023-12-01 12:02:20 -05:00
parent 98b5f6fa1e
commit 34451b5b86
3 changed files with 52 additions and 26 deletions

View File

@ -303,9 +303,25 @@ namespace blt::string
std::vector<std::string> 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<std::string> generateBox(Node* node) const;
@ -357,7 +362,7 @@ namespace blt::string
std::vector<std::string> construct();
~TreeFormatter()
BinaryTreeFormatter()
{
delete root;
}

View File

@ -146,11 +146,11 @@ void blt::string::TableFormatter::updateMaxColumnLengths()
struct node_data
{
blt::string::TreeFormatter::Node* node;
blt::string::BinaryTreeFormatter::Node* node;
std::vector<std::string> 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<std::string> blt::string::TreeFormatter::construct()
std::vector<std::string> blt::string::BinaryTreeFormatter::construct()
{
std::stack<level_data> levels;
std::queue<node_data> bfs;
@ -265,6 +265,7 @@ std::vector<std::string> blt::string::TreeFormatter::construct()
std::reverse(lines.begin(), lines.end());
size_t index = 1;
size_t minimal = std::numeric_limits<size_t>::max();
while (index < lines.size())
{
if (auto poses = blt::string::containsAll(lines[index], std::unordered_set{'$', '#', '@'}))
@ -318,13 +319,29 @@ std::vector<std::string> 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<std::string> blt::string::TreeFormatter::generateBox(blt::string::TreeFormatter::Node* node) const
std::vector<std::string> blt::string::BinaryTreeFormatter::generateBox(blt::string::BinaryTreeFormatter::Node* node) const
{
if (node == nullptr)
return {};
@ -349,9 +366,9 @@ std::vector<std::string> 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);

View File

@ -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());