mirror of
https://github.com/YosysHQ/yosys
synced 2025-05-06 07:15:48 +00:00
Content is now added to the `ContentListing` rather than the `PrettyHelp`. `open_*` methods return the `ContentListing` that was added instead of leaving a hanging continuation. This allows for (e.g.) options to be added directly to optiongroups, instead of requiring that groups be closed before continuation. This also means that all `PrettyHelp`s are a listing, with the actual log being called by the default `Pass::help()`; making the mode field redundant. Added `PrettyHelp::log_help()` which replaces the `PrettyHelp::Mode::LOG` logic. Added `ContentListing::back()` which just returns the last element of the underlying content vector. Some of the content tracking was made redundant and removed, in particular `PrettyHelp::_current_listing` and `ContentListing::parent`. Converted `ContentListing` to a class instead of a struct, adjusting constructors to match. Added `ContentListing` constructor that accepts a `source_location`. Update `HelpPass::dump_cmds_json()` for new log_help.
152 lines
4.2 KiB
C++
152 lines
4.2 KiB
C++
/*
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
*
|
|
* Copyright (C) 2025 Krystine Dawn <krystinedawn@yosyshq.com>
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*
|
|
*/
|
|
|
|
#include "kernel/log_help.h"
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
Json ContentListing::to_json() {
|
|
Json::object object;
|
|
object["type"] = type;
|
|
if (body.length()) object["body"] = body;
|
|
if (strcmp(source_file, "unknown") != 0) object["source_file"] = source_file;
|
|
if (source_line != 0) object["source_line"] = source_line;
|
|
Json::array content_array;
|
|
for (auto child : _content) content_array.push_back(child->to_json());
|
|
object["content"] = content_array;
|
|
return object;
|
|
}
|
|
|
|
void ContentListing::usage(const string &usage,
|
|
const source_location location)
|
|
{
|
|
log_assert(type.compare("root") == 0);
|
|
add_content("usage", usage, location);
|
|
}
|
|
|
|
void ContentListing::option(const string &text, const string &description,
|
|
const source_location location)
|
|
{
|
|
auto option = open_option(text);
|
|
if (description.length())
|
|
option->add_content("text", description, location);
|
|
}
|
|
|
|
void ContentListing::codeblock(const string &code, const string &language,
|
|
const source_location location)
|
|
{
|
|
add_content("code", code, location);
|
|
}
|
|
|
|
void ContentListing::paragraph(const string &text,
|
|
const source_location location)
|
|
{
|
|
add_content("text", text, location);
|
|
}
|
|
|
|
ContentListing* ContentListing::open_optiongroup(const string &name,
|
|
const source_location location)
|
|
{
|
|
log_assert(type.compare("root") == 0);
|
|
auto optiongroup = new ContentListing("optiongroup", name, location);
|
|
add_content(optiongroup);
|
|
return optiongroup;
|
|
}
|
|
|
|
ContentListing* ContentListing::open_option(const string &text,
|
|
const source_location location)
|
|
{
|
|
log_assert(type.compare("optiongroup") == 0);
|
|
auto option = new ContentListing("option", text, location);
|
|
add_content(option);
|
|
return option;
|
|
}
|
|
|
|
#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())
|
|
return;
|
|
std::istringstream iss(pass_str);
|
|
if (leading_newline)
|
|
log("\n");
|
|
for (std::string line; std::getline(iss, line);) {
|
|
log("%s", indent_str.c_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 (curr_len + word.length() >= MAX_LINE_LEN-1) {
|
|
curr_len = 0;
|
|
log("\n%s", indent_str.c_str());
|
|
}
|
|
if (word.length()) {
|
|
log("%s ", word.c_str());
|
|
curr_len += word.length() + 1;
|
|
}
|
|
}
|
|
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::PrettyHelp()
|
|
{
|
|
_prior = current_help;
|
|
_root_listing = ContentListing("root", "");
|
|
|
|
current_help = this;
|
|
}
|
|
|
|
PrettyHelp::~PrettyHelp()
|
|
{
|
|
current_help = _prior;
|
|
}
|
|
|
|
PrettyHelp *PrettyHelp::get_current()
|
|
{
|
|
if (current_help == nullptr)
|
|
new PrettyHelp();
|
|
return current_help;
|
|
}
|
|
|
|
void PrettyHelp::log_help()
|
|
{
|
|
for (auto content : _root_listing.get_content()) {
|
|
if (content->type.compare("usage") == 0) {
|
|
log_pass_str(content->body, 1, true);
|
|
} else if (content->type.compare("optiongroup") == 0) {
|
|
for (auto option : content->get_content()) {
|
|
log_pass_str(option->body, 1);
|
|
for (auto text : option->get_content()) {
|
|
log_pass_str(text->body, 2);
|
|
log("\n");
|
|
}
|
|
}
|
|
} else {
|
|
log_pass_str(content->body, 0, true);
|
|
log("\n");
|
|
}
|
|
}
|
|
}
|