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,6 +5,7 @@
*/
#include "TodoMd.h"
#include "TaskItem.h"
namespace mirai {
@ -27,13 +28,8 @@ namespace mirai {
if (line.substr(0, 3) == "## ") {
currentDate = line.substr(3);
} else if (line.substr(0, 5) == "- [ ]" || line.substr(0, 5) == "- [X]") {
std::string text = line.substr(5);
trim(text);
TaskItem taskItem = {
.text = text,
.state = line.substr(0, 5) == "- [X]" ? DONE : TODO,
.date = currentDate
};
TaskItem taskItem = StringToTask(line, currentDate);
taskItem.setDate(currentDate);
taskItems.push_back(taskItem);
}
}
@ -46,6 +42,22 @@ namespace mirai {
return tasks;
}
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata) {
Tags tags;
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();
}
return tags;
}
void TodoMdFormat::writeFile(TasksFile& tasks) {
std::ofstream file(tasks.getPath());
if (!file.is_open()) {
@ -59,12 +71,7 @@ namespace mirai {
currentDate = task->date;
file << "\n## " << (task->date != "" ? task->date : "No date") << "\n\n";
}
if (task->getState() == TODO) {
file << "- [ ] ";
} else if (task->getState() == DONE) {
file << "- [X] ";
}
file << task->getText() << '\n';
file << TaskToString(*task) << '\n';
}
file.close();
}
@ -74,5 +81,49 @@ namespace mirai {
return "";
return (field + " ");
}
TaskItem TodoMdFormat::StringToTask(const std::string& str, const std::string& date) {
std::smatch matches;
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?");
std::regex_match(str, matches, regex);
std::cout << "line " << str << std::endl;
std::cout << "M 0 " << matches[0] << std::endl;
std::cout << "M 1 " << matches[1] << std::endl;
std::cout << "M 2 " << matches[2] << std::endl;
std::cout << "M 3 " << matches[3] << std::endl;
std::cout << "M 4 " << matches[4] << std::endl;
std::cout << "M 5 " << matches[5] << std::endl;
std::cout << "M 6 " << matches[6] << std::endl;
std::cout << "M 7 " << matches[7] << std::endl;
std::string text = matches[5];
trim(text);
TaskItem taskItem = {
.text = text,
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
.date = date,
.startTime = matches[3],
.endTime = matches[4],
.tags = extractTagsFromMetadata(matches[7])
};
return taskItem;
}
std::string TodoMdFormat::TaskToString(const TaskItem& task) {
std::string str = task.getText();
if (task.getTags().size() > 0) {
str += " --";
for (const std::string& tag : task.getTags()) {
str += " " + tag;
}
}
if (task.getStartTime() != "" && task.getEndTime() != "") {
str = task.getStartTime() + "-" + task.getEndTime() + " > " + str;
}
str = (task.getState() == DONE ? "- [X] " : "- [ ] ") + str;
return str;
}
}