Move dependencies in 'external' directory and pdate git submodules

This commit is contained in:
Vyn 2024-08-31 09:42:46 +02:00
parent 63bf267a22
commit cbaa1b58d8
608 changed files with 198659 additions and 199 deletions

127
external/mirai-core/src/BaseResource.cpp vendored Normal file
View file

@ -0,0 +1,127 @@
/*
* 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 "Day.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_;
}
Day *BaseResource::day(const Date &date)
{
auto dayFound = std::find_if(days_.begin(), days_.end(), [&](std::unique_ptr<Day> &day) {
return day->getDate().day == date.day && day->getDate().month == date.month &&
day->getDate().year == date.year;
});
if (dayFound == days_.end()) {
auto newDay = std::make_unique<Day>(this, DayData{.date = date});
Day *dayPtr = newDay.get();
days_.push_back(std::move(newDay));
return dayPtr;
}
return dayFound->get();
}
std::vector<std::unique_ptr<Day>> *BaseResource::days()
{
return &days_;
}
void BaseResource::addDay(const DayData &dayData)
{
days_.push_back(std::make_unique<Day>(this, dayData));
setDirty(true);
}
TaskItem *BaseResource::getTaskById(int taskId)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
}
}
return nullptr;
}
void BaseResource::deleteTask(const TaskItem &taskToDelete)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskToDelete.id()) {
day->deleteTask(taskToDelete);
return;
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskToDelete.id()) {
event->deleteTask(taskToDelete);
return;
}
}
}
}
}
Event *BaseResource::getEventById(int eventId)
{
for (auto &day : days_) {
for (auto &event : *day->events()) {
if (event->id() == eventId) {
return event.get();
}
}
}
return nullptr;
}
/*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);*/
/*}*/
int BaseResource::nextId_ = 0;
} // namespace mirai

30
external/mirai-core/src/Config.cpp vendored Normal file
View file

@ -0,0 +1,30 @@
/*
* 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 "Config.h"
#include "ConfigImpl.h"
#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
Config::~Config() = default;
Config::Config(const std::string &path) : impl_(new ConfigImpl(path))
{
}
std::vector<std::string> Config::sources() const
{
return impl_->sources();
}
}; // namespace mirai

40
external/mirai-core/src/ConfigImpl.cpp vendored Normal file
View file

@ -0,0 +1,40 @@
/*
* 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 "ConfigImpl.h"
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace mirai
{
ConfigImpl::ConfigImpl(const std::string &path) : path_(path)
{
std::ifstream file(path_);
configJson_ = nlohmann::json::parse(file);
}
std::vector<std::string> ConfigImpl::sources() const
{
std::vector<std::string> sources;
assert(configJson_.is_object());
assert(configJson_["files"].is_array());
auto jsonSources = configJson_["files"];
for (const auto &filePath : jsonSources) {
assert(filePath.is_string());
sources.push_back(filePath.get<std::string>());
}
return sources;
}
std::string ConfigImpl::path() const
{
return path_;
}
}; // namespace mirai

27
external/mirai-core/src/ConfigImpl.h vendored Normal file
View file

@ -0,0 +1,27 @@
/*
* 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
*/
#pragma once
#include "nlohmann/json.hpp"
#include <string>
namespace mirai
{
class ConfigImpl
{
public:
ConfigImpl(const std::string &path);
std::vector<std::string> sources() const;
std::string path() const;
private:
std::string path_;
nlohmann::json configJson_;
};
} // namespace mirai

37
external/mirai-core/src/DateTime.cpp vendored Normal file
View file

