3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-11-24 22:51:34 +00:00

log_help.cc: Don't re-flow codeblocks

Move to a (single) `log_content_body()` instead of the dual `log_pass_str()` (nothing used the string-based indent arg), and pass the `ContentListing` directly; printing codeblocks verbatim (with indent).
Refactor (non-codeblock) line splitting slightly so that it can print up to and including 80 characters before the line break.
Add comments.
This commit is contained in:
Krystine Sherwin 2025-11-18 14:12:27 +13:00
parent 657b0bd92b
commit 695bace331
No known key found for this signature in database

View file

@ -78,38 +78,53 @@ ContentListing* ContentListing::open_option(const string &text,
}
#define MAX_LINE_LEN 80
void log_pass_str(const std::string &pass_str, std::string indent_str, bool leading_newline=false) {
if (pass_str.empty())
void log_content_body(const ContentListing &content, int indent=0, bool leading_newline=false) {
// skip empty nodes
if (content.body.empty())
return;
std::istringstream iss(pass_str);
if (leading_newline)
log("\n");
// iterate over lines in content
std::string indent_str(indent*4, ' ');
std::istringstream iss(content.body);
bool partial_line = false;
for (std::string line; std::getline(iss, line);) {
log("%s", indent_str);
auto curr_len = indent_str.length();
std::istringstream lss(line);
for (std::string word; std::getline(lss, word, ' ');) {
while (word[0] == '`' && word.back() == '`')
word = word.substr(1, word.length()-2);
if (partial_line && curr_len + word.length() >= MAX_LINE_LEN-1) {
curr_len = indent_str.length();
log("\n%s", indent_str);
partial_line = false;
}
if (word.length()) {
log("%s ", word);
curr_len += word.length() + 1;
partial_line = true;
if (content.type == "code") {
// code blocks are verbatim
log("%s", line);
} else {
// iterate over words and break at max line length
auto curr_len = indent_str.length();
std::istringstream lss(line);
for (std::string word; std::getline(lss, word, ' ');) {
// remove inline rst formatting
while (word[0] == '`' && word.back() == '`')
word = word.substr(1, word.length()-2);
// if the current line is not empty, break before going over max
if (partial_line && (curr_len + word.length()) >= MAX_LINE_LEN) {
curr_len = indent_str.length();
log("\n%s", indent_str);
partial_line = false;
}
// print non-empty words
if (word.length()) {
if (partial_line)
// add space after prior word
word.insert(0, " ");
log("%s", word);
curr_len += word.length();
partial_line = true;
}
}
}
log("\n");
}
}
void log_pass_str(const std::string &pass_str, int indent=0, bool leading_newline=false) {
std::string indent_str(indent*4, ' ');
log_pass_str(pass_str, indent_str, leading_newline);
}
PrettyHelp *current_help = nullptr;
@ -137,16 +152,16 @@ void PrettyHelp::log_help() const
{
for (auto &content : _root_listing) {
if (content.type.compare("usage") == 0) {
log_pass_str(content.body, 1, true);
log_content_body(content, 1, true);
log("\n");
} else if (content.type.compare("option") == 0) {
log_pass_str(content.body, 1);
for (auto text : content) {
log_pass_str(text.body, 2);
log_content_body(content, 1);
for (auto &child : content) {
log_content_body(child, 2);
log("\n");
}
} else {
log_pass_str(content.body, 0);
log_content_body(content, 0);
log("\n");
}
}