Add 'Time' and 'Tags' as proper task's properties, also add raw format handling

This commit is contained in:
Vyn 2024-04-11 11:42:13 +02:00
parent 3e7d8b4b70
commit bae67e6851
15 changed files with 142 additions and 49 deletions

View file

@ -5,13 +5,13 @@
*/
#include "TasksFile.h"
#include "using.h"
#include "TaskItem.h"
#include <memory>
namespace mirai {
TasksFile::TasksFile(TasksFileConstructor params) : name(params.name), path(params.path) {
for (const auto& task : params.tasks) {
processTaskMetaData(task);
tasks.push_back(std::make_unique<TaskItem>(task));
}
sortByDate();
@ -20,7 +20,11 @@ namespace mirai {
const std::string& TasksFile::getName() const { return name; }
const std::string& TasksFile::getPath() const { return path; }
TasksPtrs& TasksFile::getTasks() { return tasks; }
Tags& TasksFile::getTags() { return tags; }
void TasksFile::addTask(TaskItem taskItem) {
tasks.push_back(std::make_unique<TaskItem>(taskItem));
sortByDate();
}
void TasksFile::addTask(std::string text, std::string date) {
auto newTask = std::make_unique<TaskItem>(TaskItem{
@ -38,26 +42,6 @@ namespace mirai {
}));
}
void TasksFile::processTaskMetaData(const TaskItem& task) {
const std::string metaDataSeparator = " -- ";
auto separatorIndex = task.text.find(metaDataSeparator);
if (separatorIndex == std::string::npos) {
return;
}
auto metaData = task.text.substr(separatorIndex + metaDataSeparator.length());
std::regex regex("(#[a-zA-Z0-1]+)");
std::smatch matches;
while (std::regex_search(metaData, matches, regex))
{
if (std::find(tags.begin(), tags.end(), matches[0]) == tags.end()) {
tags.push_back(matches[0]);
}
metaData = matches.suffix();
}
}
void TasksFile::sortByDate() {
std::sort(tasks.begin(), tasks.end(), [] (const std::unique_ptr<TaskItem>& t1, const std::unique_ptr<TaskItem>& t2) {
if (t1->hasDate() && !t2->hasDate()) {