fix: handle missing files

This commit is contained in:
2025-12-08 13:40:07 +03:00
parent 33bca75990
commit 264fc6ce54
8 changed files with 44 additions and 28 deletions

View File

@@ -59,7 +59,7 @@ set(CORE_SOURCES
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
add_library(clrsync_core SHARED ${CORE_SOURCES}) add_library(clrsync_core SHARED ${CORE_SOURCES})
target_include_directories(clrsync_core PUBLIC src SYSTEM lib) target_include_directories(clrsync_core PUBLIC src SYSTEM lib)
target_compile_definitions(clrsync_core PRIVATE target_compile_definitions(clrsync_core PUBLIC
CLRSYNC_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/clrsync\" CLRSYNC_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/clrsync\"
) )

View File

@@ -1,4 +1,5 @@
#include "config.hpp" #include "config.hpp"
#include "core/utils.hpp"
#include <core/palette/color.hpp> #include <core/palette/color.hpp>
#include <filesystem> #include <filesystem>
@@ -23,35 +24,34 @@ void config::initialize(std::unique_ptr<clrsync::core::io::file> file)
std::filesystem::path config::get_user_config_dir() std::filesystem::path config::get_user_config_dir()
{ {
auto home = expand_user("~");
#ifdef _WIN32 #ifdef _WIN32
if (const char *appdata = std::getenv("APPDATA")) return home + "\\.config\\clrsync";
return std::filesystem::path(appdata) / "clrsync";
else
return std::filesystem::path("C:/clrsync");
#else #else
if (const char *xdg = std::getenv("XDG_CONFIG_HOME")) return home + "/.config/clrsync";
return std::filesystem::path(xdg) / "clrsync";
else if (const char *home = std::getenv("HOME"))
return std::filesystem::path(home) / ".config/clrsync";
else
return std::filesystem::path("/tmp/clrsync");
#endif #endif
} }
void config::copy_default_configs() void config::copy_default_configs()
{ {
std::filesystem::path user_config = get_user_config_dir(); std::filesystem::path user_config = get_user_config_dir();
std::filesystem::path default_dir = CLRSYNC_DATADIR;
if (!std::filesystem::exists(user_config)) if (!std::filesystem::exists(user_config))
{ {
std::filesystem::create_directories(user_config); std::filesystem::create_directories(user_config);
std::filesystem::path default_dir = CLRSYNC_DATADIR;
std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml"); std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml");
std::filesystem::copy(default_dir / "templates", user_config / "templates", std::filesystem::copy(default_dir / "templates", user_config / "templates",
std::filesystem::copy_options::recursive); std::filesystem::copy_options::recursive);
std::filesystem::copy(default_dir / "palettes", user_config / "palettes", std::filesystem::copy(default_dir / "palettes", user_config / "palettes",
std::filesystem::copy_options::recursive); std::filesystem::copy_options::recursive);
return;
}
if (!std::filesystem::exists(user_config / "config.toml"))
{
std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml");
} }
} }

View File