@ -0,0 +1,37 @@
/*
* 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 "DateTime.h"
#include <charconv>
#include <optional>
#include <ranges>
#include <string>
#include <vector>
namespace mirai
{
std::optional<mirai::Date> stringToDate(const std::string &dateStr)
{
using std::operator""sv;
auto dateSplitView =
dateStr | std::views::split("-"sv) | std::views::transform([](auto v) -> int {
int i = 0;
std::from_chars(v.data(), v.data() + v.size(), i);
return i;
});
std::vector<int> dateSplit{dateSplitView.begin(), dateSplitView.end()};
if (dateSplit.size() != 3) {
return std::nullopt;
}
auto year = dateSplit[0];
auto month = dateSplit[1];
auto day = dateSplit[2];
return mirai::Date(year, static_cast<unsigned>(month), static_cast<unsigned>(day));
}
} // namespace mirai

89
external/mirai-core/src/Day.cpp vendored Normal file
View file

@ -0,0 +1,89 @@
/*
* 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 "Day.h"
#include "BaseResource.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <ostream>
#include <vector>
namespace mirai
{
Day::Day(BaseResource *source, const DayData &data) : source_(source), data_(data)
{
}
Event *Day::createEvent(const EventData &eventData)
{
auto event = std::make_unique<Event>(source_, this, eventData);
mirai::Event *eventPtr = event.get();
events_.push_back(std::move(event));
onChange();
return eventPtr;
}
std::vector<std::unique_ptr<Event>> *Day::events()
{
return &events_;
}
const Date &Day::getDate() const
{
return data_.date;
}
TaskItem *Day::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, this, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Day::deleteTask(const TaskItem &taskToDelete)
{
int id = taskToDelete.id();
tasks_.erase(
std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == id;
}
),
tasks_.end()
);
onChange();
}
void Day::deleteEvent(const Event &eventToDelete)
{
int id = eventToDelete.id();
events_.erase(
std::remove_if(
events_.begin(), events_.end(),
[&](const std::unique_ptr<Event> &event) {
return event->id() == id;
}
),
events_.end()
);
onChange();
}
void Day::onChange()
{
source()->setDirty(true);
}
std::vector<std::unique_ptr<TaskItem>> *Day::tasks()
{
return &tasks_;
}
} // namespace mirai

99
external/mirai-core/src/Event.cpp vendored Normal file
View file

@ -0,0 +1,99 @@
/*
* 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 "Event.h"
#include "BaseResource.h"
#include "TaskItem.h"
#include "utils.h"
namespace mirai
{
Event::Event(BaseResource *source, Day *parent, const EventData &data)
: source_(source), day_(parent), data(data)
{
}
void Event::setText(const std::string &text)
{
this->data.description = text;
onChange();
}
const std::string &Event::getText() const
{
return data.description;
}
const Date &Event::getDate() const
{
return data.date;
}
const Time &Event::getStartTime() const
{
return data.startTime;
}
const Time &Event::getEndTime() const
{
return data.endTime;
}
const Tags &Event::getTags() const
{
return data.tags;
}
bool Event::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
TaskItem *Event::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, day_, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Event::deleteTask(const TaskItem &taskToDelete)
{
tasks_.erase(std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == taskToDelete.id();
}
));
}
std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
{
return &tasks_;
}
void Event::setStartTime(const Time &time)
{
data.startTime = time;
onChange();
}
void Event::setEndTime(const Time &time)
{
data.endTime = time;
onChange();
}
void Event::onChange()
{
source_->setDirty(true);
}
int Event::nextId = 0;
} // namespace mirai

View file

@ -0,0 +1,7 @@
/*
* 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 "EventEmitter.h"

92
external/mirai-core/src/Mirai.cpp vendored Normal file
View file

@ -0,0 +1,92 @@
/*
* 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 "Mirai.h"
#include "Config.h"
#include "TaskItem.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <vector>
namespace mirai
{
void Mirai::loadResource(std::unique_ptr<BaseResource> &&resource)
{
resource->load();
resources_.push_back(std::move(resource));
reloadTags();
};
void Mirai::unloadAllResources()
{
resources_.clear();
}
void Mirai::save()
{
for (auto &resource : resources_) {
if (resource->isDirty()) {
resource->save();
resource->setDirty(false);
}
}
reloadTags();
}
void Mirai::removeTask(const TaskItem *taskItem)
{
for (auto &resource : resources_) {
// resource->removeTask(taskItem); // TODO REWORK
}
}
std::vector<std::unique_ptr<BaseResource>> &Mirai::getResources()
{
return resources_;
}
std::optional<std::reference_wrapper<BaseResource>> Mirai::getResourceByName(const std::string &name
)
{
auto resourceIterator =
std::ranges::find_if(resources_, [&](const std::unique_ptr<BaseResource> &resource) {
return resource->getName() == name;
});
if (resourceIterator == resources_.end()) {
return std::nullopt;
}
return *(resourceIterator->get());
}
const std::vector<std::string> &Mirai::getTags()
{
return tags_;
}
void Mirai::reloadTags()
{
/*cpputils::debug::Timer reloadingTagsDuration;*/ // TODO REWORK
/*tags.clear();*/
/*for (auto &resource : resources) {*/
/*for (auto &task : resource->getTasks()) {*/
/*for (auto &tag : task->getTags()) {*/
/*if (!vectorUtils::contains(tags, tag)) {*/
/*tags.push_back(tag);*/
/*}*/
/*}*/
/*}*/
/*}*/
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
}
} // namespace mirai

65
external/mirai-core/src/TaskItem.cpp vendored Normal file
View file

