add initial bulk run from pre-commit over all files

This commit is contained in:
Felix Schurk
2024-07-29 22:34:51 +02:00
parent 665aeeef61
commit 93356b39c3
418 changed files with 21354 additions and 23858 deletions

View File

@@ -28,63 +28,57 @@
// cmake.h include header must come first
#include <CmdConfig.h>
#include <sstream>
#include <algorithm>
#include <Context.h>
#include <JSON.h>
#include <shared.h>
#include <format.h>
#include <shared.h>
#include <algorithm>
#include <sstream>
////////////////////////////////////////////////////////////////////////////////
CmdConfig::CmdConfig ()
{
_keyword = "config";
_usage = "task config [name [value | '']]";
_description = "Change settings in the task configuration";
_read_only = true;
_displays_id = false;
_needs_gc = false;
_uses_context = false;
_accepts_filter = false;
CmdConfig::CmdConfig() {
_keyword = "config";
_usage = "task config [name [value | '']]";
_description = "Change settings in the task configuration";
_read_only = true;
_displays_id = false;
_needs_gc = false;
_uses_context = false;
_accepts_filter = false;
_accepts_modifications = false;
_accepts_miscellaneous = true;
_category = Command::Category::config;
_category = Command::Category::config;
}
////////////////////////////////////////////////////////////////////////////////
bool CmdConfig::setConfigVariable (
const std::string& name,
const std::string& value,
bool confirmation /* = false */)
{
bool CmdConfig::setConfigVariable(const std::string& name, const std::string& value,
bool confirmation /* = false */) {
// Read .taskrc (or equivalent)
std::vector <std::string> contents;
File::read (Context::getContext ().config.file (), contents);
std::vector<std::string> contents;
File::read(Context::getContext().config.file(), contents);
auto found = false;
auto change = false;
for (auto& line : contents)
{
for (auto& line : contents) {
// Get l-trimmed version of the line
auto trimmed_line = trim (line, " ");
auto trimmed_line = trim(line, " ");
// If there is a comment on the line, it must follow the pattern.
auto comment = line.find ('#');
auto pos = trimmed_line.find (name + '=');
auto comment = line.find('#');
auto pos = trimmed_line.find(name + '=');
// TODO: Use std::regex here
if (pos == 0)
{
if (pos == 0) {
found = true;
if (!confirmation ||
confirm (format ("Are you sure you want to change the value of '{1}' from '{2}' to '{3}'?", name, Context::getContext ().config.get (name), value)))
{
auto new_line = line.substr (0, pos + name.length () + 1) + json::encode (value);
confirm(format("Are you sure you want to change the value of '{1}' from '{2}' to '{3}'?",
name, Context::getContext().config.get(name), value))) {
auto new_line = line.substr(0, pos + name.length() + 1) + json::encode(value);
// Preserve the comment
if (comment != std::string::npos)
new_line += " " + line.substr (comment);
if (comment != std::string::npos) new_line += " " + line.substr(comment);
// Rewrite the line
line = new_line;
@@ -94,61 +88,52 @@ bool CmdConfig::setConfigVariable (
}
// Not found, so append instead.
if (! found &&
(! confirmation ||
confirm (format ("Are you sure you want to add '{1}' with a value of '{2}'?", name, value))))
{
contents.push_back (name + '=' + json::encode (value));
if (!found &&
(!confirmation ||
confirm(format("Are you sure you want to add '{1}' with a value of '{2}'?", name, value)))) {
contents.push_back(name + '=' + json::encode(value));
change = true;
}
if (change)
File::write (Context::getContext ().config.file (), contents);
if (change) File::write(Context::getContext().config.file(), contents);
return change;
}
////////////////////////////////////////////////////////////////////////////////
int CmdConfig::unsetConfigVariable (const std::string& name, bool confirmation /* = false */)
{
int CmdConfig::unsetConfigVariable(const std::string& name, bool confirmation /* = false */) {
// Read .taskrc (or equivalent)
std::vector <std::string> contents;
File::read (Context::getContext ().config.file (), contents);
std::vector<std::string> contents;
File::read(Context::getContext().config.file(), contents);
auto found = false;
auto change = false;
for (auto line = contents.begin (); line != contents.end (); )
{
for (auto line = contents.begin(); line != contents.end();) {
auto lineDeleted = false;
// Get l-trimmed version of the line
// If there is a comment on the line, it must follow the pattern.
auto pos = trim (*line, " ").find (name + '=');
auto pos = trim(*line, " ").find(name + '=');
// TODO: Use std::regex here
if (pos == 0)
{
if (pos == 0) {
found = true;
// Remove name
if (!confirmation ||
confirm (format ("Are you sure you want to remove '{1}'?", name)))
{
if (!confirmation || confirm(format("Are you sure you want to remove '{1}'?", name))) {
// vector::erase method returns a valid iterator to the next object
line = contents.erase (line);
line = contents.erase(line);
lineDeleted = true;
change = true;
}
}
if (! lineDeleted)
line++;
if (!lineDeleted) line++;
}
if (change)
File::write (Context::getContext ().config.file (), contents);
if (change) File::write(Context::getContext().config.file(), contents);
if (change && found)
return 0;
@@ -159,107 +144,88 @@ int CmdConfig::unsetConfigVariable (const std::string& name, bool confirmation /
}
////////////////////////////////////////////////////////////////////////////////
int CmdConfig::execute (std::string& output)
{
int CmdConfig::execute(std::string& output) {
auto rc = 0;
std::stringstream out;
// Get the non-attribute, non-fancy command line arguments.
std::vector <std::string> words = Context::getContext ().cli2.getWords ();
std::vector<std::string> words = Context::getContext().cli2.getWords();
// Support:
// task config name value # set name to value
// task config name "" # set name to blank
// task config name # remove name
if (words.size ())
{
auto confirmation = Context::getContext ().config.getBoolean ("confirmation");
if (words.size()) {
auto confirmation = Context::getContext().config.getBoolean("confirmation");
auto found = false;
auto name = words[0];
std::string value = "";
// Join the remaining words into config variable's value
if (words.size () > 1)
{
for (unsigned int i = 1; i < words.size (); ++i)
{
if (i > 1)
value += ' ';
if (words.size() > 1) {
for (unsigned int i = 1; i < words.size(); ++i) {
if (i > 1) value += ' ';
value += words[i];
}
}
if (name != "")
{
if (name != "") {
auto change = false;
// task config name value
// task config name ""
if (words.size () > 1)
change = setConfigVariable(name, value, confirmation);
if (words.size() > 1) change = setConfigVariable(name, value, confirmation);
// task config name
else
{
else {
rc = unsetConfigVariable(name, confirmation);
if (rc == 0)
{
if (rc == 0) {
change = true;
found = true;
}
else if (rc == 1)
} else if (rc == 1)
found = true;
if (! found)
throw format ("No entry named '{1}' found.", name);
if (!found) throw format("No entry named '{1}' found.", name);
}
// Show feedback depending on whether .taskrc has been rewritten
if (change)
{
out << format ("Config file {1} modified.", Context::getContext ().config.file ())
<< '\n';
}
else
if (change) {
out << format("Config file {1} modified.", Context::getContext().config.file()) << '\n';
} else
out << "No changes made.\n";
}
else
throw std::string ("Specify the name of a config variable to modify.");
} else
throw std::string("Specify the name of a config variable to modify.");
output = out.str ();
}
else
throw std::string ("Specify the name of a config variable to modify.");
output = out.str();
} else
throw std::string("Specify the name of a config variable to modify.");
return rc;
}
////////////////////////////////////////////////////////////////////////////////
CmdCompletionConfig::CmdCompletionConfig ()
{
_keyword = "_config";
_usage = "task _config";
_description = "Lists all supported configuration variables, for completion purposes";
_read_only = true;
_displays_id = false;
_needs_gc = false;
_uses_context = false;
_accepts_filter = false;
CmdCompletionConfig::CmdCompletionConfig() {
_keyword = "_config";
_usage = "task _config";
_description = "Lists all supported configuration variables, for completion purposes";
_read_only = true;
_displays_id = false;
_needs_gc = false;
_uses_context = false;
_accepts_filter = false;
_accepts_modifications = false;
_accepts_miscellaneous = false;
_category = Command::Category::internal;
_category = Command::Category::internal;
}
////////////////////////////////////////////////////////////////////////////////
int CmdCompletionConfig::execute (std::string& output)
{
auto configs = Context::getContext ().config.all ();
std::sort (configs.begin (), configs.end ());
int CmdCompletionConfig::execute(std::string& output) {
auto configs = Context::getContext().config.all();
std::sort(configs.begin(), configs.end());
for (const auto& config : configs)
output += config + '\n';
for (const auto& config : configs) output += config + '\n';
return 0;
}