Move the change detector to a separate module.

master
Zed A. Shaw 3 weeks ago
parent 098ffcf54d
commit 522ec59c0e
  1. 1
      meson.build
  2. 20
      sample/about-bezos.md
  3. 31
      src/changes.cpp
  4. 15
      src/changes.hpp
  5. 40
      src/main.cpp

@ -96,6 +96,7 @@ sources = [
'src/parser.cpp', 'src/parser.cpp',
'src/backend.cpp', 'src/backend.cpp',
'src/control.cpp', 'src/control.cpp',
'src/changes.cpp',
] ]
tests = [ tests = [

@ -26,6 +26,26 @@ PowerPoint
} }
Can We Blame Him? 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 # I Hate PowerPoint Too
* So I made an alternative. * So I made an alternative.
* Markdown Format * Markdown Format

@ -0,0 +1,31 @@
#include "changes.hpp"
#include <fmt/core.h>
#include <SFML/System/Time.hpp>
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;
}

@ -0,0 +1,15 @@
#pragma once
#include <SFML/System/Clock.hpp>
#include <filesystem>
#include <chrono>
#include <unordered_map>
struct ChangeDetector {
std::string input_md;
sf::Clock timer{};
std::unordered_map<std::string, std::filesystem::file_time_type> $watching;
void watch(const std::string& name);
bool changed();
};

@ -6,13 +6,9 @@
#include "slides_ui.hpp" #include "slides_ui.hpp"
#include "control_ui.hpp" #include "control_ui.hpp"
#include "parser.hpp" #include "parser.hpp"
#include <filesystem>
#include <chrono>
#include <SFML/System/Clock.hpp>
#include <memory> #include <memory>
#include <unistd.h> #include <unistd.h>
#include "changes.hpp"
using namespace std::chrono_literals;
struct Options { struct Options {
std::string deck_file{}; std::string deck_file{};
@ -41,32 +37,6 @@ std::shared_ptr<SlidesUI> 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() { void print_usage() {
fmt::println("USAGE: bezos [-p PORT] [-h] -d deck.md"); 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.init();
control_ui.full_screen(false); control_ui.full_screen(false);
ChangeDetector slides_reloader{options.deck_file}; ChangeDetector reloader;
ChangeDetector layout_reloader{slides->$deck->config["layouts"]}; reloader.watch(options.deck_file);
reloader.watch(slides->$deck->config["layouts"]);
while(controller.isOpen()) { while(controller.isOpen()) {
while(const auto event = presenter.pollEvent()) { while(const auto event = presenter.pollEvent()) {
if(event) slides->handle_events(presenter, *event); if(event) slides->handle_events(presenter, *event);
} }
@ -157,7 +127,7 @@ int main(int argc, char *argv[]) {
presenter.display(); presenter.display();
controller.display(); controller.display();
if(slides_reloader.changed() || layout_reloader.changed()) { if(reloader.changed()) {
// save the current slide // save the current slide
auto current_slide = slides->$deck->current; auto current_slide = slides->$deck->current;
// load the new one // load the new one

Loading…
Cancel
Save