From 522ec59c0eda51bb2bd2d653108b3bbd39a124ee Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 3 May 2026 23:10:07 -0400 Subject: [PATCH] Move the change detector to a separate module. --- meson.build | 1 + sample/about-bezos.md | 20 ++++++++++++++++++++ src/changes.cpp | 31 +++++++++++++++++++++++++++++++ src/changes.hpp | 15 +++++++++++++++ src/main.cpp | 40 +++++----------------------------------- 5 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 src/changes.cpp create mode 100644 src/changes.hpp diff --git a/meson.build b/meson.build index a54ab49..97e3b08 100644 --- a/meson.build +++ b/meson.build @@ -96,6 +96,7 @@ sources = [ 'src/parser.cpp', 'src/backend.cpp', 'src/control.cpp', + 'src/changes.cpp', ] tests = [ diff --git a/sample/about-bezos.md b/sample/about-bezos.md index 354bac5..7426712 100644 --- a/sample/about-bezos.md +++ b/sample/about-bezos.md @@ -26,6 +26,26 @@ PowerPoint } Can We Blame Him? --- +{ + "shader": "flame", + "bg_color": [80, 0, 0, 255], + "layout": "no_title", + "font_size": 200 +} +I Made a +PowerPoint +Killer +--- +{ + "shader": "flame", + "bg_color": [80, 0, 0, 255], + "layout": "no_title", + "font_size": 300, + "title_size": 300 +} +In +C++ +--- # I Hate PowerPoint Too * So I made an alternative. * Markdown Format diff --git a/src/changes.cpp b/src/changes.cpp new file mode 100644 index 0000000..e5334d5 --- /dev/null +++ b/src/changes.cpp @@ -0,0 +1,31 @@ +#include "changes.hpp" +#include +#include + +using namespace std::chrono_literals; + +void ChangeDetector::watch(const std::string& name) { + auto last_mod_time = std::filesystem::last_write_time(name); + $watching.try_emplace(name, last_mod_time); +} + +bool ChangeDetector::changed() { + if(timer.getElapsedTime().toDuration() > 500ms) { + for(auto& [fname, last_mod_time] : $watching) { + try { + auto mod_time = std::filesystem::last_write_time(fname); + + if(last_mod_time < mod_time) { + last_mod_time = mod_time; + return true; + } + } catch(const std::filesystem::filesystem_error& err) { + fmt::println("failed to open {}: {}", err.path1().string(), err.what()); + timer.restart(); + return false; + } + } + } + + return false; +} diff --git a/src/changes.hpp b/src/changes.hpp new file mode 100644 index 0000000..701d9d9 --- /dev/null +++ b/src/changes.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include + +struct ChangeDetector { + std::string input_md; + sf::Clock timer{}; + std::unordered_map $watching; + + void watch(const std::string& name); + bool changed(); +}; diff --git a/src/main.cpp b/src/main.cpp index 49ba269..b00fc75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,13 +6,9 @@ #include "slides_ui.hpp" #include "control_ui.hpp" #include "parser.hpp" -#include -#include -#include #include #include - -using namespace std::chrono_literals; +#include "changes.hpp" struct Options { std::string deck_file{}; @@ -41,32 +37,6 @@ std::shared_ptr load_slides(const std::string& input_md, std::shared_p } } -struct ChangeDetector { - std::string input_md; - sf::Clock timer{}; - std::filesystem::file_time_type last_mod_time = std::filesystem::last_write_time(input_md); - - bool changed() { - if(timer.getElapsedTime().toDuration() > 500ms) { - try { - auto mod_time = std::filesystem::last_write_time(input_md); - - if(last_mod_time < mod_time) { - last_mod_time = mod_time; - return true; - } else { - return false; - } - } catch(const std::filesystem::filesystem_error& err) { - fmt::println("failed to open {}: {}", err.path1().string(), err.what()); - timer.restart(); - return false; - } - } - - return false; - } -}; void print_usage() { fmt::println("USAGE: bezos [-p PORT] [-h] -d deck.md"); @@ -140,11 +110,11 @@ int main(int argc, char *argv[]) { control_ui.init(); control_ui.full_screen(false); - ChangeDetector slides_reloader{options.deck_file}; - ChangeDetector layout_reloader{slides->$deck->config["layouts"]}; + ChangeDetector reloader; + reloader.watch(options.deck_file); + reloader.watch(slides->$deck->config["layouts"]); while(controller.isOpen()) { - while(const auto event = presenter.pollEvent()) { if(event) slides->handle_events(presenter, *event); } @@ -157,7 +127,7 @@ int main(int argc, char *argv[]) { presenter.display(); controller.display(); - if(slides_reloader.changed() || layout_reloader.changed()) { + if(reloader.changed()) { // save the current slide auto current_slide = slides->$deck->current; // load the new one