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
|
@ -89,7 +89,7 @@ void Backend::addTodoFromRawFormat(QString filePath, QString text, QString date)
|
|||
return;
|
||||
}
|
||||
file.value().get().addTask(
|
||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString())
|
||||
mirai::TodoMdFormat::stringToTask(text.toStdString(), date.toStdString())
|
||||
);
|
||||
mirai.save();
|
||||
view.update();
|
||||
|
@ -137,7 +137,7 @@ void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
|||
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
||||
taskItem.taskItem->setText(taskItem.taskItem->getText());
|
||||
*(taskItem.taskItem) =
|
||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
|
||||
mirai::TodoMdFormat::stringToTask(text.toStdString(), date.toStdString());
|
||||
mirai.save();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
QString QMLTaskItem::getRawFormat()
|
||||
{
|
||||
return QString::fromStdString(mirai::TodoMdFormat::TaskToString(*taskItem));
|
||||
return QString::fromStdString(mirai::TodoMdFormat::taskToString(*taskItem));
|
||||
}
|
||||
|
||||
QString QMLTaskItem::getText()
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "Mirai.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TasksFile.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
@ -38,6 +39,7 @@ void Mirai::save()
|
|||
{
|
||||
for (auto &file : files) {
|
||||
if (file->isDirty()) {
|
||||
file->sortByDate();
|
||||
TodoMdFormat::writeFile(*file);
|
||||
file->setDirty(false);
|
||||
}
|
||||
|
@ -90,6 +92,7 @@ const std::vector<std::string> &Mirai::getTags()
|
|||
|
||||
void Mirai::reloadTags()
|
||||
{
|
||||
cpputils::debug::Timer reloadingTagsDuration;
|
||||
tags.clear();
|
||||
for (auto &file : files) {
|
||||
for (auto &task : file->getTasks()) {
|
||||
|
@ -100,6 +103,7 @@ void Mirai::reloadTags()
|
|||
}
|
||||
}
|
||||
}
|
||||
reloadingTagsDuration.printTimeElapsed("ReloadingTags");
|
||||
}
|
||||
|
||||
} // namespace mirai
|
||||
|
|
|
@ -59,7 +59,6 @@ void TasksFile::addTask(TaskItemData taskItem)
|
|||
{
|
||||
tasks.push_back(std::make_unique<TaskItem>(this, taskItem));
|
||||
setDirty(true);
|
||||
sortByDate();
|
||||
}
|
||||
|
||||
void TasksFile::addTask(std::string text, std::string date)
|
||||
|
@ -68,7 +67,6 @@ void TasksFile::addTask(std::string text, std::string date)
|
|||
TaskItem{this, {.text = text, .state = TODO, .date = date == "" ? "No date" : date}}
|
||||
);
|
||||
tasks.push_back(std::move(newTask));
|
||||
sortByDate();
|
||||
}
|
||||
|
||||
void TasksFile::removeTask(const TaskItem *taskToRemove)
|
||||
|
|
|
@ -57,6 +57,7 @@ void TasksView::update()
|
|||
} else if (!t1->hasDate() && t2->hasDate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (t1->getDate() < t2->getDate()) {
|
||||
return true;
|
||||
} else if (t1->getDate() > t2->getDate()) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -20,8 +20,8 @@ class TodoMdFormat
|
|||
static std::unique_ptr<TasksFile> readFile(const std::string &path);
|
||||
static void writeFile(TasksFile &tasks);
|
||||
|
||||
static std::string TaskToString(const TaskItem &task);
|
||||
static TaskItemData StringToTask(const std::string &str, const std::string &date);
|
||||
static std::string taskToString(const TaskItem &task);
|
||||
static TaskItemData stringToTask(const std::string &str, const std::string &date);
|
||||
|
||||
private:
|
||||
static std::string fieldWithSpace(const std::string &field);
|
||||
|
|
|
@ -5,12 +5,34 @@
|
|||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include <cctype>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
bool isDate(const std::string &dateStr)
|
||||
{
|
||||
std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");
|
||||
return std::regex_match(dateStr, regex);
|
||||
// std regex are really slow
|
||||
/*std::regex regex("[0-9]{4}-[0-9]{2}-[0-9]{2}");*/
|
||||
/*return std::regex_match(dateStr, regex);*/
|
||||
|
||||
// Quick hand made check instead of regex until I rework date to use timestamp or std::chrono
|
||||
if (dateStr.length() != 10) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dateStr[4] != '-' || dateStr[7] != '-') {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < dateStr.length(); ++i) {
|
||||
if (i == 4 || i == 7) {
|
||||
// These are the '-' characters
|
||||
continue;
|
||||
}
|
||||
if (!isdigit(dateStr[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace mirai
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue