-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.cpp
More file actions
152 lines (117 loc) · 3.06 KB
/
Copy pathdocument.cpp
File metadata and controls
152 lines (117 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "document.h"
#include <LittleFS.h>
#include <algorithm>
const char* DOCUMENT_PATH = "/document.md";
const char* DEFAULT_DOCUMENT_TEXT =
"# ESP32 Typewriter Draft\n\n"
"This is the default document text.\n"
"Not actually read from the file.";
Document document;
bool Document::begin() {
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed.");
currentPath = "";
text = "";
return false;
}
if (listDocuments().empty()) {
Serial.println("No document found. Creating default document.");
currentPath = DOCUMENT_PATH;
text = DEFAULT_DOCUMENT_TEXT;
save();
}
return true;
}
std::vector<String> Document::listDocuments() const {
std::vector<String> documents;
File root = LittleFS.open("/");
if (!root) {
return documents;
}
File file = root.openNextFile();
while (file) {
if (!file.isDirectory()) {
String path = file.name();
if (!path.startsWith("/")) {
path = "/" + path;
}
if (path.endsWith(".md")) {
documents.push_back(path);
}
}
file = root.openNextFile();
}
std::sort(documents.begin(), documents.end(), [](const String& left, const String& right) {
String leftLower = left;
String rightLower = right;
leftLower.toLowerCase();
rightLower.toLowerCase();
return leftLower < rightLower;
});
return documents;
}
bool Document::open(const String& path) {
File file = LittleFS.open(path, "r");
if (!file) {
Serial.print("Failed to open document: ");
Serial.println(path);
return false;
}
text = file.readString();
file.close();
currentPath = path;
return true;
}
bool Document::createNew() {
String path = "/Untitled.md";
uint8_t index = 2;
while (LittleFS.exists(path)) {
path = "/Untitled " + String(index) + ".md";
index++;
}
currentPath = path;
text = "# Untitled\n\n";
return save();
}
const String& Document::getText() const {
return text;
}
const String& Document::getPath() const {
return currentPath;
}
String Document::getTitle() const {
if (currentPath.length() == 0) {
return "(no document)";
}
String title = currentPath;
if (title.startsWith("/")) {
title.remove(0, 1);
}
if (title.endsWith(".md")) {
title.remove(title.length() - 3);
}
return title;
}
void Document::insertChar(char c) {
text += c;
}
void Document::backspace() {
if (text.length() > 0) {
text.remove(text.length() - 1);
}
}
bool Document::save() {
if (currentPath.length() == 0) {
Serial.println("No document selected.");
return false;
}
File file = LittleFS.open(currentPath, "w");
if (!file) {
Serial.println("Failed to open document for writing.");
return false;
}
file.print(text);
file.close();
Serial.println("Document saved.");
return true;
}