@@ -2,6 +2,7 @@
#define CLRSYNC_CORE_PALETTE_PALETTE_MANAGER_HPP #define CLRSYNC_CORE_PALETTE_PALETTE_MANAGER_HPP
#include "core/utils.hpp" #include "core/utils.hpp"
#include <iostream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@@ -19,6 +20,11 @@ template <typename FileType> class palette_manager
void load_palettes_from_directory(const std::string &directory_path) void load_palettes_from_directory(const std::string &directory_path)
{ {
auto directory_path_expanded = expand_user(directory_path); auto directory_path_expanded = expand_user(directory_path);
if (!std::filesystem::exists(directory_path_expanded))
{
std::cerr << "Palettes directory does not exist\n" ;
return;
}
for (const auto &entry : std::filesystem::directory_iterator(directory_path_expanded)) for (const auto &entry : std::filesystem::directory_iterator(directory_path_expanded))
{ {
if (entry.is_regular_file()) if (entry.is_regular_file())

View File

@@ -1,8 +1,8 @@
#include "theme_template.hpp" #include "theme_template.hpp"
#include "core/utils.hpp" #include "core/utils.hpp"
#include <filesystem> #include <filesystem>
#include <format>
#include <fstream> #include <fstream>
#include <iostream>
namespace clrsync::core namespace clrsync::core
{ {
@@ -45,6 +45,11 @@ void theme_template::set_output_path(const std::string &path)
void theme_template::load_template() void theme_template::load_template()
{ {
if (!std::filesystem::exists(m_template_path))
{
std::cerr << "Template file '" << m_template_path << "' is missing\n";
return;
}
std::ifstream input(m_template_path, std::ios::binary); std::ifstream input(m_template_path, std::ios::binary);
if (!input) if (!input)
throw std::runtime_error("Failed to open template file: " + m_template_path); throw std::runtime_error("Failed to open template file: " + m_template_path);

View File

@@ -13,16 +13,12 @@ void print_color_keys()
std::string get_default_config_path() std::string get_default_config_path()
{ {
auto home = expand_user("~");
#ifdef _WIN32 #ifdef _WIN32
const char *appdata = std::getenv("APPDATA"); // "C:\Users\<User>\AppData\Roaming" return home + "\\.config\\clrsync\\config.toml";
if (!appdata)
throw std::runtime_error("APPDATA environment variable not set");
return std::string(appdata) + "\\clrsync\\config.toml";
#else #else
const char *home = std::getenv("HOME"); return home + "/.config/clrsync/config.toml";
if (!home)
throw std::runtime_error("HOME environment variable not set");
return std::string(home) + "/.config/clrsync/config.toml";
#endif #endif
} }

View File

@@ -172,6 +172,7 @@ void color_scheme_editor::render_controls()
apply_palette_to_imgui(); apply_palette_to_imgui();
apply_palette_to_editor(); apply_palette_to_editor();
notify_palette_changed(); notify_palette_changed();
m_controller.select_palette(new_palette_name_buf);
new_palette_name_buf[0] = 0; new_palette_name_buf[0] = 0;
} }
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
@@ -328,6 +329,12 @@ void color_scheme_editor::render_preview_content()
{ {
const auto &current = m_controller.current_palette(); const auto &current = m_controller.current_palette();
if (current.colors().empty())
{
ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f), "Current palette is empty");
return;
}
auto get_color = [&](const std::string &key) -> ImVec4 { auto get_color = [&](const std::string &key) -> ImVec4 {
auto it = current.colors().find(key); auto it = current.colors().find(key);
if (it != current.colors().end()) if (it != current.colors().end())
@@ -389,6 +396,8 @@ void color_scheme_editor::render_preview_content()
void color_scheme_editor::apply_palette_to_editor() void color_scheme_editor::apply_palette_to_editor()
{ {
const auto &current = m_controller.current_palette(); const auto &current = m_controller.current_palette();
if (current.colors().empty())
return;
auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t { auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t {
auto it = current.colors().find(key); auto it = current.colors().find(key);
@@ -446,6 +455,9 @@ void color_scheme_editor::apply_palette_to_editor()
void color_scheme_editor::apply_palette_to_imgui() const void color_scheme_editor::apply_palette_to_imgui() const
{ {
const auto &current = m_controller.current_palette(); const auto &current = m_controller.current_palette();
if (current.colors().empty())
return;
auto getColor = [&](const std::string &key, const std::string &fallback = "") -> ImVec4 { auto getColor = [&](const std::string &key, const std::string &fallback = "") -> ImVec4 {
auto it = current.colors().find(key); auto it = current.colors().find(key);

View File

@@ -28,14 +28,9 @@ void palette_controller::select_palette(const std::string& name)
void palette_controller::create_palette(const std::string& name) void palette_controller::create_palette(const std::string& name)
{ {
clrsync::core::palette new_palette = m_current_palette; clrsync::core::palette new_palette = m_palette_manager.load_palette_from_file(std::string(CLRSYNC_DATADIR) + "/palettes/cursed.toml");
new_palette.set_name(name); new_palette.set_name(name);
auto colors = m_current_palette.colors();
for (auto& pair : colors) {
new_palette.set_color(pair.first, pair.second);
}
auto dir = clrsync::core::config::instance().palettes_path(); auto dir = clrsync::core::config::instance().palettes_path();
m_palette_manager.save_palette_to_file(new_palette, dir); m_palette_manager.save_palette_to_file(new_palette, dir);

View File

@@ -33,6 +33,8 @@ template_editor::template_editor() : m_template_name("new_template")
void template_editor::apply_current_palette(const clrsync::core::palette &pal) void template_editor::apply_current_palette(const clrsync::core::palette &pal)
{ {
auto colors = pal.colors(); auto colors = pal.colors();
if (colors.empty())
return;
auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t { auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t {
auto it = colors.find(key); auto it = colors.find(key);
if (it == colors.end() && !fallback.empty()) if (it == colors.end() && !fallback.empty())