parent
098ffcf54d
commit
522ec59c0e
@ -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(); |
||||
}; |
||||
Loading…
Reference in new issue