Skip to content

Commit

Permalink
Small refactor to generalize config creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Apr 3, 2024
1 parent f18103b commit dd15589
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 53 deletions.
91 changes: 46 additions & 45 deletions src/contour/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,15 @@ fs::path configHome()
return configHome("contour");
}

std::string createString(Config const& c)

Check warning on line 184 in src/contour/Config.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: parameter name 'c' is too short, expected at least 3 characters [readability-identifier-length] ```cpp std::string createString(Config const& c) ^ ```
{
return createString(c, YAMLConfigWriter());
}

std::string defaultConfigString()
{
const Config config {};
auto configString = YAMLConfigWriter().createString(config);
auto configString = createString(config);

auto logger = configLog;
logger()(configString);
Expand Down Expand Up @@ -1827,29 +1832,25 @@ void YAMLConfigReader::loadFromEntry(YAML::Node const& node,
logger()("Loading entry: {}, value {}", entry, where.value);
}

std::string YAMLConfigWriter::createString(Config const& c)
template <typename Writer>
std::string createString(Config const& c)

Check warning on line 1836 in src/contour/Config.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: parameter name 'c' is too short, expected at least 3 characters [readability-identifier-length] ```cpp std::string createString(Config const& c) ^ ```
{
auto doc = std::string {};
constexpr int OneOffset = 4;

Writer writer;
auto const process = [&](auto v) {
doc.append(format(addOffset(v.documentation, Offset::levels * OneOffset), v.value()));
doc.append(writer.process(v.documentation, v.value()));
};

auto const processWithDoc = [&](auto&& docString, auto... val) {
doc.append(fmt::format(
fmt::runtime(format(addOffset(std::string(docString.value), Offset::levels * OneOffset), val...)),
fmt::arg("comment", "#")));
doc.append(fmt::format(fmt::runtime(writer.process(docString.value, val...)), fmt::arg("comment", "#")));
};

auto const processWordDelimiters = [&]() {
auto wordDelimiters = c.wordDelimiters.value();
wordDelimiters = std::regex_replace(wordDelimiters, std::regex("\\\\"), "\\$&"); /* \ -> \\ */
wordDelimiters = std::regex_replace(wordDelimiters, std::regex("\""), "\\$&"); /* " -> \" */
doc.append(fmt::format(
fmt::runtime(format(addOffset(c.wordDelimiters.documentation, Offset::levels * OneOffset),
wordDelimiters)),
fmt::arg("comment", "#")));
doc.append(fmt::format(fmt::runtime(writer.process(documentation::WordDelimiters.value, wordDelimiters)),
fmt::arg("comment", "#")));
};

if (c.platformPlugin.value() == "")
Expand All @@ -1862,7 +1863,7 @@ std::string YAMLConfigWriter::createString(Config const& c)
}

// inside renderer:
scoped([&]() {
writer.scoped([&]() {
doc.append("renderer: \n");
process(c.renderingBackend);
process(c.textureAtlasHashtableSlots);
Expand All @@ -1886,7 +1887,7 @@ std::string YAMLConfigWriter::createString(Config const& c)
// inside images:
doc.append("\nimages: \n");

scoped([&]() {
writer.scoped([&]() {
process(c.sixelScrolling);
process(c.maxImageColorRegisters);
process(c.maxImageSize);
Expand All @@ -1895,12 +1896,12 @@ std::string YAMLConfigWriter::createString(Config const& c)
// inside profiles:
doc.append(fmt::format(fmt::runtime(c.profiles.documentation), fmt::arg("comment", "#")));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};

Check warning on line 1899 in src/contour/Config.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: variable name '_' is too short, expected at least 3 characters [readability-identifier-length] ```cpp const auto _ = typename Writer::Offset {}; ^ ```
for (auto&& [name, entry]: c.profiles.value())
{
doc.append(fmt::format(" {}: \n", name));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.shell);
process(entry.ssh);

Expand All @@ -1925,27 +1926,27 @@ std::string YAMLConfigWriter::createString(Config const& c)

process(entry.margins);
// history: section
doc.append(addOffset("history:\n", Offset::levels * OneOffset));
doc.append(Writer::addOffset("history:\n", Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.maxHistoryLineCount);
process(entry.autoScrollOnUpdate);
process(entry.historyScrollMultiplier);
}

// scrollbar: section
doc.append(addOffset("scrollbar:\n", Offset::levels * OneOffset));
doc.append(Writer::addOffset("scrollbar:\n", Writer::Offset::levels * Writer::OneOffset));
;
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.scrollbarPosition);
process(entry.hideScrollbarInAltScreen);
}

// mouse: section
doc.append(addOffset("mouse:\n", Offset::levels * OneOffset));
doc.append(Writer::addOffset("mouse:\n", Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.mouseHideWhileTyping);
}

Expand All @@ -1963,7 +1964,7 @@ std::string YAMLConfigWriter::createString(Config const& c)
"permissions:\n" },
0);
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.changeFont);
process(entry.captureBuffer);
process(entry.displayHostWritableStatusLine);
Expand All @@ -1978,40 +1979,40 @@ std::string YAMLConfigWriter::createString(Config const& c)
process(entry.modalCursorScrollOff);

// status_line
doc.append(addOffset("\n"
"status_line:\n",
Offset::levels * OneOffset));
doc.append(Writer::addOffset("\n"
"status_line:\n",
Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.initialStatusDisplayType);
process(entry.statusDisplayPosition);
process(entry.syncWindowTitleWithHostWritableStatusDisplay);

doc.append(addOffset("indicator:\n", Offset::levels * OneOffset));
doc.append(Writer::addOffset("indicator:\n", Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.indicatorStatusLineLeft);
process(entry.indicatorStatusLineMiddle);
process(entry.indicatorStatusLineRight);
}
}

doc.append(addOffset("\n"
doc.append(Writer::addOffset("\n"
"background:\n",
Offset::levels * OneOffset));
Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.backgroundOpacity);
process(entry.backgroundBlur);
}

process(entry.colors);

doc.append(addOffset("\n"
doc.append(Writer::addOffset("\n"
"hyperlink_decoration:\n",
Offset::levels * OneOffset));
Writer::Offset::levels * Writer::OneOffset));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
process(entry.hyperlinkDecorationNormal);
process(entry.hyperlinkDecorationHover);
}
Expand All @@ -2020,19 +2021,19 @@ std::string YAMLConfigWriter::createString(Config const& c)
}

doc.append(fmt::format(fmt::runtime(c.colorschemes.documentation), fmt::arg("comment", "#")));
scoped([&]() {
writer.scoped([&]() {
for (auto&& [name, entry]: c.colorschemes.value())
{
doc.append(fmt::format(" {}: \n", name));

{
const auto _ = Offset {};
doc.append(fmt::format(fmt::runtime(addOffset("{comment} Default colors\n"
const auto _ = typename Writer::Offset {};
doc.append(fmt::format(fmt::runtime(Writer::addOffset("{comment} Default colors\n"
"default:\n",
Offset::levels * OneOffset)),
Writer::Offset::levels * Writer::OneOffset)),
fmt::arg("comment", "#")));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};
processWithDoc(
documentation::DefaultColors, entry.defaultBackground, entry.defaultForeground);

Expand Down Expand Up @@ -2155,22 +2156,22 @@ std::string YAMLConfigWriter::createString(Config const& c)
entry.dimColor(7));
}

doc.append(addOffset("", Offset::levels * OneOffset));
doc.append(Writer::addOffset("", Writer::Offset::levels * Writer::OneOffset));
}
}
});

doc.append(fmt::format(fmt::runtime(c.inputMappings.documentation), fmt::arg("comment", "#")));
{
const auto _ = Offset {};
const auto _ = typename Writer::Offset {};

Check warning on line 2166 in src/contour/Config.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: variable name '_' is too short, expected at least 3 characters [readability-identifier-length] ```cpp const auto _ = typename Writer::Offset {}; ^ ```
for (auto&& entry: c.inputMappings.value().keyMappings)
doc.append(addOffset(format(entry), Offset::levels * OneOffset));
doc.append(Writer::addOffset(writer.format(entry), Writer::Offset::levels * Writer::OneOffset));

for (auto&& entry: c.inputMappings.value().charMappings)
doc.append(addOffset(format(entry), Offset::levels * OneOffset));
doc.append(Writer::addOffset(writer.format(entry), Writer::Offset::levels * Writer::OneOffset));

for (auto&& entry: c.inputMappings.value().mouseMappings)
doc.append(addOffset(format(entry), Offset::levels * OneOffset));
doc.append(Writer::addOffset(writer.format(entry), Writer::Offset::levels * Writer::OneOffset));
}
return doc;
}
Expand Down
44 changes: 36 additions & 8 deletions src/contour/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <regex>
#include <set>
#include <string>
#include <string_view>
#include <system_error>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -884,13 +885,9 @@ struct YAMLConfigReader
actions::Action action);
};

