mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 12:37:41 +03:00
init
This commit is contained in:
35
src/core/theme/template_manager.hpp
Normal file
35
src/core/theme/template_manager.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef CLRSYNC_CORE_THEME_TEMPLATE_MANAGER_HPP
|
||||
#define CLRSYNC_CORE_THEME_TEMPLATE_MANAGER_HPP
|
||||
|
||||
#include <core/config/config.hpp>
|
||||
#include <core/theme/theme_template.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
|
||||
template <typename FileType>
|
||||
class template_manager
|
||||
{
|
||||
public:
|
||||
template_manager() = default;
|
||||
std::unordered_map<std::string, theme_template> &templates()
|
||||
{
|
||||
auto themes = config::instance().templates();
|
||||
m_templates.clear();
|
||||
for (const auto &t : themes)
|
||||
{
|
||||
m_templates.insert({t.first, t.second});
|
||||
}
|
||||
return m_templates;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, theme_template> m_templates{};
|
||||
};
|
||||
|
||||
} // namespace clrsync::core
|
||||
|
||||
#endif
|
||||
58
src/core/theme/theme_renderer.hpp
Normal file
58
src/core/theme/theme_renderer.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef CLRSYNC_CORE_THEME_THEME_RENDERER_HPP
|
||||
#define CLRSYNC_CORE_THEME_THEME_RENDERER_HPP
|
||||
#include <core/config/config.hpp>
|
||||
#include <core/palette/palette_manager.hpp>
|
||||
#include <core/theme/template_manager.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
|
||||
template <typename FileType> class theme_renderer
|
||||
{
|
||||
public:
|
||||
theme_renderer()
|
||||
{
|
||||
auto &cfg = config::instance();
|
||||
m_pal_manager.load_palettes_from_directory(cfg.palettes_path());
|
||||
m_template_manager = template_manager<FileType>();
|
||||
}
|
||||
|
||||
void apply_theme(const std::string &theme_name)
|
||||
{
|
||||
auto palette = m_pal_manager.get_palette(theme_name);
|
||||
if (!palette)
|
||||
throw std::runtime_error("Palette not found: " + theme_name);
|
||||
apply_palette_to_all_templates(*palette);
|
||||
}
|
||||
void apply_theme_from_path(const std::string &path)
|
||||
{
|
||||
auto palette = m_pal_manager.load_palette_from_file(path);
|
||||
apply_palette_to_all_templates(palette);
|
||||
}
|
||||
|
||||
private:
|
||||
palette_manager<FileType> m_pal_manager;
|
||||
template_manager<FileType> m_template_manager;
|
||||
|
||||
void apply_palette_to_all_templates(const palette &pal)
|
||||
{
|
||||
for (auto &t_pair : m_template_manager.templates())
|
||||
{
|
||||
auto &tmpl = t_pair.second;
|
||||
if (!tmpl.enabled())
|
||||
continue;
|
||||
tmpl.load_template();
|
||||
tmpl.apply_palette(pal);
|
||||
tmpl.save_output();
|
||||
if (!tmpl.reload_command().empty())
|
||||
{
|
||||
std::system(tmpl.reload_command().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace clrsync::core
|
||||
|
||||
#endif
|
||||
138
src/core/theme/theme_template.cpp
Normal file
138
src/core/theme/theme_template.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "theme_template.hpp"
|
||||
#include "core/utils.hpp"
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
theme_template::theme_template(const std::string &name, const std::string &template_path,
|
||||
const std::string &out_path)
|
||||
: m_name(name), m_template_path(expand_user(template_path)),
|
||||
m_output_path(expand_user(out_path))
|
||||
{
|
||||
}
|
||||
|
||||
const std::string &theme_template::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void theme_template::set_name(const std::string &name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
const std::string &theme_template::template_path() const
|
||||
{
|
||||
return m_template_path;
|
||||
}
|
||||
|
||||
void theme_template::set_template_path(const std::string &path)
|
||||
{
|
||||
m_template_path = expand_user(path);
|
||||
}
|
||||
|
||||
const std::string &theme_template::output_path() const
|
||||
{
|
||||
return m_output_path;
|
||||
}
|
||||
|
||||
void theme_template::set_output_path(const std::string &path)
|
||||
{
|
||||
m_output_path = expand_user(path);
|
||||
}
|
||||
|
||||
void theme_template::load_template()
|
||||
{
|
||||
std::ifstream input(m_template_path, std::ios::binary);
|
||||
if (!input)
|
||||
throw std::runtime_error("Failed to open template file: " + m_template_path);
|
||||
|
||||
m_template_data.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
void theme_template::apply_palette(const core::palette &palette)
|
||||
{
|
||||
m_processed_data = m_template_data;
|
||||
|
||||
for (const auto& [key, color] : palette.colors())
|
||||
{
|
||||
// simple replacement: {foreground}
|
||||
replace_all(m_processed_data, "{" + key + "}", color.format("hex"));
|
||||
|
||||
// mutli-component: {foreground.r}, {foreground.rgb}, etc.
|
||||
std::string prefix = "{" + key + ".";
|
||||
size_t pos = 0;
|
||||
while ((pos = m_processed_data.find(prefix, pos)) != std::string::npos)
|
||||
{
|
||||
size_t end = m_processed_data.find('}', pos);
|
||||
if (end == std::string::npos)
|
||||
break;
|
||||
|
||||
const size_t field_start = pos + prefix.size();
|
||||
std::string field = m_processed_data.substr(field_start, end - field_start);
|
||||
|
||||
std::string value = color.format(field);
|
||||
|
||||
m_processed_data.replace(pos, end - pos + 1, value);
|
||||
|
||||
pos += value.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void theme_template::save_output() const
|
||||
{
|
||||
std::filesystem::create_directories(std::filesystem::path(m_output_path).parent_path());
|
||||
std::ofstream output(m_output_path, std::ios::binary);
|
||||
if (!output)
|
||||
throw std::runtime_error("Failed to write output file: " + m_output_path);
|
||||
|
||||
output << m_processed_data;
|
||||
}
|
||||
|
||||
const std::string &theme_template::raw_template() const
|
||||
{
|
||||
return m_template_data;
|
||||
}
|
||||
|
||||
const std::string &theme_template::processed_template() const
|
||||
{
|
||||
return m_processed_data;
|
||||
}
|
||||
|
||||
void theme_template::replace_all(std::string &str, const std::string &from, const std::string &to)
|
||||
{
|
||||
if (from.empty())
|
||||
return;
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = str.find(from, pos)) != std::string::npos)
|
||||
{
|
||||
str.replace(pos, from.length(), to);
|
||||
pos += to.length();
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &theme_template::reload_command() const
|
||||
{
|
||||
return m_reload_cmd;
|
||||
}
|
||||
|
||||
void theme_template::set_reload_command(const std::string &cmd)
|
||||
{
|
||||
m_reload_cmd = cmd;
|
||||
}
|
||||
|
||||
bool theme_template::enabled() const
|
||||
{
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void theme_template::set_enabled(bool enabled)
|
||||
{
|
||||
m_enabled = enabled;
|
||||
}
|
||||
|
||||
} // namespace clrsync::core
|
||||
60
src/core/theme/theme_template.hpp
Normal file
60
src/core/theme/theme_template.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef clrsync_CORE_IO_THEME_TEMPLATE_HPP
|
||||
#define clrsync_CORE_IO_THEME_TEMPLATE_HPP
|
||||
|
||||
#include <core/palette/palette.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace clrsync::core
|
||||
{
|
||||
|
||||
class theme_template
|
||||
{
|
||||
public:
|
||||
theme_template() = default;
|
||||
theme_template(const std::string &name, const std::string &template_path,
|
||||
const std::string &out_path);
|
||||
|
||||
const std::string &name() const;
|
||||
|
||||
void set_name(const std::string &name);
|
||||
|
||||
const std::string &template_path() const;
|
||||
|
||||
void set_template_path(const std::string &path);
|
||||
|
||||
const std::string &output_path() const;
|
||||
|
||||
void set_output_path(const std::string &path);
|
||||
|
||||
void load_template();
|
||||
|
||||
void apply_palette(const core::palette &palette);
|
||||
|
||||
void save_output() const;
|
||||
|
||||
const std::string &raw_template() const;
|
||||
|
||||
const std::string &processed_template() const;
|
||||
|
||||
const std::string &reload_command() const;
|
||||
|
||||
void set_reload_command(const std::string &cmd);
|
||||
|
||||
bool enabled() const;
|
||||
|
||||
void set_enabled(bool enabled);
|
||||
|
||||
private:
|
||||
std::string m_name{};
|
||||
std::string m_template_path{};
|
||||
std::string m_output_path{};
|
||||
bool m_enabled = true;
|
||||
std::string m_template_data{};
|
||||
std::string m_processed_data{};
|
||||
std::string m_reload_cmd{};
|
||||
|
||||
static void replace_all(std::string &str, const std::string &from, const std::string &to);
|
||||
};
|
||||
} // namespace clrsync::core
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user