Skip to content

Commit

Permalink
Indented toml config files for improved legibility (#4558)
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano authored Apr 16, 2024
1 parent 9be56a6 commit e9eaaef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
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

0 comments on commit e9eaaef

Please sign in to comment.