@ -0,0 +1,65 @@
/*
* 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 "TaskItem.h"
#include "BaseResource.h"
#include "utils.h"
#include <iostream>
namespace mirai
{
int TaskItem::nextId = 0;
TaskItem::TaskItem(BaseResource *source, Day *day, TaskItemData data)
: source_(source), day_(day), data(data)
{
}
void TaskItem::markAsDone()
{
data.state = DONE;
onChange();
}
void TaskItem::markAsUndone()
{
data.state = TODO;
onChange();
}
void TaskItem::setText(const std::string &text)
{
this->data.text = text;
onChange();
}
const std::string &TaskItem::getText() const
{
return data.text;
}
const TaskItemState &TaskItem::getState() const
{
return data.state;
}
const Tags &TaskItem::getTags() const
{
return data.tags;
}
bool TaskItem::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
void TaskItem::onChange()
{
source()->setDirty(true);
}
} // namespace mirai

176
external/mirai-core/src/TasksView.cpp vendored Normal file
View file

@ -0,0 +1,176 @@
/*
* 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 "TasksView.h"
#include "Mirai.h"
#include "TaskItem.h"
#include "utils.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <ctime>
#include <format>
#include <iostream>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace mirai
{
TasksView::TasksView(Mirai *miraiInstance) : mirai(miraiInstance)
{
if (!miraiInstance) {
throw std::runtime_error("NULL pointer passed in TasksView");
}
update();
}
FilteredDay &TasksView::operator[](int index)
{
return filteredDays.at(index);
}
size_t TasksView::count() const
{
return filteredDays.size();
}
void TasksView::update()
{
auto todayDate = std::format("{:%Y-%m-%d}", std::chrono::system_clock::now());
filteredDays.clear();
for (auto &file : mirai->getResources()) {
if (resourcesFilter.size() > 0 &&
!vectorUtils::contains(resourcesFilter, file->getName())) {
continue;
}
for (auto &day : *file->days()) {
FilteredDay filteredDay{.day = day.get()};
for (auto &task : *day->tasks()) {
auto taskDate = std::format(
"{}-0{}-{}", day->getDate().year, // TODO REWORK REMOVE 0{}
day->getDate().month,
day->getDate().day // TODO REWORK CLEAN THIS MESS
);
if (shouldHideCompletedTasks() && task->getState() == DONE &&
taskDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredDay.filteredTasks.push_back(task.get());
}
for (auto &event : *day->events()) {
FilteredEvent filteredEvent{.event = event.get()};
auto eventDate = std::format(
// TODO REWORK
"{}-0{}-{}", day->getDate().year, day->getDate().month, day->getDate().day
);
for (auto &task : *event->tasks()) {
if (shouldHideCompletedTasks() && task->getState() == DONE &&
eventDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredEvent.filteredTasks.push_back(task.get());
}
if (!filteredEvent.filteredTasks.empty() || !shouldHideCompletedTasks() ||
eventDate >= todayDate) {
filteredDay.filteredEvents.push_back(filteredEvent);
}
}
if (!filteredDay.filteredEvents.empty()) {
std::ranges::sort(
filteredDay.filteredEvents,
[](const FilteredEvent &t1, const FilteredEvent &t2) {
return t1.event->getStartTime().hour < t2.event->getStartTime().hour;
}
);
}
if (!filteredDay.filteredEvents.empty() || !filteredDay.filteredTasks.empty()) {
auto existingDay = std::ranges::find_if(filteredDays, [&](const FilteredDay &date) {
return date.day->getDate() == filteredDay.day->getDate();
});
if (existingDay == filteredDays.end()) {
filteredDays.push_back(filteredDay);
} else {
existingDay->filteredTasks.insert(
existingDay->filteredTasks.end(), filteredDay.filteredTasks.begin(),
filteredDay.filteredTasks.end()
);
existingDay->filteredEvents.insert(
existingDay->filteredEvents.end(), filteredDay.filteredEvents.begin(),
filteredDay.filteredEvents.end()
);
}
}
}
std::ranges::sort(filteredDays, [](const FilteredDay &t1, const FilteredDay &t2) {
return t1.day->getDate() < t2.day->getDate();
});
}
}
void TasksView::addTagFilter(const std::string &tag)
{
tagsFilter.push_back(tag);
update();
}
void TasksView::removeTagFilter(const std::string &tag)
{
tagsFilter
.erase(std::remove_if(tagsFilter.begin(), tagsFilter.end(), [&](const auto &tagInFilter) {
return tag == tagInFilter;
}));
update();
}
void TasksView::addResourceFilter(const std::string &fileName)
{
resourcesFilter.push_back(fileName);
update();
}
void TasksView::removeResourceFilter(const std::string &fileName)
{
resourcesFilter.erase(std::remove_if(
resourcesFilter.begin(), resourcesFilter.end(),
[&](const auto &fileInFilter) {
return fileName == fileInFilter;
}
));
update();
}
void TasksView::removeFilters()
{
tagsFilter.clear();
update();
}
const std::vector<std::string> &TasksView::getActiveTagsFilter()
{
return tagsFilter;
}
const std::vector<std::string> &TasksView::getActiveFilesFilter()
{
return resourcesFilter;
}
} // namespace mirai

224
external/mirai-core/src/TodoMd.cpp vendored Normal file
View file

@ -0,0 +1,224 @@
/*
* 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 "TodoMd.h"
#include "TaskItem.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <charconv>
#include <fstream>
#include <iostream>
#include <memory>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
namespace mirai
{
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 (!vectorUtils::contains(tags, matches[2].str())) {
tags.push_back(matches[2]);
}
metadata = matches.suffix();
}
return tags;
}
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
{
if (field.length() == 0) {
return "";
}
return (field + " ");
}
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_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 = stringUtils::trim(matches[5]);
TaskItemData taskItem{
.text = text,
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
.tags = extractTagsFromMetadata(matches[7])
};
return taskItem;
}
Time stringToTime(const std::string &str)
{
if (str.length() != 5) {
throw std::runtime_error("Str time length must be 5 (got '" + str + "')");
}
if (str[2] != 'h') {
throw std::runtime_error("Malformated time range");
}
Time time{};
std::from_chars(str.data(), str.data() + 2, time.hour);
std::from_chars(str.data() + 3, str.data() + 5, time.minute);
return time;
}
EventData TodoMdFormat::stringToEvent(const std::string &str, const std::string &dateString)
{
std::smatch matches;
std::regex regex("> (([0-9]{2}h[0-9]{2})-([0-9]{2}h[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::string text = stringUtils::trim(matches[4]);
auto date = stringToDate(dateString);
if (!date.has_value()) {
throw std::runtime_error("Malformated date");
}
EventData eventData{
.description = text,
.date = date.value(),
.startTime = stringToTime(matches[2]),
.endTime = stringToTime(matches[3]),
.tags = extractTagsFromMetadata(matches[6])
};
return eventData;
}
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;
}
}
str = (task.getState() == DONE ? "- [X] " : "- [ ] ") + str;
return str;
}
std::string
TodoMdFormat::stringify(const std::string &name, const std::vector<std::unique_ptr<Day>> &days)
{
std::string result = "# " + name + "\n\n";
std::string currentDate = "";
for (const auto &day : days) {
auto &date = day->getDate();
result +=
"## " + std::format("{:02d}-{:02d}-{:02d}", date.year, date.month, date.day) + "\n\n";
for (const auto &event : *(day->events())) {
auto &start = event->getStartTime();
auto &end = event->getEndTime();
result += "> " +
std::format(
"{:02d}h{:02d}-{:02d}h{:02d} {}", start.hour, start.minute, end.hour,
end.minute, event->getText()
) +
"\n";
for (const auto &task : *(event->tasks())) {
result += taskToString(*task) + '\n';
}
result += '\n';
}
for (const auto &task : *(day->tasks())) {
result += taskToString(*task) + '\n';
}
result += '\n';
}
return result;
};
MiraiMarkdownFormatParseResult TodoMdFormat::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 currentDateString = "";
std::vector<mirai::DayData> daysData;
mirai::DayData *currentDay = nullptr;
mirai::EventData *currentEvent = nullptr;
cpputils::debug::Timer stringToTaskDuration;
stringToTaskDuration.reset();
cpputils::debug::Timer gelinesDuration;
while (std::getline(contentStream, line)) {
if (std::string_view{line.data(), 3} == "## ") {
currentDateString = line.substr(3);
auto dateOpt = mirai::stringToDate(currentDateString);
if (!dateOpt.has_value()) {
throw std::runtime_error("Malformated date");
}
daysData.push_back({.date = dateOpt.value()});
currentDay = &daysData.back();
} else if (line.starts_with("> ")) {
auto event = stringToEvent(line, currentDateString);
if (currentDay) {
currentDay->events.push_back(event);
currentEvent = &currentDay->events.back();
}
} else if (line.starts_with("- [ ]") || line.starts_with("- [X]")) {
stringToTaskDuration.start();
TaskItemData taskItemData = stringToTask(line, currentDateString);
stringToTaskDuration.stop();
if (currentEvent) {
currentEvent->tasks.push_back(taskItemData);
} else if (currentDay) {
currentDay->tasks.push_back(taskItemData);
}
} else if (cpputils::string::trim(line) == "") {
currentEvent = nullptr;
}
}
gelinesDuration.printTimeElapsed("getlinesDuration");
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
readMdFormatDuration.printTimeElapsed("Reading MD File duration");
return {.name = name, .days = daysData};
}
} // namespace mirai

38
external/mirai-core/src/utils.cpp vendored Normal file
View file

@ -0,0 +1,38 @@
/*
* 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 "utils.h"
#include <cctype>
namespace mirai
{
bool isDate(const std::string &dateStr)
{
// 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