mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 10:13:42 +00:00
Change 'File' concept to a 'Resource' abstract concept
This commit is contained in:
parent
7eb54cddce
commit
ca34562a1c
23 changed files with 447 additions and 351 deletions
42
src/core/BaseFileResource.h
Normal file
42
src/core/BaseFileResource.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#ifndef MIRAI_BASE_FILE_RESOURCE_H
|
||||
#define MIRAI_BASE_FILE_RESOURCE_H
|
||||
|
||||
#include "core/BaseResource.h"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct BaseFileResourceConstructor {
|
||||
std::string name;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
class BaseFileResource : public BaseResource
|
||||
{
|
||||
public:
|
||||
BaseFileResource(BaseFileResourceConstructor data) : BaseResource(data.name), path(data.path){};
|
||||
|
||||
BaseFileResource(BaseFileResource &) = delete;
|
||||
BaseFileResource(BaseFileResource &&) = delete;
|
||||
BaseFileResource operator=(BaseFileResource &) = delete;
|
||||
BaseFileResource operator=(BaseFileResource &&) = delete;
|
||||
|
||||
~BaseFileResource() override = default;
|
||||
|
||||
const std::string &getPath() const
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string path;
|
||||
};
|
||||
} // namespace mirai
|
||||
#endif
|
52
src/core/BaseResource.cpp
Normal file
52
src/core/BaseResource.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#include "BaseResource.h"
|
||||
#include "TaskItem.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void BaseResource::setDirty(bool shouldBeDirty)
|
||||
{
|
||||
isDirty_ = shouldBeDirty;
|
||||
}
|
||||
|
||||
bool BaseResource::isDirty() const
|
||||
{
|
||||
return isDirty_;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> &BaseResource::getTasks()
|
||||
{
|
||||
return tasks;
|
||||
}
|
||||
|
||||
void BaseResource::addTask(TaskItemData taskItem)
|
||||
{
|
||||
tasks.push_back(std::make_unique<TaskItem>(this, taskItem));
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void BaseResource::removeTask(const TaskItem *taskToRemove)
|
||||
{
|
||||
auto findFunction = [&](const std::unique_ptr<TaskItem> &taskInFilter) {
|
||||
return taskInFilter.get() == taskToRemove;
|
||||
};
|
||||
auto taskToDelete = std::remove_if(tasks.begin(), tasks.end(), findFunction);
|
||||
if (taskToDelete == tasks.end()) {
|
||||
return;
|
||||
}
|
||||
tasks.erase(taskToDelete);
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
} // namespace mirai
|
54
src/core/BaseResource.h
Normal file
54
src/core/BaseResource.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#ifndef MIRAI_BASE_RESOURCE_H
|
||||
#define MIRAI_BASE_RESOURCE_H
|
||||
|
||||
#include "core/TaskItem.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class BaseResource
|
||||
{
|
||||
public:
|
||||
BaseResource(std::string name) : name(name){};
|
||||
BaseResource(BaseResource &) = delete;
|
||||
BaseResource(BaseResource &&) = delete;
|
||||
BaseResource operator=(BaseResource &) = delete;
|
||||
BaseResource operator=(BaseResource &&) = delete;
|
||||
|
||||
virtual ~BaseResource() = default;
|
||||
virtual void save() = 0;
|
||||
virtual void load() = 0;
|
||||
|
||||
void setName(std::string name)
|
||||
{
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
const std::string &getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
void addTask(TaskItemData TaskItem);
|
||||
void removeTask(const TaskItem *taskToRemove);
|
||||
std::vector<std::unique_ptr<TaskItem>> &getTasks();
|
||||
|
||||
void setDirty(bool shouldBeDirty);
|
||||
bool isDirty() const;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks;
|
||||
bool isDirty_ = false;
|
||||
};
|
||||
} // namespace mirai
|
||||
#endif
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include "Mirai.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TasksFile.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
|
@ -19,70 +18,53 @@
|
|||
namespace mirai
|
||||
{
|
||||
|
||||
void Mirai::loadFile(const std::string &path)
|
||||
void Mirai::loadResource(std::unique_ptr<BaseResource> &&resource)
|
||||
{
|
||||
try {
|
||||
auto tasksFile = TodoMdFormat::readFile(path);
|
||||
files.push_back(std::move(tasksFile));
|
||||
reloadTags();
|
||||
} catch (std::exception &e) {
|
||||
std::cout << "Cannot load file " << path << " : " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
resource->load();
|
||||
resources.push_back(std::move(resource));
|
||||
reloadTags();
|
||||
};
|
||||
|
||||
void Mirai::unloadAllFiles()
|
||||
void Mirai::unloadAllResources()
|
||||
{
|
||||
files.clear();
|
||||
resources.clear();
|
||||
}
|
||||
|
||||
void Mirai::save()
|
||||
{
|
||||
for (auto &file : files) {
|
||||
if (file->isDirty()) {
|
||||
file->sortByDate();
|
||||
TodoMdFormat::writeFile(*file);
|
||||
file->setDirty(false);
|
||||
for (auto &resource : resources) {
|
||||
if (resource->isDirty()) {
|
||||
resource->save();
|
||||
resource->setDirty(false);
|
||||
}
|
||||
}
|
||||
reloadTags();
|
||||
}
|
||||
|
||||
void Mirai::addTask(TaskItemData taskItem)
|
||||
{
|
||||
files[0]->addTask(taskItem);
|
||||
}
|
||||
|
||||
void Mirai::addTask(std::string text, std::string date)
|
||||
{
|
||||
/*std::time_t t = std::time(nullptr);*/
|
||||
/*std::tm tm = *std::localtime(&t);*/
|
||||
/*std::stringstream ssCreationDate;*/
|
||||
/*ssCreationDate << std::put_time(&tm, "%Y-%m-%d");*/
|
||||
files[0]->addTask(text, date);
|
||||
}
|
||||
|
||||
void Mirai::removeTask(const TaskItem *taskItem)
|
||||
{
|
||||
for (auto &file : files) {
|
||||
file->removeTask(taskItem);
|
||||
for (auto &resource : resources) {
|
||||
resource->removeTask(taskItem);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TasksFile>> &Mirai::getFiles()
|
||||
std::vector<std::unique_ptr<BaseResource>> &Mirai::getResources()
|
||||
{
|
||||
return files;
|
||||
return resources;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<TasksFile>> Mirai::getFileByPath(const std::string &path)
|
||||
std::optional<std::reference_wrapper<BaseResource>> Mirai::getResourceByName(const std::string &name
|
||||
)
|
||||
{
|
||||
auto fileIterator = std::ranges::find_if(files, [&](const std::unique_ptr<TasksFile> &file) {
|
||||
return file->getPath() == path;
|
||||
});
|
||||
auto resourceIterator =
|
||||
std::ranges::find_if(resources, [&](const std::unique_ptr<BaseResource> &resource) {
|
||||
return resource->getName() == name;
|
||||
});
|
||||
|
||||
if (fileIterator == files.end()) {
|
||||
if (resourceIterator == resources.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *(fileIterator->get());
|
||||
return *(resourceIterator->get());
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Mirai::getTags()
|
||||
|
@ -94,8 +76,8 @@ void Mirai::reloadTags()
|
|||
{
|
||||
cpputils::debug::Timer reloadingTagsDuration;
|
||||
tags.clear();
|
||||
for (auto &file : files) {
|
||||
for (auto &task : file->getTasks()) {
|
||||
for (auto &resource : resources) {
|
||||
for (auto &task : resource->getTasks()) {
|
||||
for (auto &tag : task->getTags()) {
|
||||
if (!vectorUtils::contains(tags, tag)) {
|
||||
tags.push_back(tag);
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#define MIRAI_MIRAI_H
|
||||
|
||||
#include "TaskItem.h"
|
||||
#include "TasksFile.h"
|
||||
#include "TodoMd.h"
|
||||
#include "core/BaseFileResource.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
@ -23,22 +23,19 @@ class Mirai
|
|||
{
|
||||
|
||||
public:
|
||||
void loadFile(const std::string &path);
|
||||
void unloadAllFiles();
|
||||
void loadResource(std::unique_ptr<BaseResource> &&resource);
|
||||
void unloadAllResources();
|
||||
void save();
|
||||
void addTask(TaskItemData taskItem);
|
||||
void addTask(std::string text, std::string date);
|
||||
void removeTask(const TaskItem *taskItem);
|
||||
void addFile(TasksFileConfig config);
|
||||
std::optional<std::reference_wrapper<TasksFile>> getFileByPath(const std::string &path);
|
||||
std::optional<std::reference_wrapper<BaseResource>> getResourceByName(const std::string &name);
|
||||
|
||||
std::vector<std::unique_ptr<TasksFile>> &getFiles();
|
||||
std::vector<std::unique_ptr<BaseResource>> &getResources();
|
||||
const std::vector<std::string> &getTags();
|
||||
|
||||
void reloadTags();
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<TasksFile>> files = std::vector<std::unique_ptr<TasksFile>>();
|
||||
std::vector<std::unique_ptr<BaseResource>> resources;
|
||||
std::vector<std::string> tags;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
63
src/core/StdFileResource.h
Normal file
63
src/core/StdFileResource.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#ifndef MIRAI_STD_FILE_RESOURCE_H
|
||||
#define MIRAI_STD_FILE_RESOURCE_H
|
||||
|
||||
#include "core/BaseFileResource.h"
|
||||
#include "core/TodoMd.h"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class StdFileResource : public BaseFileResource
|
||||
{
|
||||
public:
|
||||
StdFileResource(BaseFileResourceConstructor data) : BaseFileResource(data){};
|
||||
StdFileResource(StdFileResource &) = delete;
|
||||
StdFileResource(StdFileResource &&) = delete;
|
||||
StdFileResource operator=(StdFileResource &) = delete;
|
||||
StdFileResource operator=(StdFileResource &&) = delete;
|
||||
|
||||
~StdFileResource() override = default;
|
||||
|
||||
void save() override
|
||||
{
|
||||
std::ofstream file(getPath());
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("can't create " + getPath());
|
||||
}
|
||||
|
||||
const std::string content = TodoMdFormat::stringifyTasks(getName(), getTasks());
|
||||
|
||||
file << content;
|
||||
file.close();
|
||||
};
|
||||
|
||||
void load() override
|
||||
{
|
||||
std::ifstream file(getPath());
|
||||
if (!file.is_open()) {
|
||||
return;
|
||||
}
|
||||
std::string content = "";
|
||||
std::string line;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
content += line + "\n";
|
||||
}
|
||||
file.close();
|
||||
auto result = TodoMdFormat::parse(content);
|
||||
for (const auto &task : result.tasks) {
|
||||
addTask(task);
|
||||
}
|
||||
setName(result.name);
|
||||
};
|
||||
};
|
||||
} // namespace mirai
|
||||
#endif
|
|
@ -5,14 +5,14 @@
|
|||
*/
|
||||
|
||||
#include "TaskItem.h"
|
||||
#include "TasksFile.h"
|
||||
#include "BaseResource.h"
|
||||
#include "core/utils.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
TaskItem::TaskItem(TasksFile *parent, TaskItemData data) : parent(parent), data(data)
|
||||
TaskItem::TaskItem(BaseResource *parent, TaskItemData data) : parent(parent), data(data)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -24,15 +24,15 @@ struct TaskItemData {
|
|||
Tags tags;
|
||||
};
|
||||
|
||||
class TasksFile;
|
||||
class BaseResource;
|
||||
|
||||
class TaskItem
|
||||
{
|
||||
friend TasksFile;
|
||||
friend BaseResource;
|
||||
|
||||
private:
|
||||
public:
|
||||
TaskItem(TasksFile *parent, const TaskItemData data);
|
||||
TaskItem(BaseResource *parent, const TaskItemData data);
|
||||
|
||||
TaskItem &operator=(const TaskItemData &newData)
|
||||
{
|
||||
|
@ -62,7 +62,7 @@ class TaskItem
|
|||
private:
|
||||
void onChange();
|
||||
|
||||
TasksFile *parent;
|
||||
BaseResource *parent;
|
||||
TaskItemData data;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#include "TasksFile.h"
|
||||
#include "TaskItem.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
TasksFile::TasksFile(TasksFileConstructor params) : name(params.name), path(params.path)
|
||||
{
|
||||
for (const auto &task : params.tasks) {
|
||||
auto taskPtr = std::make_unique<TaskItem>(task);
|
||||
tasks.push_back(std::move(taskPtr));
|
||||
}
|
||||
sortByDate();
|
||||
}
|
||||
|
||||
void TasksFile::onTaskChanged(TaskItem &taskItem)
|
||||
{
|
||||
std::cout << "THIS PTR: " << this << std::endl;
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void TasksFile::setDirty(bool shouldBeDirty)
|
||||
{
|
||||
isDirty_ = shouldBeDirty;
|
||||
}
|
||||
|
||||
bool TasksFile::isDirty() const
|
||||
{
|
||||
return isDirty_;
|
||||
}
|
||||
|
||||
const std::string &TasksFile::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
const std::string &TasksFile::getPath() const
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
TasksPtrs &TasksFile::getTasks()
|
||||
{
|
||||
return tasks;
|
||||
}
|
||||
|
||||
void TasksFile::addTask(TaskItemData taskItem)
|
||||
{
|
||||
tasks.push_back(std::make_unique<TaskItem>(this, taskItem));
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void TasksFile::addTask(std::string text, std::string date)
|
||||
{
|
||||
auto newTask = std::make_unique<TaskItem>(
|
||||
TaskItem{this, {.text = text, .state = TODO, .date = date == "" ? "No date" : date}}
|
||||
);
|
||||
tasks.push_back(std::move(newTask));
|
||||
}
|
||||
|
||||
void TasksFile::removeTask(const TaskItem *taskToRemove)
|
||||
{
|
||||
auto findFunction = [&](const std::unique_ptr<TaskItem> &taskInFilter) {
|
||||
return taskInFilter.get() == taskToRemove;
|
||||
};
|
||||
auto taskToDelete = std::remove_if(tasks.begin(), tasks.end(), findFunction);
|
||||
if (taskToDelete == tasks.end()) {
|
||||
return;
|
||||
}
|
||||
tasks.erase(taskToDelete);
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
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()) {
|
||||
return true;
|
||||
} else if (!t1->hasDate() && t2->hasDate()) {
|
||||
return false;
|
||||
}
|
||||
return t1->getDate() < t2->getDate();
|
||||
}
|
||||
);
|
||||
}
|
||||
} // namespace mirai
|
|
@ -1,60 +0,0 @@
|
|||
/*
|
||||
* Mirai. Copyright (C) 2024 Vyn
|
||||
* This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
|
||||
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
|
||||
#ifndef MIRAI_TASKSFILE_H
|
||||
#define MIRAI_TASKSFILE_H
|
||||
|
||||
#include "TaskItem.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
using TasksPtrs = std::vector<std::unique_ptr<TaskItem>>;
|
||||
|
||||
struct TasksFileConfig {
|
||||
std::string name;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct TasksFileConstructor {
|
||||
const std::string &name;
|
||||
const std::string &path;
|
||||
std::vector<TaskItem> tasks;
|
||||
};
|
||||
|
||||
class TasksFile
|
||||
{
|
||||
|
||||
public:
|
||||
TasksFile(const TasksFile &) = delete;
|
||||
TasksFile &operator=(const TasksFile &) = delete;
|
||||
TasksFile(TasksFileConstructor params);
|
||||
void addTask(std::string text, std::string date);
|
||||
void addTask(TaskItemData TaskItem);
|
||||
void removeTask(const TaskItem *taskToRemove);
|
||||
void sortByDate();
|
||||
|
||||
const std::string &getName() const;
|
||||
const std::string &getPath() const;
|
||||
TasksPtrs &getTasks();
|
||||
|
||||
void setDirty(bool shouldBeDirty);
|
||||
bool isDirty() const;
|
||||
|
||||
private:
|
||||
void onTaskChanged(TaskItem &);
|
||||
|
||||
std::string name;
|
||||
std::string path;
|
||||
TasksPtrs tasks;
|
||||
bool isDirty_ = false;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
||||
#endif
|
|
@ -39,8 +39,9 @@ size_t TasksView::count() const
|
|||
void TasksView::update()
|
||||
{
|
||||
tasksToShow.clear();
|
||||
for (auto &file : mirai->getFiles()) {
|
||||
if (filesFilter.size() > 0 && !vectorUtils::contains(filesFilter, file->getName())) {
|
||||
for (auto &file : mirai->getResources()) {
|
||||
if (resourcesFilter.size() > 0 &&
|
||||
!vectorUtils::contains(resourcesFilter, file->getName())) {
|
||||
continue;
|
||||
}
|
||||
for (auto &task : file->getTasks()) {
|
||||
|
@ -88,16 +89,16 @@ void TasksView::removeTagFilter(const std::string &tag)
|
|||
update();
|
||||
}
|
||||
|
||||
void TasksView::addFileFilter(const std::string &fileName)
|
||||
void TasksView::addResourceFilter(const std::string &fileName)
|
||||
{
|
||||
filesFilter.push_back(fileName);
|
||||
resourcesFilter.push_back(fileName);
|
||||
update();
|
||||
}
|
||||
|
||||
void TasksView::removeFileFilter(const std::string &fileName)
|
||||
void TasksView::removeResourceFilter(const std::string &fileName)
|
||||
{
|
||||
filesFilter.erase(std::remove_if(
|
||||
filesFilter.begin(), filesFilter.end(),
|
||||
resourcesFilter.erase(std::remove_if(
|
||||
resourcesFilter.begin(), resourcesFilter.end(),
|
||||
[&](const auto &fileInFilter) {
|
||||
return fileName == fileInFilter;
|
||||
}
|
||||
|
@ -118,6 +119,6 @@ const std::vector<std::string> &TasksView::getActiveTagsFilter()
|
|||
|
||||
const std::vector<std::string> &TasksView::getActiveFilesFilter()
|
||||
{
|
||||
return filesFilter;
|
||||
return resourcesFilter;
|
||||
}
|
||||
} // namespace mirai
|
||||
|
|
|
@ -6,11 +6,9 @@
|
|||
|
||||
#ifndef MIRAI_TASKSVIEW_H
|
||||
#define MIRAI_TASKSVIEW_H
|
||||
#include "TasksFile.h"
|
||||
#include "core/Mirai.h"
|
||||
#include "using.h"
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
|
@ -28,8 +26,8 @@ class TasksView
|
|||
void update();
|
||||
void addTagFilter(const std::string &tag);
|
||||
void removeTagFilter(const std::string &tag);
|
||||
void addFileFilter(const std::string &fileName);
|
||||
void removeFileFilter(const std::string &fileName);
|
||||
void addResourceFilter(const std::string &fileName);
|
||||
void removeResourceFilter(const std::string &fileName);
|
||||
void removeFilters();
|
||||
const Tags &getActiveTagsFilter();
|
||||
const std::vector<std::string> &getActiveFilesFilter();
|
||||
|
@ -38,7 +36,7 @@ class TasksView
|
|||
Mirai *mirai;
|
||||
std::vector<TaskItem *> tasksToShow;
|
||||
Tags tagsFilter;
|
||||
std::vector<std::string> filesFilter;
|
||||
std::vector<std::string> resourcesFilter;
|
||||
};
|
||||
} // namespace mirai
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include "TodoMd.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TasksFile.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include "utils.h"
|
||||
#include <fstream>
|
||||
|
@ -17,50 +16,6 @@
|
|||
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");
|
||||
}
|
||||
std::vector<TaskItem> taskItems;
|
||||
std::string line;
|
||||
|
||||
if (!std::getline(file, line) || line.substr(0, 2) != "# ") {
|
||||
std::cerr << "Couldn't find the task list name" << std::endl;
|
||||
}
|
||||
|
||||
std::string name = line.substr(2);
|
||||
std::string currentDate = "";
|
||||
|
||||
auto tasksFile = std::make_unique<TasksFile>(
|
||||
TasksFileConstructor{.name = name, .path = path, .tasks = taskItems}
|
||||
);
|
||||
|
||||
cpputils::debug::Timer stringToTaskDuration;
|
||||
stringToTaskDuration.reset();
|
||||
cpputils::debug::Timer gelinesDuration;
|
||||
while (std::getline(file, line)) {
|
||||
if (std::string_view{line.data(), 3} == "## ") {
|
||||
currentDate = line.substr(3);
|
||||
} 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;
|
||||
}
|
||||
|
||||
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
|
||||
{
|
||||
Tags tags;
|
||||
|
@ -77,25 +32,6 @@ Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
|
|||
return tags;
|
||||
}
|
||||
|
||||
void TodoMdFormat::writeFile(TasksFile &tasks)
|
||||
{
|
||||
std::ofstream file(tasks.getPath());
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("can't create todo.txt");
|
||||
}
|
||||
std::string currentDate = "";
|
||||
|
||||
file << "# " << tasks.getName() << "\n";
|
||||
for (const auto &task : tasks.getTasks()) {
|
||||
if (currentDate != task->getDate()) {
|
||||
currentDate = task->getDate();
|
||||
file << "\n## " << (task->getDate() != "" ? task->getDate() : "No date") << "\n\n";
|
||||
}
|
||||
file << taskToString(*task) << '\n';
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
|
||||
{
|
||||
if (field.length() == 0) {
|
||||
|
|
|
@ -8,17 +8,76 @@
|
|||
#define MIRAI_TODOMD_H
|
||||
|
||||
#include "TaskItem.h"
|
||||
#include "TasksFile.h"
|
||||
#include "cpp-utils/debug.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct MiraiMarkdownFormatParseResult {
|
||||
std::string name;
|
||||
std::vector<TaskItemData> tasks;
|
||||
};
|
||||
|
||||
class TodoMdFormat
|
||||
{
|
||||
public:
|
||||
static std::unique_ptr<TasksFile> readFile(const std::string &path);
|
||||
static void writeFile(TasksFile &tasks);
|
||||
static std::string
|
||||
stringifyTasks(const std::string &name, const std::vector<std::unique_ptr<TaskItem>> &tasks)
|
||||
{
|
||||
std::string result = "";
|
||||
std::string currentDate = "";
|
||||
|
||||
result += "# " + name + "\n";
|
||||
for (const auto &task : tasks) {
|
||||
if (currentDate != task->getDate()) {
|
||||
currentDate = task->getDate();
|
||||
result += "\n## " + (task->getDate() != "" ? task->getDate() : "No date") + "\n\n";
|
||||
}
|
||||
result += taskToString(*task) + '\n';
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
static MiraiMarkdownFormatParseResult parse(const std::string &content)
|
||||
{
|
||||
cpputils::debug::Timer readMdFormatDuration;
|
||||
std::vector<TaskItem> taskItems;
|
||||
std::string line;
|
||||
|
||||
std::stringstream contentStream(content);
|
||||
|
||||
if (!std::getline(contentStream, line) || line.substr(0, 2) != "# ") {
|
||||
std::cerr << "Couldn't find the task list name" << std::endl;
|
||||
}
|
||||
|
||||
std::string name = line.substr(2);
|
||||
std::string currentDate = "";
|
||||
|
||||
std::vector<TaskItemData> result;
|
||||
|
||||
cpputils::debug::Timer stringToTaskDuration;
|
||||
stringToTaskDuration.reset();
|
||||
cpputils::debug::Timer gelinesDuration;
|
||||
while (std::getline(contentStream, line)) {
|
||||
if (std::string_view{line.data(), 3} == "## ") {
|
||||
currentDate = line.substr(3);
|
||||
} 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;
|
||||
result.push_back(taskItemData);
|
||||
}
|
||||
}
|
||||
|
||||
gelinesDuration.printTimeElapsed("getlinesDuration");
|
||||
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
|
||||
readMdFormatDuration.printTimeElapsed("Reading MD File duration");
|
||||
return {.name = name, .tasks = result};
|
||||
}
|
||||
|
||||
static std::string taskToString(const TaskItem &task);
|
||||
static TaskItemData stringToTask(const std::string &str, const std::string &date);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue