mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 10:13:42 +00:00
Rework mirai core
This commit is contained in:
parent
f885d355cd
commit
36a2fe9220
62 changed files with 27689 additions and 765 deletions
25
external/mirai-core-old/CMakeLists.txt
vendored
Normal file
25
external/mirai-core-old/CMakeLists.txt
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
cmake_minimum_required(VERSION 3.21)
|
||||
project(mirai LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
|
||||
|
||||
add_library(mirai-core
|
||||
src/Mirai.cpp
|
||||
src/Config.cpp
|
||||
src/ConfigImpl.cpp
|
||||
src/TaskItem.cpp
|
||||
src/Day.cpp
|
||||
src/Event.cpp
|
||||
src/DateTime.cpp
|
||||
src/EventEmitter.cpp
|
||||
src/BaseSource.cpp
|
||||
src/StdFileSource.cpp
|
||||
src/TasksView.cpp
|
||||
src/TodoMd.cpp
|
||||
src/utils.cpp
|
||||
)
|
||||
|
||||
target_include_directories(mirai-core PRIVATE "external")
|
||||
target_include_directories(mirai-core PRIVATE "include/mirai-core")
|
65
external/mirai-core-old/external/cpp-utils/debug.h
vendored
Normal file
65
external/mirai-core-old/external/cpp-utils/debug.h
vendored
Normal 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
|
||||
*/
|
||||
|
||||
#ifndef CPP_UTILS_DEBUG_H
|
||||
#define CPP_UTILS_DEBUG_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace cpputils::debug
|
||||
{
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
Timer() : startTime(std::chrono::steady_clock::now())
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
startTime = std::chrono::steady_clock::now();
|
||||
isRunning = true;
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
duration += std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
duration = 0;
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
void printTimeElapsed(const std::string &message) const
|
||||
{
|
||||
long durationToShow = duration;
|
||||
if (isRunning) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
durationToShow +=
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime).count();
|
||||
}
|
||||
std::cout << "[Debug - Timer] " << message << ": " << durationToShow << " ms" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> startTime;
|
||||
|
||||
// Timer are always running when created. You can use .reset() after creation.
|
||||
bool isRunning = true;
|
||||
long duration = 0;
|
||||
};
|
||||
} // namespace cpputils::debug
|
||||
|
||||
#endif
|
27
external/mirai-core-old/external/cpp-utils/string.h
vendored
Normal file
27
external/mirai-core-old/external/cpp-utils/string.h
vendored
Normal 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
|
||||
*/
|
||||
|
||||
#ifndef CPP_UTILS_STRING_H
|
||||
#define CPP_UTILS_STRING_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace cpputils::string {
|
||||
|
||||
inline std::string trim(std::string str) {
|
||||
str.erase(str.begin(), std::ranges::find_if(str, [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
str.erase(std::ranges::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), str.end());
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
48
external/mirai-core-old/external/cpp-utils/vector.h
vendored
Normal file
48
external/mirai-core-old/external/cpp-utils/vector.h
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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 CPP_UTILS_VECTOR_H
|
||||
#define CPP_UTILS_VECTOR_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace cpputils::vector
|
||||
{
|
||||
|
||||
template <typename C, typename T>
|
||||
concept AnyIterable = std::same_as<typename C::value_type, T> && requires(C c) {
|
||||
{
|
||||
c.begin()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
c.end()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
const_cast<const C &>(c).begin()
|
||||
} -> std::forward_iterator;
|
||||
{
|
||||
const_cast<const C &>(c).end()
|
||||
} -> std::forward_iterator;
|
||||
};
|
||||
|
||||
template <typename T> bool contains(const std::vector<T> &vec, const T &value)
|
||||
{
|
||||
return std::ranges::find(vec, value) != vec.end();
|
||||
}
|
||||
|
||||
template <typename T> bool containsAll(const std::vector<T> &vec, const AnyIterable<T> auto vec2)
|
||||
{
|
||||
for (auto &elem : vec) {
|
||||
if (!contains(vec2, elem)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace cpputils::vector
|
||||
|
||||
#endif
|
24767
external/mirai-core-old/external/nlohmann/json.hpp
vendored
Normal file
24767
external/mirai-core-old/external/nlohmann/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load diff
37
external/mirai-core-old/include/mirai-core/BaseFileSource.h
vendored
Normal file
37
external/mirai-core-old/include/mirai-core/BaseFileSource.h
vendored
Normal 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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "BaseSource.h"
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct BaseFileSourceConstructor {
|
||||
std::string name;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
class BaseFileSource : public BaseSource
|
||||
{
|
||||
public:
|
||||
BaseFileSource(BaseFileSourceConstructor data) : BaseSource(data.name), path(data.path) {};
|
||||
|
||||
BaseFileSource(BaseFileSource &) = delete;
|
||||
BaseFileSource(BaseFileSource &&) = delete;
|
||||
~BaseFileSource() override = default;
|
||||
|
||||
const std::string &getPath() const
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string path;
|
||||
};
|
||||
} // namespace mirai
|
91
external/mirai-core-old/include/mirai-core/BaseSource.h
vendored
Normal file
91
external/mirai-core-old/include/mirai-core/BaseSource.h
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* 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 "DateTime.h"
|
||||
#include "Day.h"
|
||||
#include "Event.h"
|
||||
#include "TaskItem.h"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct SourceData {
|
||||
std::vector<std::unique_ptr<Day>> days;
|
||||
std::vector<std::unique_ptr<Event>> events;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks;
|
||||
};
|
||||
|
||||
struct createTaskParams {
|
||||
std::string title;
|
||||
std::optional<Event> event;
|
||||
std::optional<Date> date;
|
||||
};
|
||||
|
||||
struct createEventParams {
|
||||
std::string title;
|
||||
Date date;
|
||||
Time startsAt;
|
||||
Time endsAt;
|
||||
};
|
||||
|
||||
class BaseSource
|
||||
{
|
||||
|
||||
public:
|
||||
struct AddTaskData {
|
||||
std::string text;
|
||||
Tags tags;
|
||||
};
|
||||
|
||||
BaseSource(std::string name) : name_(name) {};
|
||||
BaseSource(BaseSource &) = delete;
|
||||
BaseSource(BaseSource &&) = delete;
|
||||
|
||||
virtual ~BaseSource() = default;
|
||||
virtual void save() = 0;
|
||||
virtual void load() = 0;
|
||||
|
||||
void setName(std::string name);
|
||||
const std::string &getName() const;
|
||||
|
||||
void createTask(const createTaskParams &task);
|
||||
void removeTask(const TaskItem &task);
|
||||
|
||||
TaskItem *getTaskById(int taskId);
|
||||
Event *getEventById(int eventId);
|
||||
Day *day(const Date &date);
|
||||
|
||||
// --
|
||||
|
||||
// void addDay(const DayData &dayData);
|
||||
// void addUnscheduledTask(const TaskItemData &taskData);
|
||||
// std::vector<std::unique_ptr<TaskItem>> *unscheduledTasks();
|
||||
// std::vector<std::unique_ptr<Day>> *days();
|
||||
|
||||
// void deleteTask(const TaskItem &task);
|
||||
|
||||
void setDirty(bool shouldBeDirty);
|
||||
bool isDirty() const;
|
||||
|
||||
int id() const;
|
||||
|
||||
private:
|
||||
static int nextId_;
|
||||
int id_ = nextId_++;
|
||||
std::string name_;
|
||||
SourceData data;
|
||||
// std::vector<std::unique_ptr<Day>> days_;
|
||||
// std::vector<std::unique_ptr<TaskItem>> unscheduledTasks_;
|
||||
bool isDirty_ = false;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
29
external/mirai-core-old/include/mirai-core/Config.h
vendored
Normal file
29
external/mirai-core-old/include/mirai-core/Config.h
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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 <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class ConfigImpl;
|
||||
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
Config(const std::string &path);
|
||||
~Config();
|
||||
|
||||
std::vector<std::string> sources() const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ConfigImpl> impl_;
|
||||
};
|
||||
} // namespace mirai
|
54
external/mirai-core-old/include/mirai-core/DateTime.h
vendored
Normal file
54
external/mirai-core-old/include/mirai-core/DateTime.h
vendored
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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <bits/chrono.h>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct Date {
|
||||
explicit Date(int year, unsigned month, unsigned day);
|
||||
|
||||
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp);
|
||||
explicit Date(std::chrono::year_month_day chronoDate);
|
||||
|
||||
bool operator==(const Date &other) const;
|
||||
bool operator<(const Date &other) const;
|
||||
bool operator>(const Date &other) const;
|
||||
|
||||
std::chrono::year_month_day toStdChrono() const
|
||||
{
|
||||
return std::chrono::year_month_day{
|
||||
std::chrono::year(year),
|
||||
std::chrono::month(month),
|
||||
std::chrono::day(day),
|
||||
};
|
||||
}
|
||||
|
||||
int year;
|
||||
unsigned month;
|
||||
unsigned day;
|
||||
};
|
||||
|
||||
struct Time {
|
||||
|
||||
bool operator==(const Time &other) const;
|
||||
bool operator<(const Time &other) const;
|
||||
bool operator>(const Time &other) const;
|
||||
|
||||
int hour;
|
||||
int minute;
|
||||
};
|
||||
|
||||
std::optional<mirai::Date> stringToDate(const std::string &dateStr);
|
||||
|
||||
} // namespace mirai
|
56
external/mirai-core-old/include/mirai-core/Day.h
vendored
Normal file
56
external/mirai-core-old/include/mirai-core/Day.h
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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 "DateTime.h"
|
||||
#include "Event.h"
|
||||
#include "TaskItem.h"
|
||||
#include "using.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class BaseSource;
|
||||
|
||||
struct DayData {
|
||||
Date date;
|
||||
std::vector<EventData> events;
|
||||
std::vector<TaskItemData> tasks;
|
||||
};
|
||||
|
||||
class Day
|
||||
{
|
||||
public:
|
||||
Day(BaseSource *source, const DayData &data);
|
||||
void setDate(const Date &date);
|
||||
|
||||
Event *createEvent(const EventData &eventData);
|
||||
TaskItem *createTask(const TaskItemData &taskData);
|
||||
void insertTask(std::unique_ptr<TaskItem> &&task);
|
||||
void deleteTask(const TaskItem &taskToDelete);
|
||||
std::optional<std::unique_ptr<TaskItem>> takeTask(const TaskItem &taskToDelete);
|
||||
void deleteEvent(const Event &eventToDelete);
|
||||
std::vector<std::unique_ptr<Event>> *events();
|
||||
std::vector<std::unique_ptr<TaskItem>> *tasks();
|
||||
|
||||
const Date &getDate() const;
|
||||
|
||||
Event *getEventById(int eventId);
|
||||
BaseSource *source();
|
||||
|
||||
private:
|
||||
void onChange();
|
||||
|
||||
BaseSource *source_;
|
||||
std::vector<std::unique_ptr<Event>> events_;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks_;
|
||||
DayData data_;
|
||||
};
|
||||
} // namespace mirai
|
68
external/mirai-core-old/include/mirai-core/Event.h
vendored
Normal file
68
external/mirai-core-old/include/mirai-core/Event.h
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* 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 "DateTime.h"
|
||||
#include "TaskItem.h"
|
||||
#include "using.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class BaseSource;
|
||||
|
||||
struct EventData {
|
||||
std::string description;
|
||||
Date date;
|
||||
Time startTime;
|
||||
Time endTime;
|
||||
Tags tags;
|
||||
std::vector<TaskItemData> tasks;
|
||||
};
|
||||
|
||||
class Day;
|
||||
|
||||
class Event
|
||||
{
|
||||
private:
|
||||
public:
|
||||
Event(BaseSource *source, Day *parent, const EventData &data);
|
||||
|
||||
Event &operator=(const EventData &newData);
|
||||
|
||||
void setText(const std::string &text);
|
||||
void setStartTime(const Time &time);
|
||||
void setEndTime(const Time &time);
|
||||
|
||||
TaskItem *createTask(const TaskItemData &taskData);
|
||||
void deleteTask(const TaskItem &taskToDelete);
|
||||
std::vector<std::unique_ptr<TaskItem>> *tasks();
|
||||
|
||||
const std::string &getText() const;
|
||||
const Date &getDate() const;
|
||||
const Time &getStartTime() const;
|
||||
const Time &getEndTime() const;
|
||||
const Tags &getTags() const;
|
||||
bool hasTag(const std::string &tag) const;
|
||||
|
||||
int id() const;
|
||||
Day *day();
|
||||
|
||||
private:
|
||||
void onChange();
|
||||
|
||||
static int nextId;
|
||||
int id_ = nextId++;
|
||||
Day *day_;
|
||||
EventData data;
|
||||
std::vector<std::unique_ptr<TaskItem>> tasks_;
|
||||
BaseSource *source_;
|
||||
};
|
||||
} // namespace mirai
|
28
external/mirai-core-old/include/mirai-core/EventEmitter.h
vendored
Normal file
28
external/mirai-core-old/include/mirai-core/EventEmitter.h
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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 <functional>
|
||||
#include <vector>
|
||||
|
||||
template <typename T> class EventEmitter
|
||||
{
|
||||
|
||||
public:
|
||||
void registerCallback(std::function<T> func)
|
||||
{
|
||||
callbacks.push_back(func);
|
||||
}
|
||||
|
||||
void emit(T data)
|
||||
{
|
||||
for (auto &callback : callbacks) {
|
||||
callback(data);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::function<T>> callbacks;
|
||||
};
|
45
external/mirai-core-old/include/mirai-core/Mirai.h
vendored
Normal file
45
external/mirai-core-old/include/mirai-core/Mirai.h
vendored
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* 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 "BaseFileSource.h"
|
||||
#include "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include "TodoMd.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class Mirai
|
||||
{
|
||||
|
||||
public:
|
||||
void loadSource(std::unique_ptr<BaseSource> &&resource);
|
||||
void unloadAllSources();
|
||||
void save();
|
||||
void deleteTask(const TaskItem &taskItem);
|
||||
std::optional<std::reference_wrapper<Source>> getSourceByName(const std::string &name);
|
||||
|
||||
std::vector<std::unique_ptr<BaseSource>> &getSources();
|
||||
const std::vector<std::string> &getTags();
|
||||
|
||||
void reloadTags();
|
||||
|
||||
// Returns a non owning pointer to the requested resource or nullptr if not found.
|
||||
BaseSource *getSourceById(int id);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseSource>> sources_;
|
||||
std::vector<std::string> tags_;
|
||||
};
|
||||
} // namespace mirai
|
29
external/mirai-core-old/include/mirai-core/StdFileSource.h
vendored
Normal file
29
external/mirai-core-old/include/mirai-core/StdFileSource.h
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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 "BaseFileSource.h"
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class StdFileSource : public BaseFileSource
|
||||
{
|
||||
public:
|
||||
StdFileSource(BaseFileSourceConstructor data) : BaseFileSource(data) {};
|
||||
StdFileSource(StdFileSource &) = delete;
|
||||
StdFileSource(StdFileSource &&) = delete;
|
||||
StdFileSource operator=(StdFileSource &) = delete;
|
||||
StdFileSource operator=(StdFileSource &&) = delete;
|
||||
|
||||
~StdFileSource() override = default;
|
||||
|
||||
void save() override;
|
||||
void load() override;
|
||||
;
|
||||
};
|
||||
} // namespace mirai
|
60
external/mirai-core-old/include/mirai-core/TaskItem.h
vendored
Normal file
60
external/mirai-core-old/include/mirai-core/TaskItem.h
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* 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 "using.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
class SourceDataProvider;
|
||||
class Day;
|
||||
|
||||
enum TaskItemState { TODO, DONE };
|
||||
|
||||
struct TaskItemData {
|
||||
std::string text;
|
||||
TaskItemState state;
|
||||
Tags tags;
|
||||
};
|
||||
|
||||
class TaskItem
|
||||
{
|
||||
public:
|
||||
TaskItem(SourceDataProvider *source, Day *day, const TaskItemData data);
|
||||
|
||||
TaskItem &operator=(const TaskItemData &newData);
|
||||
|
||||
void markAsDone();
|
||||
void markAsUndone();
|
||||
void setDate(const std::string &date);
|
||||
void setText(const std::string &text);
|
||||
|
||||
const std::string &getLine() const;
|
||||
const std::string &getText() const;
|
||||
const TaskItemState &getState() const;
|
||||
const Tags &getTags() const;
|
||||
bool hasTag(const std::string &tag) const;
|
||||
|
||||
int id() const;
|
||||
SourceDataProvider *source();
|
||||
Day *day();
|
||||
|
||||
private:
|
||||
void onChange();
|
||||
|
||||
static int nextId;
|
||||
int id_ = nextId++;
|
||||
TaskItemData data;
|
||||
|
||||
SourceDataProvider *source_;
|
||||
Day *day_;
|
||||
};
|
||||
|
||||
} // namespace mirai
|
61
external/mirai-core-old/include/mirai-core/TasksView.h
vendored
Normal file
61
external/mirai-core-old/include/mirai-core/TasksView.h
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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 "Mirai.h"
|
||||
#include "TaskItem.h"
|
||||
#include "using.h"
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct FilteredEvent {
|
||||
Event *event;
|
||||
std::vector<TaskItem *> filteredTasks;
|
||||
};
|
||||
|
||||
struct FilteredDay {
|
||||
Day *day;
|
||||
std::vector<FilteredEvent> filteredEvents;
|
||||
std::vector<TaskItem *> filteredTasks;
|
||||
};
|
||||
|
||||
class TasksView
|
||||
{
|
||||
|
||||
public:
|
||||
TasksView(Mirai *mirai);
|
||||
|
||||
FilteredDay &operator[](int index);
|
||||
std::vector<TaskItem *> &filteredUnscheduledTasks();
|
||||
|
||||
size_t count() const;
|
||||
void update();
|
||||
void addTagFilter(const std::string &tag);
|
||||
void removeTagFilter(const std::string &tag);
|
||||
void addSourceFilter(const std::string &fileName);
|
||||
void removeSourceFilter(const std::string &fileName);
|
||||
void removeFilters();
|
||||
const Tags &getActiveTagsFilter();
|
||||
const std::vector<std::string> &getActiveFilesFilter();
|
||||
bool isSourceFilterActive(const std::string &sourceName);
|
||||
|
||||
void hideCompletedTasks(bool hide);
|
||||
bool shouldHideCompletedTasks() const;
|
||||
|
||||
private:
|
||||
std::vector<FilteredDay> filteredDays;
|
||||
std::vector<TaskItem *> filteredUnscheduledTasks_;
|
||||
|
||||
Mirai *mirai;
|
||||
Tags tagsFilter;
|
||||
std::vector<std::string> resourcesFilter;
|
||||
bool shouldHideCompletedTasks_ = true;
|
||||
};
|
||||
} // namespace mirai
|
46
external/mirai-core-old/include/mirai-core/TodoMd.h
vendored
Normal file
46
external/mirai-core-old/include/mirai-core/TodoMd.h
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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 "BaseSource.h"
|
||||
#include "DateTime.h"
|
||||
#include "Day.h"
|
||||
#include "Event.h"
|
||||
#include "TaskItem.h"
|
||||
#include <format>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
struct MiraiMarkdownFormatParseResult {
|
||||
std::string name;
|
||||
std::vector<DayData> days;
|
||||
std::vector<TaskItemData> unscheduledTasks;
|
||||
};
|
||||
|
||||
class TodoMdFormat
|
||||
{
|
||||
public:
|
||||
static std::string stringify(BaseSource &source);
|
||||
|
||||
static MiraiMarkdownFormatParseResult parse(const std::string &content);
|
||||
|
||||
static std::string taskToString(const TaskItem &task);
|
||||
static TaskItemData stringToTask(const std::string &str, const std::string &date);
|
||||
static EventData stringToEvent(const std::string &str, const std::string &date);
|
||||
|
||||
private:
|
||||
static std::string fieldWithSpace(const std::string &field);
|
||||
static TaskItem parseLine(const std::string &line);
|
||||
static Tags extractTagsFromMetadata(std::string metadata);
|
||||
};
|
||||
} // namespace mirai
|
91
external/mirai-core-old/include/mirai-core/TodoTxt.h
vendored
Normal file
91
external/mirai-core-old/include/mirai-core/TodoTxt.h
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* 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 "TaskItem.h"*/
|
||||
/*#include "utils.h"*/
|
||||
/*#include <fstream>*/
|
||||
/*#include <iostream>*/
|
||||
/*#include <iterator>*/
|
||||
/*#include <memory>*/
|
||||
/*#include <regex>*/
|
||||
/*#include <stdexcept>*/
|
||||
/*#include <string>*/
|
||||
/*#include <vector>*/
|
||||
|
||||
/*namespace mirai {*/
|
||||
|
||||
/*class TodoTxtFormat {*/
|
||||
/*public:*/
|
||||
|
||||
/*static TaskList readFile() {*/
|
||||
/*std::ifstream file("../newqml/todo.txt");*/
|
||||
/*if (!file.is_open()) {*/
|
||||
/*throw std::runtime_error("todo.txt file not found");*/
|
||||
/*}*/
|
||||
/*std::vector<TaskItem> taskItems;*/
|
||||
/*std::string line;*/
|
||||
/*while (std::getline(file, line)) {*/
|
||||
/*auto taskItem = parseLine(line);*/
|
||||
/*taskItems.push_back(taskItem);*/
|
||||
/*}*/
|
||||
/*file.close();*/
|
||||
/*TaskList tasks({*/
|
||||
/*.tasks = taskItems*/
|
||||
/*});*/
|
||||
/*return tasks;*/
|
||||
/*}*/
|
||||
|
||||
/*static void writeFile(TaskList& tasks) {*/
|
||||
/*std::ofstream file("../newqml/todo.txt");*/
|
||||
/*if (!file.is_open()) {*/
|
||||
/*throw std::runtime_error("can't create todo.txt");*/
|
||||
/*}*/
|
||||
/*for (const auto& task : tasks.getTasks()) {*/
|
||||
/*file << fieldWithSpace(task->state == DONE ? "x" : task->priority);*/
|
||||
/*file << fieldWithSpace(task->state == DONE ? task->getDate() : "");*/
|
||||
/*file << fieldWithSpace(task->getCreationDate());*/
|
||||
/*file << task->getText() << '\n';*/
|
||||
/*}*/
|
||||
/*file.close();*/
|
||||
/*}*/
|
||||
|
||||
/*private:*/
|
||||
|
||||
/*static std::string fieldWithSpace(const std::string& field) {*/
|
||||
/*if (field.length() == 0)*/
|
||||
/*return "";*/
|
||||
/*return (field + " ");*/
|
||||
/*}*/
|
||||
|
||||
/*static TaskItem parseLine(const std::string& line) {*/
|
||||
/*std::smatch matches;*/
|
||||
/*std::regex regex("(^x )?(\\([A-Z]\\) )?([0-9]{4}-[0-9]{2}-[0-9]{2} )?([0-9]{4}-[0-9]{2}-[0-9]{2}
|
||||
* )?(.*)");*/
|
||||
/*std::regex_match(line, matches, regex);*/
|
||||
|
||||
/*for (size_t i = 0; i < matches.size(); ++i) {*/
|
||||
/*std::ssub_match sub_match = matches[i];*/
|
||||
/*std::string piece = sub_match.str();*/
|
||||
/*}*/
|
||||
|
||||
/*const TaskItemState taskState = trim_copy(matches[1].str()) == "x" ? DONE : TODO;*/
|
||||
/*const std::string priority = trim_copy(matches[2].str());*/
|
||||
/*const std::string firstDate = trim_copy(matches[3].str());*/
|
||||
/*const std::string secondDate = trim_copy(matches[4].str());*/
|
||||
/*const std::string text = trim_copy(matches[5].str());*/
|
||||
|
||||
/*const std::string date = taskState == DONE ? firstDate : "";*/
|
||||
|
||||
/*return {*/
|
||||
/*.text = text,*/
|
||||
/*.state = taskState,*/
|
||||
/*.date = date,*/
|
||||
/*};*/
|
||||
/*}*/
|
||||
/*};*/
|
||||
/*}*/
|
16
external/mirai-core-old/include/mirai-core/using.h
vendored
Normal file
16
external/mirai-core-old/include/mirai-core/using.h
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* 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 <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
using Tags = std::vector<std::string>;
|
||||
}
|
22
external/mirai-core-old/include/mirai-core/utils.h
vendored
Normal file
22
external/mirai-core-old/include/mirai-core/utils.h
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* 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 <algorithm>
|
||||
#include <cctype>
|
||||
#include <cpp-utils/string.h>
|
||||
#include <cpp-utils/vector.h>
|
||||
#include <locale>
|
||||
#include <regex>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
namespace stringUtils = cpputils::string;
|
||||
namespace vectorUtils = cpputils::vector;
|
||||
|
||||
bool isDate(const std::string &dateStr);
|
||||
} // namespace mirai
|
167
external/mirai-core-old/src/BaseSource.cpp
vendored
Normal file
167
external/mirai-core-old/src/BaseSource.cpp
vendored
Normal file
|
@ -0,0 +1,167 @@
|
|||
/*
|
||||
* 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 "BaseSource.h"
|
||||
#include "Day.h"
|
||||
#include "TaskItem.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void BaseSource::setDirty(bool shouldBeDirty)
|
||||
{
|
||||
isDirty_ = shouldBeDirty;
|
||||
}
|
||||
|
||||
bool BaseSource::isDirty() const
|
||||
{
|
||||
return isDirty_;
|
||||
}
|
||||
|
||||
Day *BaseSource::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>> *BaseSource::days()
|
||||
{
|
||||
return &days_;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TaskItem>> *BaseSource::unscheduledTasks()
|
||||
{
|
||||
return &unscheduledTasks_;
|
||||
}
|
||||
|
||||
void BaseSource::addDay(const DayData &dayData)
|
||||
{
|
||||
days_.push_back(std::make_unique<Day>(this, dayData));
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
void BaseSource::addUnscheduledTask(const TaskItemData &taskData)
|
||||
{
|
||||
unscheduledTasks_.push_back(std::make_unique<TaskItem>(this, nullptr, taskData));
|
||||
setDirty(true);
|
||||
}
|
||||
|
||||
TaskItem *BaseSource::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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto &task : *unscheduledTasks()) {
|
||||
if (task->id() == taskId) {
|
||||
return task.get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void BaseSource::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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unscheduledTasks_.erase(
|
||||
std::remove_if(
|
||||
unscheduledTasks_.begin(), unscheduledTasks_.end(),
|
||||
[&](const std::unique_ptr<TaskItem> &task) {
|
||||
return task->id() == taskToDelete.id();
|
||||
}
|
||||
),
|
||||
unscheduledTasks_.end()
|
||||
);
|
||||
}
|
||||
|
||||
Event *BaseSource::getEventById(int eventId)
|
||||
{
|
||||
for (auto &day : days_) {
|
||||
for (auto &event : *day->events()) {
|
||||
if (event->id() == eventId) {
|
||||
return event.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void BaseSource::setName(std::string name)
|
||||
{
|
||||
this->name_ = name;
|
||||
}
|
||||
|
||||
const std::string &BaseSource::getName() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
int BaseSource::id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
/*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 BaseSource::nextId_ = 0;
|
||||
|
||||
} // namespace mirai
|
30
external/mirai-core-old/src/Config.cpp
vendored
Normal file
30
external/mirai-core-old/src/Config.cpp
vendored
Normal 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
|
48
external/mirai-core-old/src/ConfigImpl.cpp
vendored
Normal file
48
external/mirai-core-old/src/ConfigImpl.cpp
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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 "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
ConfigImpl::ConfigImpl(const std::string &path) : path_(path)
|
||||
{
|
||||
std::ifstream file(path_);
|
||||
if (!file) {
|
||||
configJson_ = nlohmann::json::parse(R"(
|
||||
{
|
||||
"files": []
|
||||
}
|
||||
)");
|
||||
return;
|
||||
}
|
||||
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-old/src/ConfigImpl.h
vendored
Normal file
27
external/mirai-core-old/src/ConfigImpl.h
vendored
Normal 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
|
105
external/mirai-core-old/src/DateTime.cpp
vendored
Normal file
105
external/mirai-core-old/src/DateTime.cpp
vendored
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* 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));
|
||||
}
|
||||
|
||||
Date::Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
|
||||
{
|
||||
}
|
||||
|
||||
Date::Date(std::chrono::time_point<std::chrono::system_clock> tp)
|
||||
{
|
||||
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
|
||||
year = static_cast<int>(chronoDate.year());
|
||||
month = static_cast<unsigned>(chronoDate.month());
|
||||
day = static_cast<unsigned>(chronoDate.day());
|
||||
}
|
||||
|
||||
Date::Date(std::chrono::year_month_day chronoDate)
|
||||
{
|
||||
year = static_cast<int>(chronoDate.year());
|
||||
month = static_cast<unsigned>(chronoDate.month());
|
||||
day = static_cast<unsigned>(chronoDate.day());
|
||||
}
|
||||
|
||||
int year;
|
||||
unsigned month;
|
||||
unsigned day;
|
||||
|
||||
bool Date::operator==(const Date &other) const
|
||||
{
|
||||
return other.year == year && other.month == month && other.day == day;
|
||||
}
|
||||
|
||||
bool Date::operator<(const Date &other) const
|
||||
{
|
||||
if (year < other.year) {
|
||||
return true;
|
||||
}
|
||||
if (year == other.year && month < other.month) {
|
||||
return true;
|
||||
}
|
||||
if (year == other.year && month == other.month && day < other.day) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Date::operator>(const Date &other) const
|
||||
{
|
||||
return !(*this < other) && !(*this == other);
|
||||
}
|
||||
|
||||
bool Time::operator==(const Time &other) const
|
||||
{
|
||||
return other.hour == hour && other.minute == minute;
|
||||
}
|
||||
|
||||
bool Time::operator<(const Time &other) const
|
||||
{
|
||||
if (hour < other.hour) {
|
||||
return true;
|
||||
}
|
||||
if (hour == other.hour && minute < other.minute) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Time::operator>(const Time &other) const
|
||||
{
|
||||
return !(*this < other) && !(*this == other);
|
||||
}
|
||||
} // namespace mirai
|
126
external/mirai-core-old/src/Day.cpp
vendored
Normal file
126
external/mirai-core-old/src/Day.cpp
vendored
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* 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 "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Day::Day(BaseSource *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::insertTask(std::unique_ptr<TaskItem> &&task)
|
||||
{
|
||||
tasks_.push_back(std::move(task));
|
||||
onChange();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
std::optional<std::unique_ptr<TaskItem>> Day::takeTask(const TaskItem &taskToDelete)
|
||||
{
|
||||
auto taskIterator = std::ranges::find_if(tasks_, [&](const auto &task) {
|
||||
return task->id() == taskToDelete.id();
|
||||
});
|
||||
if (taskIterator == tasks_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto task = std::move(*taskIterator);
|
||||
tasks_.erase(taskIterator);
|
||||
onChange();
|
||||
return task;
|
||||
}
|
||||
|
||||
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_;
|
||||
}
|
||||
|
||||
Event *Day::getEventById(int eventId)
|
||||
{
|
||||
for (auto &event : events_) {
|
||||
if (event->id() == eventId) {
|
||||
return event.get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
BaseSource *Day::source()
|
||||
{
|
||||
return source_;
|
||||
}
|
||||
} // namespace mirai
|
116
external/mirai-core-old/src/Event.cpp
vendored
Normal file
116
external/mirai-core-old/src/Event.cpp
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* 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 "BaseSource.h"
|
||||
#include "TaskItem.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
Event::Event(BaseSource *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);
|
||||
}
|
||||
|
||||
Event &Event::operator=(const EventData &newData)
|
||||
{
|
||||
data = newData;
|
||||
onChange();
|
||||
return *this;
|
||||
};
|
||||
|
||||
int Event::id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
Day *Event::day()
|
||||
{
|
||||
return day_;
|
||||
}
|
||||
|
||||
int Event::nextId = 0;
|
||||
|
||||
} // namespace mirai
|
7
external/mirai-core-old/src/EventEmitter.cpp
vendored
Normal file
7
external/mirai-core-old/src/EventEmitter.cpp
vendored
Normal 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"
|
100
external/mirai-core-old/src/Mirai.cpp
vendored
Normal file
100
external/mirai-core-old/src/Mirai.cpp
vendored
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* 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::loadSource(std::unique_ptr<BaseSource> &&resource)
|
||||
{
|
||||
resource->load();
|
||||
sources_.push_back(std::move(resource));
|
||||
reloadTags();
|
||||
};
|
||||
|
||||
void Mirai::unloadAllSources()
|
||||
{
|
||||
sources_.clear();
|
||||
}
|
||||
|
||||
void Mirai::save()
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
if (resource->isDirty()) {
|
||||
resource->save();
|
||||
resource->setDirty(false);
|
||||
}
|
||||
}
|
||||
reloadTags();
|
||||
}
|
||||
|
||||
void Mirai::deleteTask(const TaskItem &taskItem)
|
||||
{
|
||||
for (auto &resource : sources_) {
|
||||
resource->deleteTask(taskItem);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<BaseSource>> &Mirai::getSources()
|
||||
{
|
||||
return sources_;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<BaseSource>> Mirai::getSourceByName(const std::string &name)
|
||||
{
|
||||
auto resourceIterator =
|
||||
std::ranges::find_if(sources_, [&](const std::unique_ptr<BaseSource> &resource) {
|
||||
return resource->getName() == name;
|
||||
});
|
||||
|
||||
if (resourceIterator == sources_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *(resourceIterator->get());
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Mirai::getTags()
|
||||
{
|
||||
return tags_;
|
||||
}
|
||||
|
||||
void Mirai::reloadTags()
|
||||
{
|
||||
// TODO TAGS
|
||||
/*cpputils::debug::Timer reloadingTagsDuration;*/
|
||||
/*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");*/
|
||||
}
|
||||
|
||||
BaseSource *Mirai::getSourceById(int id)
|
||||
{
|
||||
if (id >= sources_.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
return sources_.at(id).get();
|
||||
}
|
||||
|
||||
} // namespace mirai
|
61
external/mirai-core-old/src/StdFileSource.cpp
vendored
Normal file
61
external/mirai-core-old/src/StdFileSource.cpp
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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 "StdFileSource.h"
|
||||
#include "TodoMd.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
void StdFileSource::save()
|
||||
{
|
||||
std::ofstream file(getPath());
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("can't create " + getPath());
|
||||
}
|
||||
|
||||
const std::string content = TodoMdFormat::stringify(*this);
|
||||
|
||||
file << content;
|
||||
file.close();
|
||||
};
|
||||
|
||||
void StdFileSource::load()
|
||||
{
|
||||
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 &dayData : result.days) {
|
||||
Day *newDay = day(dayData.date);
|
||||
for (const auto &eventData : dayData.events) {
|
||||
Event *newEvent = newDay->createEvent(eventData);
|
||||
for (const auto &taskData : eventData.tasks) {
|
||||
TaskItem *newTask = newEvent->createTask(taskData);
|
||||
}
|
||||
}
|
||||
for (const auto &taskData : dayData.tasks) {
|
||||
TaskItem *newTask = newDay->createTask(taskData);
|
||||
}
|
||||
}
|
||||
for (const auto &taskData : result.unscheduledTasks) {
|
||||
addUnscheduledTask(taskData);
|
||||
}
|
||||
setName(result.name);
|
||||
};
|
||||
} // namespace mirai
|
87
external/mirai-core-old/src/TaskItem.cpp
vendored
Normal file
87
external/mirai-core-old/src/TaskItem.cpp
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* 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 "BaseSource.h"
|
||||
#include "utils.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
int TaskItem::nextId = 0;
|
||||
|
||||
TaskItem &TaskItem::operator=(const TaskItemData &newData)
|
||||
{
|
||||
data = newData;
|
||||
onChange();
|
||||
return *this;
|
||||
};
|
||||
|
||||
TaskItem::TaskItem(BaseSource *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);
|
||||
}
|
||||
|
||||
int TaskItem::id() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
BaseSource *TaskItem::source()
|
||||
{
|
||||
return source_;
|
||||
}
|
||||
|
||||
Day *TaskItem::day()
|
||||
{
|
||||
return day_;
|
||||
}
|
||||
|
||||
} // namespace mirai
|
212
external/mirai-core-old/src/TasksView.cpp
vendored
Normal file
212
external/mirai-core-old/src/TasksView.cpp
vendored
Normal file
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
std::vector<TaskItem *> &TasksView::filteredUnscheduledTasks()
|
||||
{
|
||||
return filteredUnscheduledTasks_;
|
||||
}
|
||||
|
||||
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();
|
||||
filteredUnscheduledTasks_.clear();
|
||||
|
||||
for (auto &file : mirai->getSources()) {
|
||||
if (resourcesFilter.size() > 0 &&
|
||||
!vectorUtils::contains(resourcesFilter, file->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto &task : *file->unscheduledTasks()) {
|
||||
if (shouldHideCompletedTasks() && task->getState() == DONE) {
|
||||
continue;
|
||||
}
|
||||
if (tagsFilter.size() > 0 && !vectorUtils::containsAll(tagsFilter, task->getTags())) {
|
||||
continue;
|
||||
}
|
||||
filteredUnscheduledTasks_.push_back(task.get());
|
||||
}
|
||||
for (auto &day : *file->days()) {
|
||||
FilteredDay filteredDay{.day = day.get()};
|
||||
for (auto &task : *day->tasks()) {
|
||||
auto taskDate = std::format(
|
||||
"{:04d}-{:02d}-{:02d}", day->getDate().year, day->getDate().month,
|
||||
day->getDate().day
|
||||
);
|
||||
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(
|
||||
"{:04d}-{:02d}-{:02d}", 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() || !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()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &filteredDay : filteredDays) {
|
||||
std::ranges::sort(
|
||||
filteredDay.filteredEvents,
|
||||
[](const FilteredEvent &t1, const FilteredEvent &t2) {
|
||||
return t1.event->getStartTime().hour < t2.event->getStartTime().hour;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
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::addSourceFilter(const std::string &fileName)
|
||||
{
|
||||
resourcesFilter.push_back(fileName);
|
||||
update();
|
||||
}
|
||||
|
||||
void TasksView::removeSourceFilter(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();
|
||||
resourcesFilter.clear();
|
||||
update();
|
||||
}
|
||||
|
||||
const std::vector<std::string> &TasksView::getActiveTagsFilter()
|
||||
{
|
||||
return tagsFilter;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &TasksView::getActiveFilesFilter()
|
||||
{
|
||||
return resourcesFilter;
|
||||
}
|
||||
|
||||
bool TasksView::isSourceFilterActive(const std::string &sourceName)
|
||||
{
|
||||
if (std::ranges::find(getActiveFilesFilter(), sourceName) == getActiveFilesFilter().end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TasksView::hideCompletedTasks(bool hide)
|
||||
{
|
||||
shouldHideCompletedTasks_ = hide;
|
||||
}
|
||||
|
||||
bool TasksView::shouldHideCompletedTasks() const
|
||||
{
|
||||
return shouldHideCompletedTasks_;
|
||||
}
|
||||
} // namespace mirai
|
239
external/mirai-core-old/src/TodoMd.cpp
vendored
Normal file
239
external/mirai-core-old/src/TodoMd.cpp
vendored
Normal file
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* 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(BaseSource &source)
|
||||
{
|
||||
const std::string &name = source.getName();
|
||||
const std::vector<std::unique_ptr<Day>> *days = source.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';
|
||||
}
|
||||
|
||||
if (source.unscheduledTasks()->size() > 0) {
|
||||
result += "## Unscheduled\n\n";
|
||||
for (const auto &task : *(source.unscheduledTasks())) {
|
||||
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;
|
||||
std::vector<mirai::TaskItemData> unscheduledTasks;
|
||||
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);
|
||||
if (currentDateString == "Unscheduled") {
|
||||
currentDay = nullptr;
|
||||
continue;
|
||||
}
|
||||
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 = ¤tDay->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 {
|
||||
unscheduledTasks.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, .unscheduledTasks = unscheduledTasks};
|
||||
}
|
||||
} // namespace mirai
|
38
external/mirai-core-old/src/utils.cpp
vendored
Normal file
38
external/mirai-core-old/src/utils.cpp
vendored
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue