Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate indented toml config files for improved legibility #4558

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions nano/lib/tomlconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,24 @@ void nano::tomlconfig::erase_default_values (tomlconfig & defaults_a)
erase_defaults (defaults_l.get_tree (), self.get_tree (), get_tree ());
}

std::string nano::tomlconfig::to_string ()
{
std::stringstream ss;
cpptoml::toml_writer writer{ ss, "" };
tree->accept (writer);
return ss.str ();
}

std::string nano::tomlconfig::to_string_commented_entries ()
std::string nano::tomlconfig::to_string (bool comment_values)
{
std::stringstream ss, ss_processed;
cpptoml::toml_writer writer{ ss, "" };
tree->accept (writer);
std::string line;
while (std::getline (ss, line, '\n'))
{
if (!line.empty () && line[0] != '#' && line[0] != '[')
if (!line.empty () && line[0] != '[')
{
line = "#" + line;
if (line[0] == '#') // Already commented
{
line = "\t" + line;
}
else
{
line = comment_values ? "\t# " + line : "\t" + line;
}
}
ss_processed << line << std::endl;
}
Expand Down
3 changes: 1 addition & 2 deletions nano/lib/tomlconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class tomlconfig : public nano::configbase
tomlconfig & erase (std::string const & key_a);
std::shared_ptr<cpptoml::array> create_array (std::string const & key, boost::optional<char const *> documentation_a);
void erase_default_values (tomlconfig & defaults_a);
std::string to_string ();
std::string to_string_commented_entries ();
std::string to_string (bool comment_values);

/** Set value for the given key. Any existing value will be overwritten. */
template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions nano/node/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,11 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map

if (vm.count ("use_defaults"))
{
std::cout << toml.to_string () << std::endl;
std::cout << toml.to_string (false) << std::endl;
}
else
{
std::cout << toml.to_string_commented_entries () << std::endl;
std::cout << toml.to_string (true) << std::endl;
}
}
}
Expand Down
Loading