mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 04:29:04 +03:00
chore: structured src/gui, run clang-format
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "config.hpp"
|
||||
#include "core/utils.hpp"
|
||||
#include "core/error.hpp"
|
||||
#include "core/utils.hpp"
|
||||
|
||||
#include <core/palette/color.hpp>
|
||||
#include <filesystem>
|
||||
@@ -25,11 +25,12 @@ Result<void> config::initialize(std::unique_ptr<clrsync::core::io::file> file)
|
||||
m_file = std::move(file);
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Config file is missing");
|
||||
|
||||
|
||||
auto parse_result = m_file->parse();
|
||||
if (!parse_result)
|
||||
return Err<void>(error_code::config_invalid, parse_result.error().message, parse_result.error().context);
|
||||
|
||||
return Err<void>(error_code::config_invalid, parse_result.error().message,
|
||||
parse_result.error().context);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
@@ -68,10 +69,10 @@ void config::copy_file(const std::filesystem::path &src, const std::filesystem::
|
||||
|
||||
std::ifstream in(src, std::ios::binary);
|
||||
std::ofstream out(dst, std::ios::binary);
|
||||
|
||||
|
||||
if (!in || !out)
|
||||
return;
|
||||
|
||||
|
||||
out << in.rdbuf();
|
||||
}
|
||||
|
||||
@@ -79,7 +80,7 @@ void config::copy_dir(const std::filesystem::path &src, const std::filesystem::p
|
||||
{
|
||||
if (!std::filesystem::exists(src))
|
||||
return;
|
||||
|
||||
|
||||
for (auto const &entry : std::filesystem::recursive_directory_iterator(src))
|
||||
{
|
||||
auto rel = std::filesystem::relative(entry.path(), src);
|
||||
@@ -166,7 +167,7 @@ Result<void> config::set_default_theme(const std::string &theme)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
m_file->set_value("general", "default_theme", theme);
|
||||
return m_file->save_file();
|
||||
}
|
||||
@@ -175,7 +176,7 @@ Result<void> config::set_palettes_path(const std::string &path)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
m_file->set_value("general", "palettes_path", path);
|
||||
return m_file->save_file();
|
||||
}
|
||||
@@ -184,7 +185,7 @@ Result<void> config::set_font(const std::string &font)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
m_file->set_value("general", "font", font);
|
||||
return m_file->save_file();
|
||||
}
|
||||
@@ -192,17 +193,17 @@ Result<void> config::set_font_size(int font_size)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
m_file->set_value("general", "font_size", font_size);
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
Result<void> config::update_template(const std::string &key,
|
||||
const clrsync::core::theme_template &theme_template)
|
||||
const clrsync::core::theme_template &theme_template)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
m_themes[key] = theme_template;
|
||||
m_file->set_value("templates." + key, "input_path", theme_template.template_path());
|
||||
m_file->set_value("templates." + key, "output_path", theme_template.output_path());
|
||||
@@ -215,25 +216,29 @@ Result<void> config::remove_template(const std::string &key)
|
||||
{
|
||||
if (!m_file)
|
||||
return Err<void>(error_code::config_missing, "Configuration not initialized");
|
||||
|
||||
|
||||
auto it = m_themes.find(key);
|
||||
if (it == m_themes.end())
|
||||
return Err<void>(error_code::template_not_found, "Template not found", key);
|
||||
|
||||
|
||||
std::filesystem::path template_file = it->second.template_path();
|
||||
if (std::filesystem::exists(template_file))
|
||||
{
|
||||
try {
|
||||
try
|
||||
{
|
||||
std::filesystem::remove(template_file);
|
||||
} catch (const std::exception& e) {
|
||||
return Err<void>(error_code::file_write_failed, "Failed to delete template file", e.what());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
return Err<void>(error_code::file_write_failed, "Failed to delete template file",
|
||||
e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
m_themes.erase(it);
|
||||
|
||||
|
||||
m_file->remove_section("templates." + key);
|
||||
|
||||
|
||||
return m_file->save_file();
|
||||
}
|
||||
|
||||
@@ -264,14 +269,16 @@ const std::unordered_map<std::string, clrsync::core::theme_template> config::tem
|
||||
return m_themes;
|
||||
}
|
||||
|
||||
Result<const clrsync::core::theme_template*> config::template_by_name(const std::string &name) const
|
||||
Result<const clrsync::core::theme_template *> config::template_by_name(
|
||||
const std::string &name) const
|
||||
{
|
||||
auto it = m_themes.find(name);
|
||||
if (it != m_themes.end())
|
||||
{
|
||||
return Ok(&it->second);
|
||||
}
|
||||
return Err<const clrsync::core::theme_template*>(error_code::template_not_found, "Template not found", name);
|
||||
return Err<const clrsync::core::theme_template *>(error_code::template_not_found,
|
||||
"Template not found", name);
|
||||
}
|
||||
|
||||
} // namespace clrsync::core
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef CLRSYNC_CORE_CONFIG_HPP
|
||||
#define CLRSYNC_CORE_CONFIG_HPP
|
||||
|
||||
#include <core/error.hpp>
|
||||
#include <core/io/file.hpp>
|
||||
#include <core/theme/theme_template.hpp>
|
||||
#include <core/error.hpp>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -22,17 +22,16 @@ class config
|
||||
const std::string &palettes_path();
|
||||
const std::string default_theme() const;
|
||||
const std::unordered_map<std::string, clrsync::core::theme_template> templates();
|
||||
Result<const clrsync::core::theme_template*> template_by_name(const std::string &name) const;
|
||||
Result<const clrsync::core::theme_template *> template_by_name(const std::string &name) const;
|
||||
std::filesystem::path get_user_config_dir();
|
||||
|
||||
|
||||
Result<void> set_default_theme(const std::string &theme);
|
||||
Result<void> set_palettes_path(const std::string &path);
|
||||
Result<void> set_font(const std::string &font);
|
||||
Result<void> set_font_size(int font_size);
|
||||
|
||||
Result<void> update_template(const std::string &key,
|
||||
const clrsync::core::theme_template &theme_template);
|
||||
const clrsync::core::theme_template &theme_template);
|
||||
Result<void> remove_template(const std::string &key);
|
||||
static std::filesystem::path get_data_dir();
|
||||
|
||||
@@ -44,8 +43,8 @@ class config
|
||||
std::string m_palettes_dir{};
|
||||
std::unique_ptr<io::file> m_file;
|
||||
std::unordered_map<std::string, theme_template> m_themes{};
|
||||
static void copy_file(const std::filesystem::path& src, const std::filesystem::path& dst);
|
||||
static void copy_dir(const std::filesystem::path& src, const std::filesystem::path& dst);
|
||||
static void copy_file(const std::filesystem::path &src, const std::filesystem::path &dst);
|
||||
static void copy_dir(const std::filesystem::path &src, const std::filesystem::path &dst);
|
||||
void copy_default_configs();
|
||||
};
|
||||
} // namespace clrsync::core
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef CLRSYNC_CORE_ERROR_HPP
|
||||
#define CLRSYNC_CORE_ERROR_HPP
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <optional>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
@@ -11,54 +11,73 @@ namespace clrsync::core
|
||||
enum class error_code
|
||||
{
|
||||
unknown,
|
||||
|
||||
|
||||
file_not_found,
|
||||
file_open_failed,
|
||||
file_write_failed,
|
||||
file_read_failed,
|
||||
dir_create_failed,
|
||||
|
||||
|
||||
parse_failed,
|
||||
invalid_format,
|
||||
|
||||
|
||||
config_missing,
|
||||
config_invalid,
|
||||
|
||||
|
||||
template_not_found,
|
||||
template_load_failed,
|
||||
template_apply_failed,
|
||||
|
||||
|
||||
palette_not_found,
|
||||
palette_load_failed,
|
||||
|
||||
|
||||
init_failed,
|
||||
invalid_arg,
|
||||
resource_missing,
|
||||
};
|
||||
|
||||
inline const char* error_code_string(error_code code)
|
||||
inline const char *error_code_string(error_code code)
|
||||
{
|
||||
switch (code)
|
||||
{
|
||||
case error_code::unknown: return "Unknown error";
|
||||
case error_code::file_not_found: return "File not found";
|
||||
case error_code::file_open_failed: return "Failed to open file";
|
||||
case error_code::file_write_failed: return "Failed to write file";
|
||||
case error_code::file_read_failed: return "Failed to read file";
|
||||
case error_code::dir_create_failed: return "Failed to create directory";
|
||||
case error_code::parse_failed: return "Parse failed";
|
||||
case error_code::invalid_format: return "Invalid format";
|
||||
case error_code::config_missing: return "Configuration missing";
|
||||
case error_code::config_invalid: return "Configuration invalid";
|
||||
case error_code::template_not_found: return "Template not found";
|
||||
case error_code::template_load_failed: return "Failed to load template";
|
||||
case error_code::template_apply_failed: return "Failed to apply template";
|
||||
case error_code::palette_not_found: return "Palette not found";
|
||||
case error_code::palette_load_failed: return "Failed to load palette";
|
||||
case error_code::init_failed: return "Initialization failed";
|
||||
case error_code::invalid_arg: return "Invalid argument";
|
||||
case error_code::resource_missing: return "Resource missing";
|
||||
default: return "Unknown error code";
|
||||
case error_code::unknown:
|
||||
return "Unknown error";
|
||||
case error_code::file_not_found:
|
||||
return "File not found";
|
||||
case error_code::file_open_failed:
|
||||
return "Failed to open file";
|
||||
case error_code::file_write_failed:
|
||||
return "Failed to write file";
|
||||
case error_code::file_read_failed:
|
||||
return "Failed to read file";
|
||||
case error_code::dir_create_failed:
|
||||
return "Failed to create directory";
|
||||
case error_code::parse_failed:
|
||||
return "Parse failed";
|
||||
case error_code::invalid_format:
|
||||
return "Invalid format";
|
||||
case error_code::config_missing:
|
||||
return "Configuration missing";
|
||||
case error_code::config_invalid:
|
||||
return "Configuration invalid";
|
||||
case error_code::template_not_found:
|
||||
return "Template not found";
|
||||
case error_code::template_load_failed:
|
||||
return "Failed to load template";
|
||||
case error_code::template_apply_failed:
|
||||
return "Failed to apply template";
|
||||
case error_code::palette_not_found:
|
||||
return "Palette not found";
|
||||
case error_code::palette_load_failed:
|
||||
return "Failed to load palette";
|
||||
case error_code::init_failed:
|
||||
return "Initialization failed";
|
||||
case error_code::invalid_arg:
|
||||
return "Invalid argument";
|
||||
case error_code::resource_missing:
|
||||
return "Resource missing";
|
||||
default:
|
||||
return "Unknown error code";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,15 +86,20 @@ struct Error
|
||||
error_code code;
|
||||
std::string message;
|
||||
std::string context;
|
||||
|
||||
Error(error_code c) : code(c), message(error_code_string(c)) {}
|
||||
|
||||
Error(error_code c, std::string msg)
|
||||
: code(c), message(std::move(msg)) {}
|
||||
|
||||
|
||||
Error(error_code c) : code(c), message(error_code_string(c))
|
||||
{
|
||||
}
|
||||
|
||||
Error(error_code c, std::string msg) : code(c), message(std::move(msg))
|
||||
{
|
||||
}
|
||||
|
||||
Error(error_code c, std::string msg, std::string ctx)
|
||||
: code(c), message(std::move(msg)), context(std::move(ctx)) {}
|
||||
|
||||
: code(c), message(std::move(msg)), context(std::move(ctx))
|
||||
{
|
||||
}
|
||||
|
||||
std::string description() const
|
||||
{
|
||||
if (context.empty())
|
||||
@@ -84,59 +108,81 @@ struct Error
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class [[nodiscard]] Result
|
||||
template <typename T> class [[nodiscard]] Result
|
||||
{
|
||||
private:
|
||||
private:
|
||||
std::variant<T, Error> m_data;
|
||||
|
||||
public:
|
||||
Result(T value) : m_data(std::move(value)) {}
|
||||
|
||||
Result(Error error) : m_data(std::move(error)) {}
|
||||
|
||||
bool is_ok() const { return std::holds_alternative<T>(m_data); }
|
||||
|
||||
bool is_error() const { return std::holds_alternative<Error>(m_data); }
|
||||
|
||||
explicit operator bool() const { return is_ok(); }
|
||||
|
||||
T& value() & { return std::get<T>(m_data); }
|
||||
const T& value() const & { return std::get<T>(m_data); }
|
||||
T&& value() && { return std::get<T>(std::move(m_data)); }
|
||||
|
||||
const Error& error() const { return std::get<Error>(m_data); }
|
||||
|
||||
|
||||
public:
|
||||
Result(T value) : m_data(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
Result(Error error) : m_data(std::move(error))
|
||||
{
|
||||
}
|
||||
|
||||
bool is_ok() const
|
||||
{
|
||||
return std::holds_alternative<T>(m_data);
|
||||
}
|
||||
|
||||
bool is_error() const
|
||||
{
|
||||
return std::holds_alternative<Error>(m_data);
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return is_ok();
|
||||
}
|
||||
|
||||
T &value() &
|
||||
{
|
||||
return std::get<T>(m_data);
|
||||
}
|
||||
const T &value() const &
|
||||
{
|
||||
return std::get<T>(m_data);
|
||||
}
|
||||
T &&value() &&
|
||||
{
|
||||
return std::get<T>(std::move(m_data));
|
||||
}
|
||||
|
||||
const Error &error() const
|
||||
{
|
||||
return std::get<Error>(m_data);
|
||||
}
|
||||
|
||||
T value_or(T default_value) const
|
||||
{
|
||||
return is_ok() ? std::get<T>(m_data) : std::move(default_value);
|
||||
}
|
||||
|
||||
|
||||
std::optional<T> ok() const
|
||||
{
|
||||
if (is_ok())
|
||||
return std::get<T>(m_data);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
|
||||
std::optional<Error> err() const
|
||||
{
|
||||
if (is_error())
|
||||
return std::get<Error>(m_data);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
auto map(F&& func) -> Result<decltype(func(std::declval<T>()))>
|
||||
|
||||
template <typename F> auto map(F &&func) -> Result<decltype(func(std::declval<T>()))>
|
||||
{
|
||||
using U = decltype(func(std::declval<T>()));
|
||||
if (is_ok())
|
||||
return Result<U>(func(std::get<T>(m_data)));
|
||||
return Result<U>(std::get<Error>(m_data));
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
auto and_then(F&& func) -> decltype(func(std::declval<T>()))
|
||||
|
||||
template <typename F> auto and_then(F &&func) -> decltype(func(std::declval<T>()))
|
||||
{
|
||||
if (is_ok())
|
||||
return func(std::get<T>(m_data));
|
||||
@@ -145,30 +191,47 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class [[nodiscard]] Result<void>
|
||||
template <> class [[nodiscard]] Result<void>
|
||||
{
|
||||
private:
|
||||
private:
|
||||
std::optional<Error> m_error;
|
||||
|
||||
public:
|
||||
Result() : m_error(std::nullopt) {}
|
||||
|
||||
Result(Error error) : m_error(std::move(error)) {}
|
||||
|
||||
bool is_ok() const { return !m_error.has_value(); }
|
||||
|
||||
bool is_error() const { return m_error.has_value(); }
|
||||
|
||||
explicit operator bool() const { return is_ok(); }
|
||||
|
||||
const Error& error() const { return *m_error; }
|
||||
|
||||
std::optional<Error> err() const { return m_error; }
|
||||
|
||||
public:
|
||||
Result() : m_error(std::nullopt)
|
||||
{
|
||||
}
|
||||
|
||||
Result(Error error) : m_error(std::move(error))
|
||||
{
|
||||
}
|
||||
|
||||
bool is_ok() const
|
||||
{
|
||||
return !m_error.has_value();
|
||||
}
|
||||
|
||||
bool is_error() const
|
||||
{
|
||||
return m_error.has_value();
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return is_ok();
|
||||
}
|
||||
|
||||
const Error &error() const
|
||||
{
|
||||
return *m_error;
|
||||
}
|
||||
|
||||
std::optional<Error> err() const
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Result<T> Ok(T value)
|
||||
template <typename T> Result<T> Ok(T value)
|
||||
{
|
||||
return Result<T>(std::move(value));
|
||||
}
|
||||
@@ -178,26 +241,22 @@ inline Result<void> Ok()
|
||||
return Result<void>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> Err(Error error)
|
||||
template <typename T> Result<T> Err(Error error)
|
||||
{
|
||||
return Result<T>(std::move(error));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> Err(error_code code)
|
||||
template <typename T> Result<T> Err(error_code code)
|
||||
{
|
||||
return Result<T>(Error(code));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> Err(error_code code, std::string message)
|
||||
template <typename T> Result<T> Err(error_code code, std::string message)
|
||||
{
|
||||
return Result<T>(Error(code, std::move(message)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Result<T> Err(error_code code, std::string message, std::string context)
|
||||
template <typename T> Result<T> Err(error_code code, std::string message, std::string context)
|
||||
{
|
||||
return Result<T>(Error(code, std::move(message), std::move(context)));
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#ifndef CLRSYNC_CORE_IO_FILE_HPP
|
||||
#define CLRSYNC_CORE_IO_FILE_HPP
|
||||
#include <core/error.hpp>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <core/error.hpp>
|
||||
|
||||
using value_type = std::variant<std::string, uint32_t, int, bool>;
|
||||
|
||||
@@ -16,7 +16,10 @@ class file
|
||||
file() = default;
|
||||
file(std::string path) {};
|
||||
virtual ~file() = default;
|
||||
virtual Result<void> parse() { return Ok(); };
|
||||
virtual Result<void> parse()
|
||||
{
|
||||
return Ok();
|
||||
};
|
||||
virtual const std::string get_string_value(const std::string §ion,
|
||||
const std::string &key) const
|
||||
{
|
||||
@@ -35,14 +38,17 @@ class file
|
||||
return {};
|
||||
}
|
||||
virtual void set_value(const std::string §ion, const std::string &key,
|
||||
const value_type &value)
|
||||
const value_type &value)
|
||||
{
|
||||
insert_or_update_value(section, key, value);
|
||||
}
|
||||
virtual void insert_or_update_value(const std::string §ion, const std::string &key,
|
||||
const value_type &value) {};
|
||||
virtual void remove_section(const std::string §ion) {};
|
||||
virtual Result<void> save_file() { return Ok(); };
|
||||
virtual Result<void> save_file()
|
||||
{
|
||||
return Ok();
|
||||
};
|
||||
};
|
||||
} // namespace clrsync::core::io
|
||||
#endif
|
||||
@@ -15,7 +15,7 @@ Result<void> toml_file::parse()
|
||||
{
|
||||
if (!std::filesystem::exists(m_path))
|
||||
return Err<void>(error_code::file_not_found, "File does not exist", m_path);
|
||||
|
||||
|
||||
m_file = toml::parse_file(m_path);
|
||||
return Ok();
|
||||
}
|
||||
@@ -96,10 +96,10 @@ void toml_file::remove_section(const std::string §ion)
|
||||
{
|
||||
toml::table *tbl = m_file.as_table();
|
||||
auto parts = split(section, '.');
|
||||
|
||||
|
||||
if (parts.empty())
|
||||
return;
|
||||
|
||||
|
||||
for (size_t i = 0; i < parts.size() - 1; ++i)
|
||||
{
|
||||
auto *sub = (*tbl)[parts[i]].as_table();
|
||||
@@ -107,26 +107,29 @@ void toml_file::remove_section(const std::string §ion)
|
||||
return;
|
||||
tbl = sub;
|
||||
}
|
||||
|
||||
|
||||
tbl->erase(parts.back());
|
||||
}
|
||||
|
||||
Result<void> toml_file::save_file()
|
||||
{
|
||||
try {
|
||||
try
|
||||
{
|
||||
std::filesystem::create_directories(std::filesystem::path(m_path).parent_path());
|
||||
} catch (const std::exception& e) {
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
return Err<void>(error_code::dir_create_failed, e.what(), m_path);
|
||||
}
|
||||
|
||||
|
||||
std::ofstream stream(m_path, std::ios::binary);
|
||||
if (!stream)
|
||||
return Err<void>(error_code::file_write_failed, "Failed to open file for writing", m_path);
|
||||
|
||||
|
||||
stream << m_file;
|
||||
if (!stream)
|
||||
return Err<void>(error_code::file_write_failed, "Failed to write to file", m_path);
|
||||
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef CLRSYNC_CORE_IO_TOML_FILE_HPP
|
||||
#define CLRSYNC_CORE_IO_TOML_FILE_HPP
|
||||
#include <core/io/file.hpp>
|
||||
#include <core/error.hpp>
|
||||
#include <core/io/file.hpp>
|
||||
#include <string>
|
||||
#include <toml/toml.hpp>
|
||||
|
||||
|
||||
@@ -111,14 +111,17 @@ void color::from_hex_string(const std::string &str)
|
||||
if (str.empty() || str[0] != '#')
|
||||
throw std::invalid_argument("Invalid hex color format");
|
||||
|
||||
if (str.size() == 7) {
|
||||
if (str.size() == 7)
|
||||
{
|
||||
uint32_t rgb = static_cast<uint32_t>(std::stoul(str.substr(1), nullptr, 16));
|
||||
m_hex = (rgb << 8) | 0xFF;
|
||||
}
|
||||
else if (str.size() == 9) {
|
||||
else if (str.size() == 9)
|
||||
{
|
||||
m_hex = static_cast<uint32_t>(std::stoul(str.substr(1), nullptr, 16));
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
throw std::invalid_argument("Invalid hex color format");
|
||||
}
|
||||
}
|
||||
@@ -137,30 +140,38 @@ const std::string color::to_hex_string_with_alpha() const
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
std::string color::format(const std::string& field) const
|
||||
std::string color::format(const std::string &field) const
|
||||
{
|
||||
auto rgb = to_rgb();
|
||||
auto rgb = to_rgb();
|
||||
auto rgba = to_rgba();
|
||||
auto hslv = to_hsl();
|
||||
auto hslav = to_hsla();
|
||||
|
||||
if (field == "hex") return to_hex_string();
|
||||
if (field == "hex_stripped") {
|
||||
if (field == "hex")
|
||||
return to_hex_string();
|
||||
if (field == "hex_stripped")
|
||||
{
|
||||
auto s = to_hex_string();
|
||||
return s.substr(1);
|
||||
}
|
||||
|
||||
if (field == "hexa") return to_hex_string_with_alpha();
|
||||
if (field == "hexa_stripped") {
|
||||
if (field == "hexa")
|
||||
return to_hex_string_with_alpha();
|
||||
if (field == "hexa_stripped")
|
||||
{
|
||||
auto s = to_hex_string_with_alpha();
|
||||
return s.substr(1);
|
||||
}
|
||||
|
||||
if (field == "r") return std::to_string(rgb.r);
|
||||
if (field == "g") return std::to_string(rgb.g);
|
||||
if (field == "b") return std::to_string(rgb.b);
|
||||
if (field == "r")
|
||||
return std::to_string(rgb.r);
|
||||
if (field == "g")
|
||||
return std::to_string(rgb.g);
|
||||
if (field == "b")
|
||||
return std::to_string(rgb.b);
|
||||
|
||||
if (field == "a") {
|
||||
if (field == "a")
|
||||
{
|
||||
float af = rgba.a / 255.0f;
|
||||
return std::format("{:.2f}", af);
|
||||
}
|
||||
@@ -169,9 +180,7 @@ std::string color::format(const std::string& field) const
|
||||
return std::format("rgb({},{},{})", rgb.r, rgb.g, rgb.b);
|
||||
|
||||
if (field == "rgba")
|
||||
return std::format("rgba({},{},{},{:.2f})",
|
||||
rgba.r, rgba.g, rgba.b,
|
||||
rgba.a / 255.0f);
|
||||
return std::format("rgba({},{},{},{:.2f})", rgba.r, rgba.g, rgba.b, rgba.a / 255.0f);
|
||||
|
||||
if (field == "h")
|
||||
return std::format("{:.0f}", hslv.h);
|
||||
@@ -185,12 +194,10 @@ std::string color::format(const std::string& field) const
|
||||
return std::format("{:.2f}", hslav.a);
|
||||
|
||||
if (field == "hsl")
|
||||
return std::format("hsl({:.0f},{:.2f},{:.2f})",
|
||||
hslv.h, hslv.s, hslv.l);
|
||||
return std::format("hsl({:.0f},{:.2f},{:.2f})", hslv.h, hslv.s, hslv.l);
|
||||
|
||||
if (field == "hsla")
|
||||
return std::format("hsla({:.0f},{:.2f},{:.2f},{:.2f})",
|
||||
hslav.h, hslav.s, hslav.l, hslav.a);
|
||||
return std::format("hsla({:.0f},{:.2f},{:.2f},{:.2f})", hslav.h, hslav.s, hslav.l, hslav.a);
|
||||
|
||||
throw std::runtime_error("Unknown color format: " + field);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class color
|
||||
|
||||
const std::string to_hex_string_with_alpha() const;
|
||||
|
||||
std::string format(const std::string& field) const;
|
||||
std::string format(const std::string &field) const;
|
||||
|
||||
void set(uint32_t hex);
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iterator>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
constexpr const char* COLOR_KEYS[] = {
|
||||
constexpr const char *COLOR_KEYS[] = {
|
||||
// General UI
|
||||
"background",
|
||||
"on_background",
|
||||
@@ -79,31 +79,31 @@ constexpr size_t NUM_COLOR_KEYS = std::size(COLOR_KEYS);
|
||||
inline const std::unordered_map<std::string, uint32_t> DEFAULT_COLORS = {
|
||||
{"background", 0x111111ff},
|
||||
{"on_background", 0xd4d4d4ff},
|
||||
|
||||
|
||||
{"surface", 0x111111ff},
|
||||
{"on_surface", 0xd4d4d4ff},
|
||||
|
||||
|
||||
{"surface_variant", 0x191919ff},
|
||||
{"on_surface_variant", 0xd4d4d4ff},
|
||||
|
||||
|
||||
{"border_focused", 0x2e2e2eff},
|
||||
{"border", 0x242424ff},
|
||||
|
||||
|
||||
{"foreground", 0xd2d2d2ff},
|
||||
|
||||
|
||||
{"cursor", 0xd2d2d2ff},
|
||||
{"accent", 0x9a8652ff},
|
||||
|
||||
|
||||
{"success", 0x668a51ff},
|
||||
{"info", 0x3a898cff},
|
||||
{"warning", 0xb47837ff},
|
||||
{"error", 0xaa4e4aff},
|
||||
|
||||
|
||||
{"on_success", 0xd2d2d2ff},
|
||||
{"on_info", 0xd2d2d2ff},
|
||||
{"on_warning", 0xd2d2d2ff},
|
||||
{"on_error", 0xd2d2d2ff},
|
||||
|
||||
|
||||
{"editor_background", 0x111111ff},
|
||||
{"editor_command", 0x3a898cff},
|
||||
{"editor_comment", 0x849899ff},
|
||||
@@ -119,7 +119,7 @@ inline const std::unordered_map<std::string, uint32_t> DEFAULT_COLORS = {
|
||||
{"editor_string", 0x9a8652ff},
|
||||
{"editor_success", 0x668a51ff},
|
||||
{"editor_warning", 0xb47837ff},
|
||||
|
||||
|
||||
{"base00", 0x111111ff},
|
||||
{"base01", 0x668a51ff},
|
||||
{"base02", 0x9a8652ff},
|
||||
|
||||
@@ -26,7 +26,7 @@ template <typename FileType> class palette_file
|
||||
if (!m_file->parse())
|
||||
return false;
|
||||
m_palette.set_name(m_file->get_string_value("general", "name"));
|
||||
|
||||
|
||||
for (const auto &color_key : COLOR_KEYS)
|
||||
{
|
||||
auto it = DEFAULT_COLORS.find(color_key);
|
||||
@@ -35,7 +35,7 @@ template <typename FileType> class palette_file
|
||||
m_palette.set_color(color_key, core::color(it->second));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const auto &color_key : COLOR_KEYS)
|
||||
{
|
||||
auto color_str = m_file->get_string_value("colors", color_key);
|
||||
|
||||
@@ -6,12 +6,10 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
|
||||
template <typename FileType>
|
||||
class template_manager
|
||||
template <typename FileType> class template_manager
|
||||
{
|
||||
public:
|
||||
template_manager() = default;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef CLRSYNC_CORE_THEME_THEME_RENDERER_HPP
|
||||
#define CLRSYNC_CORE_THEME_THEME_RENDERER_HPP
|
||||
#include <core/config/config.hpp>
|
||||
#include <core/error.hpp>
|
||||
#include <core/palette/palette_manager.hpp>
|
||||
#include <core/theme/template_manager.hpp>
|
||||
#include <core/error.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace clrsync::core
|
||||
@@ -26,7 +26,7 @@ template <typename FileType> class theme_renderer
|
||||
return Err<void>(error_code::palette_not_found, "Palette not found", theme_name);
|
||||
return apply_palette_to_all_templates(*palette);
|
||||
}
|
||||
|
||||
|
||||
Result<void> apply_theme_from_path(const std::string &path)
|
||||
{
|
||||
auto palette = m_pal_manager.load_palette_from_file(path);
|
||||
@@ -44,23 +44,24 @@ template <typename FileType> class theme_renderer
|
||||
auto &tmpl = t_pair.second;
|
||||
if (!tmpl.enabled())
|
||||
continue;
|
||||
|
||||
|
||||
auto load_result = tmpl.load_template();
|
||||
if (!load_result)
|
||||
return load_result;
|
||||
|
||||
|
||||
tmpl.apply_palette(pal);
|
||||
|
||||
|
||||
auto save_result = tmpl.save_output();
|
||||
if (!save_result)
|
||||
return save_result;
|
||||
|
||||
|
||||
if (!tmpl.reload_command().empty())
|
||||
{
|
||||
int result = std::system(tmpl.reload_command().c_str());
|
||||
if (result != 0)
|
||||
{
|
||||
std::cerr << "Warning: Command " << tmpl.reload_command() << " failed with code " << result << "\n";
|
||||
std::cerr << "Warning: Command " << tmpl.reload_command()
|
||||
<< " failed with code " << result << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,13 +47,15 @@ Result<void> theme_template::load_template()
|
||||
{
|
||||
if (!std::filesystem::exists(m_template_path))
|
||||
{
|
||||
return Err<void>(error_code::template_not_found, "Template file is missing", m_template_path);
|
||||
return Err<void>(error_code::template_not_found, "Template file is missing",
|
||||
m_template_path);
|
||||
}
|
||||
|
||||
|
||||
std::ifstream input(m_template_path, std::ios::binary);
|
||||
if (!input)
|
||||
{
|
||||
return Err<void>(error_code::template_load_failed, "Failed to open template file", m_template_path);
|
||||
return Err<void>(error_code::template_load_failed, "Failed to open template file",
|
||||
m_template_path);
|
||||
}
|
||||
|
||||
m_template_data.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
|
||||
@@ -64,7 +66,7 @@ void theme_template::apply_palette(const core::palette &palette)
|
||||
{
|
||||
m_processed_data = m_template_data;
|
||||
|
||||
for (const auto& [key, color] : palette.colors())
|
||||
for (const auto &[key, color] : palette.colors())
|
||||
{
|
||||
// simple replacement: {foreground}
|
||||
replace_all(m_processed_data, "{" + key + "}", color.format("hex"));
|
||||
@@ -96,23 +98,25 @@ Result<void> theme_template::save_output() const
|
||||
{
|
||||
std::filesystem::create_directories(std::filesystem::path(m_output_path).parent_path());
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
return Err<void>(error_code::dir_create_failed, e.what(), m_output_path);
|
||||
}
|
||||
|
||||
|
||||
std::ofstream output(m_output_path, std::ios::binary);
|
||||
if (!output)
|
||||
{
|
||||
return Err<void>(error_code::file_write_failed, "Failed to open output file for writing", m_output_path);
|
||||
return Err<void>(error_code::file_write_failed, "Failed to open output file for writing",
|
||||
m_output_path);
|
||||
}
|
||||
|
||||
output << m_processed_data;
|
||||
if (!output)
|
||||
{
|
||||
return Err<void>(error_code::file_write_failed, "Failed to write to output file", m_output_path);
|
||||
return Err<void>(error_code::file_write_failed, "Failed to write to output file",
|
||||
m_output_path);
|
||||
}
|
||||
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef clrsync_CORE_IO_THEME_TEMPLATE_HPP
|
||||
#define clrsync_CORE_IO_THEME_TEMPLATE_HPP
|
||||
|
||||
#include <core/palette/palette.hpp>
|
||||
#include <core/error.hpp>
|
||||
#include <core/palette/palette.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace clrsync::core
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "utils.hpp"
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
@@ -14,10 +14,10 @@ void print_color_keys()
|
||||
|
||||
std::string get_default_config_path()
|
||||
{
|
||||
const char* env_path = std::getenv("CLRSYNC_CONFIG_PATH");
|
||||
const char *env_path = std::getenv("CLRSYNC_CONFIG_PATH");
|
||||
if (env_path && env_path[0] != '\0')
|
||||
return normalize_path(env_path).string();
|
||||
|
||||
|
||||
std::filesystem::path home = normalize_path("~");
|
||||
std::filesystem::path config_path = home / ".config" / "clrsync" / "config.toml";
|
||||
return config_path.string();
|
||||
@@ -27,7 +27,7 @@ std::string expand_user(const std::string &path)
|
||||
{
|
||||
if (path.empty() || path[0] != '~')
|
||||
return path;
|
||||
|
||||
|
||||
if (path.length() == 1 || path[1] == '/' || path[1] == '\\')
|
||||
{
|
||||
#ifdef _WIN32
|
||||
@@ -37,10 +37,10 @@ std::string expand_user(const std::string &path)
|
||||
#endif
|
||||
if (!home)
|
||||
return path;
|
||||
|
||||
|
||||
if (path.length() == 1)
|
||||
return std::string(home);
|
||||
|
||||
|
||||
return std::string(home) + path.substr(1);
|
||||
}
|
||||
return path;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef CLRSYNC_CORE_UTILS_HPP
|
||||
#define CLRSYNC_CORE_UTILS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include <core/palette/color_keys.hpp>
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
namespace clrsync::core
|
||||
{
|
||||
|
||||
const std::string GIT_SEMVER = "0.1.6+git.g1a1747a";
|
||||
const std::string GIT_SEMVER = "0.1.6+git.g613c2c8";
|
||||
|
||||
const std::string version_string();
|
||||
} // namespace clrsync::core
|
||||
|
||||
Reference in New Issue
Block a user