mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 01:33:19 +00:00
Support multiple files
This commit is contained in:
parent
f8f49233dc
commit
689eea07a7
22 changed files with 528 additions and 131 deletions
|
@ -6,12 +6,16 @@
|
|||
|
||||
#include "TodoMd.h"
|
||||
#include "TaskItem.h"
|
||||
#include "cpp-utils/vector.h"
|
||||
#include "core/TasksFile.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
TasksFile TodoMdFormat::readFile(const std::string &path)
|
||||
std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
||||
{
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
|
@ -27,18 +31,22 @@ TasksFile TodoMdFormat::readFile(const std::string &path)
|
|||
std::string name = line.substr(2);
|
||||
std::string currentDate = "";
|
||||
|
||||
auto tasksFile = std::make_unique<TasksFile>(
|
||||
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
|
||||
);
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
if (line.substr(0, 3) == "## ") {
|
||||
currentDate = line.substr(3);
|
||||
} else if (line.substr(0, 5) == "- [ ]" || line.substr(0, 5) == "- [X]") {
|
||||
TaskItem taskItem = StringToTask(line, currentDate);
|
||||
taskItem.setDate(currentDate);
|
||||
taskItems.push_back(taskItem);
|
||||
TaskItemData taskItemData = StringToTask(line, currentDate);
|
||||
taskItemData.date = currentDate;
|
||||
tasksFile->addTask(taskItemData);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
TasksFile tasks({.name = name, .path = path, .tasks = taskItems});
|
||||
return tasks;
|
||||
tasksFile->setDirty(false);
|
||||
return tasksFile;
|
||||
}
|
||||
|
||||
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
|
||||
|
@ -67,9 +75,9 @@ void TodoMdFormat::writeFile(TasksFile &tasks)
|
|||
|
||||
file << "# " << tasks.getName() << "\n";
|
||||
for (const auto &task : tasks.getTasks()) {
|
||||
if (currentDate != task->date) {
|
||||
currentDate = task->date;
|
||||
file << "\n## " << (task->date != "" ? task->date : "No date") << "\n\n";
|
||||
if (currentDate != task->getDate()) {
|
||||
currentDate = task->getDate();
|
||||
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
|
||||
}
|
||||
file << TaskToString(*task) << '\n';
|
||||
}
|
||||
|
@ -78,16 +86,17 @@ void TodoMdFormat::writeFile(TasksFile &tasks)
|
|||
|
||||
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
|
||||
{
|
||||
if (field.length() == 0)
|
||||
if (field.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return (field + " ");
|
||||
}
|
||||
|
||||
TaskItem TodoMdFormat::StringToTask(const std::string &str, const std::string &date)
|
||||
TaskItemData 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 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;*/
|
||||
|
@ -102,12 +111,14 @@ TaskItem TodoMdFormat::StringToTask(const std::string &str, const std::string &d
|
|||
|
||||
std::string text = stringUtils::trim(matches[5]);
|
||||
|
||||
TaskItem taskItem = {.text = text,
|
||||
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
|
||||
.date = date,
|
||||
.startTime = matches[3],
|
||||
.endTime = matches[4],
|
||||
.tags = extractTagsFromMetadata(matches[7])};
|
||||
TaskItemData 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue