mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 01:33:19 +00:00
Improve startup time
This commit is contained in:
parent
d90bfbc483
commit
597ea0ac2d
10 changed files with 82 additions and 23 deletions
|
@ -7,16 +7,19 @@
|
|||
#include "TodoMd.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TasksFile.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
||||
{
|
||||
cpputils::debug::Timer readMdFormatDuration;
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("todo.txt file not found");
|
||||
|
@ -35,17 +38,26 @@ std::unique_ptr<TasksFile> TodoMdFormat::readFile(const std::string &path)
|
|||
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
|
||||
);
|
||||
|
||||
cpputils::debug::Timer stringToTaskDuration;
|
||||
stringToTaskDuration.reset();
|
||||
cpputils::debug::Timer gelinesDuration;
|
||||
while (std::getline(file, line)) {
|
||||
if (line.substr(0, 3) == "## ") {
|
||||
if (std::string_view{line.data(), 3} == "## ") {
|
||||
currentDate = line.substr(3);
|
||||
} else if (line.substr(0, 5) == "- [ ]" || line.substr(0, 5) == "- [X]") {
|
||||
TaskItemData taskItemData = StringToTask(line, currentDate);
|
||||
} else if (std::string_view{line.data(), 5} == "- [ ]" || std::string_view{line.data(), 5} == "- [X]") {
|
||||
stringToTaskDuration.start();
|
||||
TaskItemData taskItemData = stringToTask(line, currentDate);
|
||||
stringToTaskDuration.stop();
|
||||
taskItemData.date = currentDate;
|
||||
tasksFile->addTask(taskItemData);
|
||||
}
|
||||
}
|
||||
|
||||
gelinesDuration.printTimeElapsed("getlinesDuration");
|
||||
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
|
||||
file.close();
|
||||
tasksFile->setDirty(false);
|
||||
readMdFormatDuration.printTimeElapsed("Reading MD File duration of " + path);
|
||||
return tasksFile;
|
||||
}
|
||||
|
||||
|
@ -79,7 +91,7 @@ void TodoMdFormat::writeFile(TasksFile &tasks)
|
|||
currentDate = task->getDate();
|
||||
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
|
||||
}
|
||||
file << TaskToString(*task) << '\n';
|
||||
file << taskToString(*task) << '\n';
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
@ -92,7 +104,7 @@ std::string TodoMdFormat::fieldWithSpace(const std::string &field)
|
|||
return (field + " ");
|
||||
}
|
||||
|
||||
TaskItemData 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}) > )?(.*?)( -- (.*))?"
|
||||
|
@ -122,7 +134,7 @@ TaskItemData TodoMdFormat::StringToTask(const std::string &str, const std::strin
|
|||
return taskItem;
|
||||
}
|
||||
|
||||
std::string TodoMdFormat::TaskToString(const TaskItem &task)
|
||||
std::string TodoMdFormat::taskToString(const TaskItem &task)
|
||||
{
|
||||
std::string str = task.getText();
|
||||
if (task.getTags().size() > 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue