Skip to content

Commit

Permalink
global config
Browse files Browse the repository at this point in the history
  • Loading branch information
f0e committed Apr 16, 2023
1 parent f53709f commit 5958d07
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
52 changes: 42 additions & 10 deletions blur/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,10 @@ std::filesystem::path c_config::get_config_filename(const std::filesystem::path&
return video_folder / filename;
}

s_blur_settings c_config::parse(const std::filesystem::path& config_filepath, bool& first_time) {
s_blur_settings c_config::parse(const std::filesystem::path& config_filepath) {
auto read_config = [&]() {
std::map<std::string, std::string> config = {};

// check if the config file exists, if not, write the default values
if (!std::filesystem::exists(config_filepath)) {
first_time = true;
create(config_filepath);
}

// retrieve all of the variables in the config file
std::ifstream input(config_filepath);
std::string line;
Expand Down Expand Up @@ -198,7 +192,45 @@ s_blur_settings c_config::parse(const std::filesystem::path& config_filepath, bo
return settings;
}

s_blur_settings c_config::parse_folder(const std::filesystem::path& video_folder, std::filesystem::path& config_filepath, bool& first_time) {
config_filepath = get_config_filename(video_folder);
return parse(config_filepath, first_time);
s_blur_settings c_config::get_config(const std::filesystem::path& config_filepath, bool use_global) {
bool local_cfg_exists = std::filesystem::exists(config_filepath);

auto global_cfg_path = blur.path / filename;
bool global_cfg_exists = std::filesystem::exists(global_cfg_path);

std::filesystem::path cfg_path;
if (use_global && !local_cfg_exists && global_cfg_exists) {
cfg_path = global_cfg_path;

if (blur.using_ui || blur.verbose)
console.print("using global config");
}
else {
// check if the config file exists, if not, write the default values
if (!local_cfg_exists) {
config.create(config_filepath);

if (blur.using_ui) {
// check if the config exists
console.print_blank_line();
console.print(fmt::format("configuration file not found, default config generated at {}", config_filepath.string()));
console.print_blank_line();
console.print("continue render? (y/n)");
console.print_blank_line();

char choice = console.get_char();
if (tolower(choice) != 'y') {
throw std::exception("stopping render");
}
}
else {
if (blur.verbose)
console.print(fmt::format("configuration file not found, default config generated at {}", config_filepath.string()));
}
}

cfg_path = config_filepath;
}

return parse(cfg_path);
}
10 changes: 5 additions & 5 deletions blur/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class c_config {
private:
const std::string filename = ".blur-config.cfg";

private:
std::filesystem::path get_config_filename(const std::filesystem::path& video_folder);

public:
void create(const std::filesystem::path& filepath, s_blur_settings current_settings = s_blur_settings());

s_blur_settings parse(const std::filesystem::path& config_filepath, bool& first_time);
s_blur_settings parse_folder(const std::filesystem::path& video_folder, std::filesystem::path& config_filepath, bool& first_time);
std::filesystem::path get_config_filename(const std::filesystem::path& video_folder);

s_blur_settings parse(const std::filesystem::path& config_filepath);

s_blur_settings get_config(const std::filesystem::path& config_filepath, bool use_global);
};

inline c_config config;
39 changes: 5 additions & 34 deletions blur/rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,40 +129,11 @@ c_render::c_render(const std::filesystem::path& input_path, std::optional<std::f
console.print(fmt::format("opening {} for processing", this->video_path.filename().string()));
}

if (config_path.has_value()) {
bool first_time_config = false;
this->settings = config.parse(output_path.value(), first_time_config);

if (first_time_config) {
if (blur.verbose)
console.print(fmt::format("configuration file not found, default config generated at {}", config_path->string()));
}
}
else {
// parse config file (do it now, not when rendering. nice for batch rendering the same file with different settings)
bool first_time_config = false;
std::filesystem::path config_filepath;
this->settings = config.parse_folder(this->video_folder, config_filepath, first_time_config);

if (blur.using_ui) {
// check if the config exists
if (first_time_config) {
console.print_blank_line();
console.print(fmt::format("configuration file not found, default config generated at {}", config_filepath.string()));
console.print_blank_line();
console.print("continue render? (y/n)");
console.print_blank_line();

char choice = console.get_char();
if (tolower(choice) != 'y') {
throw std::exception("stopping render");
}

// get settings again in case the user modified them
this->settings = config.parse_folder(this->video_folder, config_filepath, first_time_config);
}
}
}
// parse config file (do it now, not when rendering. nice for batch rendering the same file with different settings)
if (config_path.has_value())
this->settings = config.get_config(output_path.value(), false); // specified config path, don't use global
else
this->settings = config.get_config(config.get_config_filename(video_folder), true);

if (output_path.has_value())
this->output_path = output_path.value();
Expand Down

0 comments on commit 5958d07

Please sign in to comment.