feat: add action history management

This commit is contained in:
BlackAngelTV committed 2026-08-24 21:13:50 +02:00
1 parent 829e30750b
commit 6dd3bc7b8f
2 files changed
+133

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
#include "HistoryManager.h"
#include <filesystem>
#include <cstring>
#include <iomanip>
namespace fs = std::filesystem;
HistoryManager::HistoryManager(const std::string& historyFile)
: m_historyFile(expandPath(historyFile)) {
load();
}
HistoryManager::~HistoryManager() {
save();
}
std::string HistoryManager::expandPath(const std::string& path) {
if (path.empty()) return path;
std::string result = path;
if (result[0] == '~') {
const char* home = getenv("HOME");
if (home) {
result = std::string(home) + result.substr(1);
}
}
// Ensure directory exists
auto dir = fs::path(result).parent_path();
if (!fs::exists(dir)) {
fs::create_directories(dir);
}
return result;
}
std::string HistoryManager::getCurrentTimestamp() {
auto now = std::time(nullptr);
auto* tm = std::localtime(&now);
char buffer[64];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm);
return std::string(buffer);
}
void HistoryManager::record(const HistoryEntry& entry) {
HistoryEntry e = entry;
if (e.timestamp.empty()) {
e.timestamp = getCurrentTimestamp();
}
m_history.push_back(e);
save();
}
const std::vector<HistoryEntry>& HistoryManager::getHistory() const {
return m_history;
}
void HistoryManager::clear() {
m_history.clear();
save();
}
void HistoryManager::save() {
std::ofstream file(m_historyFile);
if (!file.is_open()) return;
file << "timestamp,action,package,plugin,success\n";
for (const auto& entry : m_history) {
file << entry.timestamp << ","
<< entry.action << ","
<< entry.package << ","
<< entry.plugin << ","
<< (entry.success ? "1" : "0") << "\n";
}
}
void HistoryManager::load() {
std::ifstream file(m_historyFile);
if (!file.is_open()) return;
std::string line;
std::getline(file, line); // skip header
while (std::getline(file, line)) {
std::istringstream ss(line);
HistoryEntry entry;
std::string successStr;
std::getline(ss, entry.timestamp, ',');
std::getline(ss, entry.action, ',');
std::getline(ss, entry.package, ',');
std::getline(ss, entry.plugin, ',');
std::getline(ss, successStr, ',');
entry.success = (successStr == "1");
m_history.push_back(entry);
}
}