struct YAMLConfigWriter
struct Writer
{
std::string static addOffset(std::string const& doc, size_t off)
{
auto offset = std::string(off, ' ');
return std::regex_replace(doc, std::regex(".+\n"), offset + "$&");
}
virtual ~Writer() = default;

Check warning on line 890 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: class 'Writer' defines a default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions] ```cpp ct Writer ^ ```

struct Offset
{
Expand All @@ -906,8 +903,6 @@ struct YAMLConfigWriter
lambda();
}

std::string createString(Config const& c);

template <typename T>
auto format(T v)
{
Expand All @@ -920,6 +915,39 @@ struct YAMLConfigWriter
return fmt::format(fmt::runtime(doc), format(args)..., fmt::arg("comment", "#"));
}

};

template <typename T>
std::string createString(Config const& c, T)
{
return createString<T>(c);

Check warning on line 923 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: parameter name 'c' is too short, expected at least 3 characters [readability-identifier-length] ```cpp st& c, T) ^ ```

Check warning on line 923 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: all parameters should be named in a function [readability-named-parameter] ```suggestion st& c, T /*unused*/) ```
}

struct YAMLConfigWriter: Writer
{

static constexpr int OneOffset = 4;
using Writer::format;
std::string static addOffset(std::string const& doc, size_t off)
{
auto offset = std::string(off, ' ');
return std::regex_replace(doc, std::regex(".+\n"), offset + "$&");
}

template <typename T>
std::string process(std::string doc, T val)
{

Check warning on line 939 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: the parameter 'doc' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param] ```cpp doc, T val) ^ ```
return format(addOffset(doc, Offset::levels * OneOffset), val);

Check warning on line 940 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: performing an implicit widening conversion to type 'size_t' (aka 'unsigned long') of a multiplication performed in type 'int' [bugprone-implicit-widening-of-multiplication-result] ```cpp val) ^ ``` <details> <summary>Additional context</summary> **src/contour/Config.h:939:** make conversion explicit to silence this warning ```cpp val) ^ ``` **src/contour/Config.h:939:** perform multiplication in a wider type ```cpp val) ^ ``` </details>
}

template <typename... T>
std::string process(std::string doc, T... val)

Check warning on line 944 in src/contour/Config.h

View workflow job for this annotation

GitHub Actions / clang-tidy-review

clang-tidy

warning: the parameter 'doc' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param] ```cpp ename... T> ^ ```
{
return format(addOffset(doc, Offset::levels * OneOffset), val...);
}



[[nodiscard]] std::string format(KeyInputMapping v)
{
return format("{:<30},{:<30},{:<30}\n",
Expand Down

0 comments on commit dd15589

Please sign in to comment.