mirror of
https://github.com/obsqrbtz/clrsync.git
synced 2026-04-09 04:29:04 +03:00
refactor: error handling with err objects
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "core/config/config.hpp"
|
||||
#include "core/io/toml_file.hpp"
|
||||
#include "core/utils.hpp"
|
||||
#include "core/error.hpp"
|
||||
|
||||
#include "color_scheme_editor.hpp"
|
||||
#include "gui/font_loader.hpp"
|
||||
@@ -21,13 +22,10 @@ int main(int, char**)
|
||||
auto config_path = clrsync::core::get_default_config_path();
|
||||
auto conf = std::make_unique<clrsync::core::io::toml_file>(config_path);
|
||||
|
||||
try
|
||||
auto init_result = clrsync::core::config::instance().initialize(std::move(conf));
|
||||
if (!init_result)
|
||||
{
|
||||
clrsync::core::config::instance().initialize(std::move(conf));
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << "Fatal error: " << e.what() << std::endl;
|
||||
std::cerr << "Fatal error: " << init_result.error().description() << std::endl;
|
||||
std::cerr << "Hint: Set CLRSYNC_CONFIG_PATH environment variable or ensure config exists at: " << config_path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -10,10 +10,12 @@ palette_controller::palette_controller()
|
||||
|
||||
if (m_palettes.empty())
|
||||
return;
|
||||
|
||||
try {
|
||||
m_current_palette = m_palettes[clrsync::core::config::instance().default_theme()];
|
||||
} catch (...) {
|
||||
|
||||
auto default_theme = clrsync::core::config::instance().default_theme();
|
||||
auto it = m_palettes.find(default_theme);
|
||||
if (it != m_palettes.end()) {
|
||||
m_current_palette = it->second;
|
||||
} else {
|
||||
m_current_palette = m_palettes.begin()->second;
|
||||
}
|
||||
}
|
||||
@@ -54,7 +56,7 @@ void palette_controller::delete_current_palette()
|
||||
void palette_controller::apply_current_theme() const
|
||||
{
|
||||
clrsync::core::theme_renderer<clrsync::core::io::toml_file> theme_renderer;
|
||||
theme_renderer.apply_theme(m_current_palette.name());
|
||||
(void)theme_renderer.apply_theme(m_current_palette.name());
|
||||
}
|
||||
|
||||
void palette_controller::set_color(const std::string& key, const clrsync::core::color& color)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "settings_window.hpp"
|
||||
#include "core/config/config.hpp"
|
||||
#include "core/error.hpp"
|
||||
#include "gui/font_loader.hpp"
|
||||
#include "imgui.h"
|
||||
#include <cstring>
|
||||
@@ -119,83 +120,85 @@ void settings_window::render()
|
||||
|
||||
void settings_window::load_settings()
|
||||
{
|
||||
try
|
||||
{
|
||||
auto& cfg = clrsync::core::config::instance();
|
||||
|
||||
std::string default_theme = cfg.default_theme();
|
||||
strncpy(m_default_theme, default_theme.c_str(), sizeof(m_default_theme) - 1);
|
||||
m_default_theme[sizeof(m_default_theme) - 1] = '\0';
|
||||
|
||||
std::string palettes_path = cfg.palettes_path();
|
||||
strncpy(m_palettes_path, palettes_path.c_str(), sizeof(m_palettes_path) - 1);
|
||||
m_palettes_path[sizeof(m_palettes_path) - 1] = '\0';
|
||||
|
||||
std::string font = cfg.font();
|
||||
strncpy(m_font, font.c_str(), sizeof(m_font) - 1);
|
||||
m_font[sizeof(m_font) - 1] = '\0';
|
||||
|
||||
m_font_size = cfg.font_size();
|
||||
|
||||
m_error_message = "";
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
m_error_message = std::string("Failed to load settings: ") + e.what();
|
||||
|
||||
// Set defaults on error
|
||||
strncpy(m_default_theme, "dark", sizeof(m_default_theme));
|
||||
strncpy(m_palettes_path, "~/.config/clrsync/palettes", sizeof(m_palettes_path));
|
||||
strncpy(m_font, "JetBrains Mono Nerd Font", sizeof(m_font));
|
||||
m_font_size = 14;
|
||||
}
|
||||
auto& cfg = clrsync::core::config::instance();
|
||||
|
||||
std::string default_theme = cfg.default_theme();
|
||||
strncpy(m_default_theme, default_theme.c_str(), sizeof(m_default_theme) - 1);
|
||||
m_default_theme[sizeof(m_default_theme) - 1] = '\0';
|
||||
|
||||
std::string palettes_path = cfg.palettes_path();
|
||||
strncpy(m_palettes_path, palettes_path.c_str(), sizeof(m_palettes_path) - 1);
|
||||
m_palettes_path[sizeof(m_palettes_path) - 1] = '\0';
|
||||
|
||||
std::string font = cfg.font();
|
||||
strncpy(m_font, font.c_str(), sizeof(m_font) - 1);
|
||||
m_font[sizeof(m_font) - 1] = '\0';
|
||||
|
||||
m_font_size = cfg.font_size();
|
||||
|
||||
m_error_message = "";
|
||||
}
|
||||
|
||||
void settings_window::apply_settings()
|
||||
{
|
||||
try
|
||||
auto& cfg = clrsync::core::config::instance();
|
||||
|
||||
if (strlen(m_default_theme) == 0)
|
||||
{
|
||||
auto& cfg = clrsync::core::config::instance();
|
||||
|
||||
if (strlen(m_default_theme) == 0)
|
||||
{
|
||||
m_error_message = "Default theme cannot be empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen(m_palettes_path) == 0)
|
||||
{
|
||||
m_error_message = "Palettes path cannot be empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen(m_font) == 0)
|
||||
{
|
||||
m_error_message = "Font cannot be empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_font_size < 8 || m_font_size > 48)
|
||||
{
|
||||
m_error_message = "Font size must be between 8 and 48";
|
||||
return;
|
||||
}
|
||||
|
||||
cfg.set_default_theme(m_default_theme);
|
||||
cfg.set_palettes_path(m_palettes_path);
|
||||
cfg.set_font(m_font);
|
||||
cfg.set_font_size(m_font_size);
|
||||
|
||||
|
||||
font_loader fn_loader;
|
||||
auto font = fn_loader.load_font(m_font, m_font_size);
|
||||
if (font)
|
||||
ImGui::GetIO().FontDefault = font;
|
||||
|
||||
m_error_message = "";
|
||||
m_error_message = "Default theme cannot be empty";
|
||||
return;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
|
||||
if (strlen(m_palettes_path) == 0)
|
||||
{
|
||||
m_error_message = std::string("Failed to apply settings: ") + e.what();
|
||||
m_error_message = "Palettes path cannot be empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen(m_font) == 0)
|
||||
{
|
||||
m_error_message = "Font cannot be empty";
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_font_size < 8 || m_font_size > 48)
|
||||
{
|
||||
m_error_message = "Font size must be between 8 and 48";
|
||||
return;
|
||||
}
|
||||
|
||||
auto result1 = cfg.set_default_theme(m_default_theme);
|
||||
if (!result1)
|
||||
{
|
||||
m_error_message = "Failed to set default theme: " + result1.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
auto result2 = cfg.set_palettes_path(m_palettes_path);
|
||||
if (!result2)
|
||||
{
|
||||
m_error_message = "Failed to set palettes path: " + result2.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
auto result3 = cfg.set_font(m_font);
|
||||
if (!result3)
|
||||
{
|
||||
m_error_message = "Failed to set font: " + result3.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
auto result4 = cfg.set_font_size(m_font_size);
|
||||
if (!result4)
|
||||
{
|
||||
m_error_message = "Failed to set font size: " + result4.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
font_loader fn_loader;
|
||||
auto font = fn_loader.load_font(m_font, m_font_size);
|
||||
if (font)
|
||||
ImGui::GetIO().FontDefault = font;
|
||||
|
||||
m_error_message = "";
|
||||
}
|
||||
@@ -11,7 +11,7 @@ void template_controller::set_template_enabled(const std::string& key, bool enab
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end()) {
|
||||
it->second.set_enabled(enabled);
|
||||
clrsync::core::config::instance().update_template(key, it->second);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ void template_controller::set_template_output_path(const std::string& key, const
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end()) {
|
||||
it->second.set_output_path(path);
|
||||
clrsync::core::config::instance().update_template(key, it->second);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ void template_controller::set_template_reload_command(const std::string& key, co
|
||||
auto it = m_templates.find(key);
|
||||
if (it != m_templates.end()) {
|
||||
it->second.set_reload_command(cmd);
|
||||
clrsync::core::config::instance().update_template(key, it->second);
|
||||
(void)clrsync::core::config::instance().update_template(key, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -343,59 +343,62 @@ void template_editor::save_template()
|
||||
|
||||
m_validation_error = "";
|
||||
|
||||
try
|
||||
auto &cfg = clrsync::core::config::instance();
|
||||
std::string palettes_path = cfg.palettes_path();
|
||||
std::filesystem::path templates_dir =
|
||||
std::filesystem::path(palettes_path).parent_path() / "templates";
|
||||
|
||||
if (!std::filesystem::exists(templates_dir))
|
||||
{
|
||||
auto &cfg = clrsync::core::config::instance();
|
||||
std::string palettes_path = cfg.palettes_path();
|
||||
std::filesystem::path templates_dir =
|
||||
std::filesystem::path(palettes_path).parent_path() / "templates";
|
||||
std::filesystem::create_directories(templates_dir);
|
||||
}
|
||||
|
||||
if (!std::filesystem::exists(templates_dir))
|
||||
std::filesystem::path template_file;
|
||||
if (m_is_editing_existing)
|
||||
{
|
||||
auto existing_template_result = cfg.template_by_name(trimmed_name);
|
||||
if (!existing_template_result)
|
||||
{
|
||||
std::filesystem::create_directories(templates_dir);
|
||||
}
|
||||
|
||||
std::filesystem::path template_file;
|
||||
if (m_is_editing_existing)
|
||||
{
|
||||
const auto &existing_template = cfg.template_by_name(trimmed_name);
|
||||
template_file = existing_template.template_path();
|
||||
}
|
||||
else
|
||||
{
|
||||
template_file = templates_dir / trimmed_name;
|
||||
}
|
||||
|
||||
std::string template_content = m_editor.GetText();
|
||||
|
||||
std::ofstream out(template_file);
|
||||
if (!out.is_open())
|
||||
{
|
||||
m_validation_error = "Failed to write template file";
|
||||
m_validation_error = "Template not found: " + existing_template_result.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
out << template_content;
|
||||
out.close();
|
||||
|
||||
clrsync::core::theme_template tmpl(trimmed_name, template_file.string(), trimmed_path);
|
||||
tmpl.set_reload_command(m_reload_command);
|
||||
tmpl.set_enabled(m_enabled);
|
||||
|
||||
cfg.update_template(trimmed_name, tmpl);
|
||||
|
||||
m_template_name = trimmed_name;
|
||||
m_output_path = trimmed_path;
|
||||
m_is_editing_existing = true;
|
||||
m_saved_content = m_editor.GetText();
|
||||
m_has_unsaved_changes = false;
|
||||
|
||||
refresh_templates();
|
||||
template_file = existing_template_result.value()->template_path();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
else
|
||||
{
|
||||
m_validation_error = std::string("Error saving template: ") + e.what();
|
||||
template_file = templates_dir / trimmed_name;
|
||||
}
|
||||
|
||||
std::string template_content = m_editor.GetText();
|
||||
|
||||
std::ofstream out(template_file);
|
||||
if (!out.is_open())
|
||||
{
|
||||
m_validation_error = "Failed to write template file";
|
||||
return;
|
||||
}
|
||||
|
||||
out << template_content;
|
||||
out.close();
|
||||
|
||||
clrsync::core::theme_template tmpl(trimmed_name, template_file.string(), trimmed_path);
|
||||
tmpl.set_reload_command(m_reload_command);
|
||||
tmpl.set_enabled(m_enabled);
|
||||
|
||||
auto result = cfg.update_template(trimmed_name, tmpl);
|
||||
if (!result)
|
||||
{
|
||||
m_validation_error = "Error saving template: " + result.error().description();
|
||||
return;
|
||||
}
|
||||
|
||||
m_template_name = trimmed_name;
|
||||
m_output_path = trimmed_path;
|
||||
m_is_editing_existing = true;
|
||||
m_saved_content = m_editor.GetText();
|
||||
m_has_unsaved_changes = false;
|
||||
|
||||
refresh_templates();
|
||||
}
|
||||
|
||||
void template_editor::load_template(const std::string &name)
|
||||
@@ -413,27 +416,24 @@ void template_editor::load_template(const std::string &name)
|
||||
m_is_editing_existing = true;
|
||||
m_validation_error = "";
|
||||
|
||||
try
|
||||
std::ifstream in(tmpl.template_path());
|
||||
if (in.is_open())
|
||||
{
|
||||
std::ifstream in(tmpl.template_path());
|
||||
if (in.is_open())
|
||||
std::string content;
|
||||
std::string line;
|
||||
while (std::getline(in, line))
|
||||
{
|
||||
std::string content;
|
||||
std::string line;
|
||||
while (std::getline(in, line))
|
||||
{
|
||||
content += line + "\n";
|
||||
}
|
||||
in.close();
|
||||
|
||||
m_editor.SetText(content);
|
||||
m_saved_content = content;
|
||||
m_has_unsaved_changes = false;
|
||||
content += line + "\n";
|
||||
}
|
||||
in.close();
|
||||
|
||||
m_editor.SetText(content);
|
||||
m_saved_content = content;
|
||||
m_has_unsaved_changes = false;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
else
|
||||
{
|
||||
m_validation_error = std::string("Error loading template: ") + e.what();
|
||||
m_validation_error = "Error loading template: Failed to open file";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user