mirror of
https://github.com/YosysHQ/yosys
synced 2026-05-30 21:57:47 +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:
parent
657b0bd92b
commit
695bace331
1 changed files with 41 additions and 26 deletions
|
|
@ -78,38 +78,53 @@ ContentListing* ContentListing::open_option(const string &text,
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_LINE_LEN 80
|
#define MAX_LINE_LEN 80
|
||||||
void log_pass_str(const std::string &pass_str, std::string indent_str, bool leading_newline=false) {
|
void log_content_body(const ContentListing &content, int indent=0, bool leading_newline=false) {
|
||||||
if (pass_str.empty())
|
// skip empty nodes
|
||||||
|
if (content.body.empty())
|
||||||
return;
|
return;
|
||||||
std::istringstream iss(pass_str);
|
|
||||||
if (leading_newline)
|
if (leading_newline)
|
||||||
log("\n");
|
log("\n");
|
||||||
|
|
||||||
|
// iterate over lines in content
|
||||||
|
std::string indent_str(indent*4, ' ');
|
||||||
|
std::istringstream iss(content.body);
|
||||||
bool partial_line = false;
|
bool partial_line = false;
|
||||||
for (std::string line; std::getline(iss, line);) {
|
for (std::string line; std::getline(iss, line);) {
|
||||||
log("%s", indent_str);
|
log("%s", indent_str);
|
||||||
auto curr_len = indent_str.length();
|
if (content.type == "code") {
|
||||||
std::istringstream lss(line);
|
// code blocks are verbatim
|
||||||
for (std::string word; std::getline(lss, word, ' ');) {
|
log("%s", line);
|
||||||
while (word[0] == '`' && word.back() == '`')
|
} else {
|
||||||
word = word.substr(1, word.length()-2);
|
// iterate over words and break at max line length
|
||||||
if (partial_line && curr_len + word.length() >= MAX_LINE_LEN-1) {
|
auto curr_len = indent_str.length();
|
||||||
curr_len = indent_str.length();
|
std::istringstream lss(line);
|
||||||
log("\n%s", indent_str);
|
for (std::string word; std::getline(lss, word, ' ');) {
|
||||||
partial_line = false;
|
// remove inline rst formatting
|
||||||
}
|
while (word[0] == '`' && word.back() == '`')
|
||||||
if (word.length()) {
|
word = word.substr(1, word.length()-2);
|
||||||
log("%s ", word);
|
|
||||||
curr_len += word.length() + 1;
|
// if the current line is not empty, break before going over max
|
||||||
partial_line = true;
|
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");
|
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;
|
PrettyHelp *current_help = nullptr;
|
||||||
|
|
||||||
|
|
@ -137,16 +152,16 @@ void PrettyHelp::log_help() const
|
||||||
{
|
{
|
||||||
for (auto &content : _root_listing) {
|
for (auto &content : _root_listing) {
|
||||||
if (content.type.compare("usage") == 0) {
|
if (content.type.compare("usage") == 0) {
|
||||||
log_pass_str(content.body, 1, true);
|
log_content_body(content, 1, true);
|
||||||
log("\n");
|
log("\n");
|
||||||
} else if (content.type.compare("option") == 0) {
|
} else if (content.type.compare("option") == 0) {
|
||||||
log_pass_str(content.body, 1);
|
log_content_body(content, 1);
|
||||||
for (auto text : content) {
|
for (auto &child : content) {
|
||||||
log_pass_str(text.body, 2);
|
log_content_body(child, 2);
|
||||||
log("\n");
|
log("\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log_pass_str(content.body, 0);
|
log_content_body(content, 0);
|
||||||
log("\n");
|
log("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue