-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
847 lines (796 loc) · 43.8 KB
/
main.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
* FTXUI Libraries
*/
#include "ftxui/component/component.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/component/component_options.hpp"
/*
* System libraries
*/
#include <sstream>
#include <filesystem>
#include <algorithm>
/*
* Own module libraries
*/
#include "Utilities.h"
#include "ValidationCheck.h"
#include "StateTracker.h"
#include "WindowsUtilities.h"
#include "MarkdownParser.h"
#include "SpecialComponents/SpecialCheckbox.h"
/*
* Personal data structures
*/
struct ApplicationMetaData {
std::filesystem::path filePath;
std::shared_ptr<std::string> task_header_ref;
std::vector<std::shared_ptr<bool>> checkbox_statuses;
std::vector<std::shared_ptr<std::string>> checkbox_labels;
std::vector<std::shared_ptr<std::wstring>> checkbox_comments;
// Singleton instance
static ApplicationMetaData &instance() {
static ApplicationMetaData instance;
return instance;
}
private:
// Private constructor for singleton
ApplicationMetaData() {}
};
typedef struct markdownFile_ {
std::filesystem::path filePath;
std::string fileName;
bool isParseable{};
} markdownFile;
/*
* Personal methods
*/
void HandleSavingEvent(const ApplicationMetaData &data) {
static const std::regex startingCommentPrefix("^\\s*=>");
static const std::regex play_symbol(u8"▶️");
static const std::regex pause_symbol(u8"⏸️");
std::string completeFileContent;
std::string taskLabel;
bool foundTaskStatus = false;
auto filePathToSave = data.filePath;
std::ofstream fileOutputStream(filePathToSave, std::ios::out);
if (!fileOutputStream) {
// Handle the error.
std::cerr << "Failed to open the file for writing: " << filePathToSave << std::endl;
}
completeFileContent += "# " + *(data.task_header_ref) + "\n\n";
for (size_t i = 0; i < data.checkbox_labels.size(); i++) {
completeFileContent += *(data.checkbox_statuses[i]) ? "- [x] " : "- [ ] ";
taskLabel = *data.checkbox_labels.at(i);
if (!foundTaskStatus) {
if (std::regex_search(taskLabel, play_symbol)) {
taskLabel = std::regex_replace(taskLabel, play_symbol, "");
foundTaskStatus = true;
} else if (std::regex_search(taskLabel, pause_symbol)) {
taskLabel = std::regex_replace(taskLabel, pause_symbol, "");
foundTaskStatus = true;
}
}
completeFileContent += taskLabel;
completeFileContent += "\n";
if (!(*data.checkbox_comments.at(i)).empty()) {
auto comment = convertToStandardString(*data.checkbox_comments.at(i));
auto replacedComment = std::regex_replace(comment, startingCommentPrefix, "-");
completeFileContent += replacedComment;
completeFileContent += "\n";
}
}
if (!completeFileContent.empty() && completeFileContent.back() == '\n') {
completeFileContent.pop_back();
}
fileOutputStream << completeFileContent;
if (!fileOutputStream) {
// Handle the error.
std::cerr << "Failed to write to the file: " << filePathToSave << std::endl;
}
fileOutputStream.close();
if (!fileOutputStream) {
// Handle the error.
std::cerr << "Failed to close the file: " << filePathToSave << std::endl;
}
// Get the path to the TEMP folder
auto tempFolderPath = std::filesystem::temp_directory_path();
std::string config_file = "postopek_files.config";
// Construct the full path to the file in the TEMP folder
std::filesystem::path configFilePath = tempFolderPath / config_file;
// Check if the file exists and it is a regular file
if (!std::filesystem::is_regular_file(configFilePath)) {
throw std::runtime_error("Not a valid file! Run first_run.bat first!");
}
std::ifstream file(configFilePath);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: " + configFilePath.string());
}
std::string line;
bool seenFileBefore = false; // By default, this is a new file
while (std::getline(file, line)) {
// Find the position of the '=' character
size_t delimiterPos = line.find('=');
if (delimiterPos != std::string::npos) {
// Extract the key and value from the line
std::string lineKey = line.substr(0, delimiterPos);
std::string lineValue = line.substr(delimiterPos + 1);
trimStringInPlace(lineKey);
trimStringInPlace(lineValue);
if (lineKey == "FILE") {
auto visitedPath = std::filesystem::path(lineValue);
if (filePathToSave == visitedPath) {
seenFileBefore = true;
break;
}
}
}
}
file.close();
if (!seenFileBefore) {
// This is a new file (saved for the first time)
std::ofstream fileOutput(configFilePath, std::ios::app);
if (!fileOutput) {
// Handle the error.
std::cerr << "Failed to open the file for writing: " << configFilePath << std::endl;
}
fileOutput << "FILE=";
fileOutput << filePathToSave.string();
fileOutput << "\n";
if (!fileOutput) {
// Handle the error.
std::cerr << "Failed to write to the file: " << configFilePath << std::endl;
}
fileOutput.close();
}
}
void removeLineWithFilePath(const std::string &opened_file) {
// Get the path to the TEMP folder
auto tempFolderPath = std::filesystem::temp_directory_path();
std::string config_file = "postopek_files.config";
// Construct the full path to the file in the TEMP folder
std::filesystem::path configFilePath = tempFolderPath / config_file;
std::ifstream file_in(configFilePath);
std::vector<std::string> lines;
std::string line_basic;
if (file_in.is_open()) {
while (std::getline(file_in, line_basic)) {
if (line_basic.find(opened_file) != std::string::npos) {
// found file path to remove
continue;
}
lines.push_back(line_basic);
}
file_in.close();
}
std::ofstream file_out(configFilePath);
if (file_out.is_open()) {
for (const auto &line: lines) {
file_out << line << "\n";
}
file_out.close();
}
}
std::vector<FileContainer> loadMarkdownContainers(std::vector<markdownFile> &markdowns) {
std::vector<FileContainer> containers;
size_t parseableMarkdowns = std::count_if(markdowns.begin(), markdowns.end(), [](const markdownFile &m) {
return m.isParseable;
});
containers.reserve(parseableMarkdowns);
for (const auto &f: markdowns) {
if (!f.isParseable) { continue; }
containers.emplace_back(f.filePath);
containers.back().Parse();
}
return containers;
}
std::vector<markdownFile> getMarkdownVector(std::vector<std::filesystem::path> &mdPaths) {
std::vector<markdownFile> markdowns;
markdowns.reserve(mdPaths.size());
// Set file names to entries
for (const auto &path: mdPaths) {
markdownFile m;
m.filePath = path;
m.fileName = path.filename().string();
m.isParseable = isValidMarkdownFile(path.string());
markdowns.push_back(m);
}
return markdowns;
}
int getTaskNumber(const std::smatch &matches) {
std::string taskNumberString = matches.str(1);
int taskNumber = std::stoi(taskNumberString);
return taskNumber;
}
bool isGoodTaskNumber(const std::smatch &matches, int start, int end) {
int taskNumber = getTaskNumber(matches);
return ((taskNumber >= start) && (taskNumber <= end));
}
/*
* Main method (start point)
*/
int main() {
// Initialization steps
// Init Screen
auto screen = ftxui::ScreenInteractive::Fullscreen();
StateTracker &stateTracker = StateTracker::getInstance();
// Static constants
static const std::regex timestamp_regex("\\[[0-1][0-9]:[0-5][0-9] (AM|PM)\\]");
// Read from config file
std::vector<std::filesystem::path> mdPaths;
std::vector<markdownFile_> markdowns;
std::vector<FileContainer> markDownContainers;
std::deque<bool> previouslyUnseenFiles;
std::tie(mdPaths, previouslyUnseenFiles) = checkTempFileAndGetFiles();
markdowns = getMarkdownVector(mdPaths);
// Variables
auto &focus_selector = stateTracker.getFocusSelector();
focus_selector = 0;
auto &menu_selector = stateTracker.getMenuSelector();
menu_selector = -1;
auto &show_saved_status = stateTracker.getSaveStatusIndicator();
show_saved_status = false;
auto &incorrect_input_shortcut_indicator = stateTracker.getInputParseStatusIndicator();
incorrect_input_shortcut_indicator = false;
auto &incorrect_input_indicator = stateTracker.getInputValidationStatusIndicator();
incorrect_input_shortcut_indicator = false;
auto &file_modified_flag = stateTracker.getFileModifiedFlag();
file_modified_flag = false;
auto &file_save_check_flag = stateTracker.getConfirmQuitStatusIndicator();
file_save_check_flag = false;
auto &move_to_menu_save_flag = stateTracker.getMenuSaveFlagIndicator();
move_to_menu_save_flag = false;
auto task_status_flag = std::make_shared<std::bitset<2>>(stateTracker.getTaskStatusFlagIndicator());
(*task_status_flag).reset();
// UI Divisions
// Menu related
std::vector<std::string> menuEntries;
std::vector<bool> statusFlags;
std::vector<std::shared_ptr<bool>> shouldStartFreshStatusFlags;
ftxui::Components startFreshCheckboxes;
auto menuContainer = ftxui::Container::Vertical({});
auto statusContainer = ftxui::Container::Vertical({});
auto startFreshContainer = ftxui::Container::Vertical({});
for (auto i = 0; i < markdowns.size(); i++) {
auto &mdStruct = markdowns[i];
menuEntries.push_back(mdStruct.fileName);
statusFlags.push_back(mdStruct.isParseable);
// by default, start fresh
shouldStartFreshStatusFlags.push_back(std::make_shared<bool>(previouslyUnseenFiles[i]));
auto status = ftxui::Renderer([&mdStruct] {
if (mdStruct.isParseable) {
return ftxui::text("🟢");
} else {
return ftxui::text("⛔");
}
});
statusContainer->Add(status);
}
for (auto &statusFlag: shouldStartFreshStatusFlags) {
startFreshCheckboxes.push_back(ftxui::Checkbox("", statusFlag.get()));
}
for (auto &cb: startFreshCheckboxes) {
startFreshContainer->Add(cb);
}
// Time Renderer
auto timeRenderer = ftxui::Renderer([&] {
std::string time_str = getCurrentTime();
return ftxui::text(time_str)
| color(ftxui::Color::CornflowerBlue)
| ftxui::bold
| ftxui::center
| ftxui::border;
});
// Task UI
auto taskComponentContainer = ftxui::Container::Vertical({});
std::shared_ptr<std::string> task_header;
auto taskHeaderContainer = ftxui::Renderer([&task_header] {
return task_header->empty() ? ftxui::nothing(ftxui::text(""))
: (ftxui::text(" " + *task_header + " ") | ftxui::border | ftxui::inverted |
ftxui::center);
});
auto on_change = [&file_modified_flag]() {
file_modified_flag = true;
};
std::vector<std::shared_ptr<std::string>> checkbox_labels;
std::vector<std::shared_ptr<std::wstring>> checkbox_comments;
std::vector<std::shared_ptr<bool>> checkbox_status, checkbox_show_comment_status;
std::vector<std::shared_ptr<int>> iteration_range_values;
// Define the callback function
auto fileSelectorCallback = [&]() {
if (!statusFlags.at(menu_selector)) {
focus_selector = 2;
} else {
taskComponentContainer->DetachAllChildren();
markDownContainers = loadMarkdownContainers(markdowns);
auto focused_file_container = markDownContainers.at(menu_selector);
auto currentContainerSize = focused_file_container.getTasks().size();
auto startFreshOption = *(shouldStartFreshStatusFlags.at(menu_selector));
checkbox_labels.clear();
checkbox_comments.clear();
checkbox_show_comment_status.clear();
iteration_range_values.clear();
checkbox_status.clear();
task_header = std::make_shared<std::string>(focused_file_container.getHeader());
size_t i;
taskComponentContainer->Add(taskHeaderContainer);
for (i = 0; i < currentContainerSize; i++) {
checkbox_labels.emplace_back(std::make_shared<std::string>(focused_file_container.getTasks()[i]));
checkbox_comments.emplace_back(
std::make_shared<std::wstring>(convertToWideString(focused_file_container.getComments()[i])));
if (!startFreshOption) {
// do not start fresh
checkbox_status.push_back(std::make_shared<bool>(focused_file_container.getStatus()[i]));
} else {
// start fresh
checkbox_status.push_back(std::make_shared<bool>(false));
}
checkbox_show_comment_status.push_back(std::make_shared<bool>(false));
iteration_range_values.push_back(std::make_shared<int>(i + 1));
}
for (i = 0; i < currentContainerSize; i++) {
auto checkbox_option = ftxui::CheckboxOption();
auto checkbox_status_ptr = checkbox_status[i];
auto label_ptr = checkbox_labels[i];
if (startFreshOption && label_ptr && label_ptr->length() > 0) {
/* Start fresh and check if string has timestamp already
* If is freshly started, replace timestamp with nothing
*/
if (std::regex_search(*label_ptr, timestamp_regex)) {
// Replace the pattern with an empty string
*label_ptr = std::regex_replace(*label_ptr, timestamp_regex, "");
}
}
auto iter_value_ptr = iteration_range_values[i];
checkbox_option.transform = [label_ptr, checkbox_status_ptr, iter_value_ptr](auto &&PH1) {
return CheckboxDecorator(label_ptr, checkbox_status_ptr, iter_value_ptr,
std::forward<decltype(PH1)>(PH1));
};
checkbox_option.on_change = on_change;
auto cb = ftxui::Checkbox(checkbox_labels[i].get(), checkbox_status[i].get(), checkbox_option);
auto cb_show_comment_status_ptr = checkbox_show_comment_status[i];
auto specialCb = std::make_shared<SpecialCheckbox>(cb, cb_show_comment_status_ptr, label_ptr,
task_status_flag);
taskComponentContainer->Add(specialCb);
auto hover_text_ptr = checkbox_comments[i];
auto hover_text_renderer = ftxui::Renderer([cb_show_comment_status_ptr, hover_text_ptr]() {
auto &hovered_status = *cb_show_comment_status_ptr;
auto &hover_text = *hover_text_ptr;
if (hovered_status) {
// Convert hover_text from std::wstring to std::string.
std::string hover_text_str = std::string(hover_text.begin(), hover_text.end());
std::vector<std::string> lines;
std::istringstream iss(hover_text_str);
for (std::string line; std::getline(iss, line);) {
lines.push_back(line);
}
std::vector<ftxui::Element> elements;
elements.reserve(lines.size());
for (const auto &line: lines) {
elements.push_back(ftxui::text(line) | color(ftxui::Color::Red) | ftxui::bold);
}
return hover_text_str.empty() ? ftxui::nothing(ftxui::text("")) : (ftxui::vbox(elements) |
ftxui::focus |
ftxui::border |
ftxui::center);
} else {
return ftxui::nothing(ftxui::text(""));
}
});
taskComponentContainer->Add(hover_text_renderer);
}
focus_selector = 1;
}
};
// Create the MenuOption object
ftxui::MenuOption file_menu_option;
file_menu_option.on_enter = fileSelectorCallback;
menuContainer->Add(ftxui::Menu(&menuEntries, &menu_selector, file_menu_option));
// Input Box and Update button
std::shared_ptr<std::string> content = std::make_shared<std::string>();
auto input_option = ftxui::InputOption();
auto on_enter_wildcard = [content, &input_option, &incorrect_input_shortcut_indicator]() {
std::regex newTaskPattern("ant (\\d+)");
std::regex newCommentPattern("acc (\\d+)");
std::regex changeTaskPattern("ctt (\\d+)");
std::regex wildCardPattern("<!.+>");
std::smatch matches;
bool wildCardSet = false;
if (std::regex_search(*content, matches, wildCardPattern)) {
wildCardSet = true;
incorrect_input_shortcut_indicator = true;
// wait for 2 seconds
std::thread([&incorrect_input_shortcut_indicator]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
incorrect_input_shortcut_indicator = false;
}).detach();
}
if (!wildCardSet) {
if (std::regex_search(*content, matches, newTaskPattern)) {
*content = std::regex_replace(*content, newTaskPattern, "<!add-new-task-$1>: ");
} else if (std::regex_search(*content, matches, newCommentPattern)) {
*content = std::regex_replace(*content, newCommentPattern, "<!add-new-comment-$1>: ");
} else if (std::regex_search(*content, matches, changeTaskPattern)) {
*content = std::regex_replace(*content, changeTaskPattern, "<!change-task-$1>: ");
}
// Place the cursor after the size of content.
input_option.cursor_position = static_cast<int>(content->size());
}
};
input_option.on_enter = on_enter_wildcard;
auto input_component = ftxui::Input(content.get(), "Enter text...", &input_option);
std::wstring hover_text;
auto onUpdate = [&content, &iteration_range_values, &checkbox_comments,
&checkbox_labels, &taskComponentContainer, &checkbox_show_comment_status,
&checkbox_status, &incorrect_input_indicator, &file_modified_flag, &task_status_flag] {
std::regex newTaskPattern("<!add-new-task-(\\d+)>:\\s*(.*)");
std::regex newCommentPattern("<!add-new-comment-(\\d+)>:\\s*(.*)");
std::regex changeTaskPattern("<!change-task-(\\d+)>:\\s*(.*)");
std::smatch matches;
if (std::regex_search(*content, matches, newTaskPattern) && matches.size() > 1) {
if (!isGoodTaskNumber(matches, *iteration_range_values.front(), *iteration_range_values.back())) { return; }
// get task number
int taskNumber = getTaskNumber(matches);
auto newTaskContent = matches.str(2);
// insert new item in lists
checkbox_labels.insert(checkbox_labels.begin() + taskNumber, std::make_shared<std::string>(newTaskContent));
checkbox_comments.insert(checkbox_comments.begin() + taskNumber, std::make_shared<std::wstring>());
checkbox_status.insert(checkbox_status.begin() + taskNumber, std::make_shared<bool>(false));
checkbox_show_comment_status.insert(checkbox_show_comment_status.begin() + taskNumber,
std::make_shared<bool>(false));
if (taskNumber == checkbox_labels.size() - 1) {
iteration_range_values.push_back(
std::make_shared<int>(iteration_range_values.size() + 1));
} else {
iteration_range_values.insert(iteration_range_values.begin() + taskNumber,
std::make_shared<int>(taskNumber + 1));
for (int i = taskNumber + 1; i < iteration_range_values.size(); i++) {
*iteration_range_values[i] += 1;
}
}
// remove and add new element in Task container
auto checkbox_option = ftxui::CheckboxOption();
auto checkbox_status_ptr = checkbox_status[taskNumber];
auto label_ptr = checkbox_labels[taskNumber];
auto iter_value_ptr = iteration_range_values[taskNumber];
checkbox_option.transform = [label_ptr, checkbox_status_ptr, iter_value_ptr](auto &&PH1) {
return CheckboxDecorator(label_ptr, checkbox_status_ptr, iter_value_ptr,
std::forward<decltype(PH1)>(PH1));
};
auto cb = ftxui::Checkbox(checkbox_labels[taskNumber].get(), checkbox_status[taskNumber].get(),
checkbox_option);
auto cb_show_comment_status_ptr = checkbox_show_comment_status[taskNumber];
auto specialCb = std::make_shared<SpecialCheckbox>(cb, cb_show_comment_status_ptr, label_ptr,
task_status_flag);
auto hover_text_ptr = checkbox_comments[taskNumber];
auto hover_text_renderer = ftxui::Renderer([cb_show_comment_status_ptr, hover_text_ptr]() {
auto &hovered_status = *cb_show_comment_status_ptr;
auto &hover_text = *hover_text_ptr;
if (hovered_status) {
// Convert hover_text from std::wstring to std::string.
std::string hover_text_str = std::string(hover_text.begin(), hover_text.end());
std::vector<std::string> lines;
std::istringstream iss(hover_text_str);
for (std::string line; std::getline(iss, line);) {
lines.push_back(line);
}
std::vector<ftxui::Element> elements;
elements.reserve(lines.size());
for (const auto &line: lines) {
elements.push_back(ftxui::text(line) | color(ftxui::Color::Red) | ftxui::bold);
}
return hover_text_str.empty() ? ftxui::nothing(ftxui::text("")) : (ftxui::vbox(elements) |
ftxui::focus |
ftxui::border |
ftxui::center);
} else {
return ftxui::nothing(ftxui::text(""));
}
});
if (taskNumber + 1 == checkbox_labels.size()) {
taskComponentContainer.get()->Add(specialCb);
taskComponentContainer.get()->Add(hover_text_renderer);
} else {
int PREVIOUS_COMPONENT_SIZE = 1;
ftxui::Components postTaskComponents;
for (int i = ((taskNumber * 2) + PREVIOUS_COMPONENT_SIZE);
i < ((checkbox_labels.size() - 1) * 2 + PREVIOUS_COMPONENT_SIZE); i++) {
auto backIteratorIndex = (taskNumber * 2) + PREVIOUS_COMPONENT_SIZE;
postTaskComponents.push_back(taskComponentContainer.get()->ChildAt(backIteratorIndex));
taskComponentContainer.get()->ChildAt(backIteratorIndex)->Detach();
}
taskComponentContainer.get()->Add(specialCb);
taskComponentContainer.get()->Add(hover_text_renderer);
for (const auto &comp: postTaskComponents) {
taskComponentContainer.get()->Add(comp);
}
}
content->clear();
file_modified_flag = true;
} else if (std::regex_search(*content, matches, newCommentPattern) && matches.size() > 1) {
if (!isGoodTaskNumber(matches, *iteration_range_values.front(), *iteration_range_values.back())) { return; }
int taskNumber = getTaskNumber(matches);
auto newContent = " " + matches.str(2);
auto ¤t_comment = *checkbox_comments[taskNumber - 1];
if (!current_comment.empty()) {
current_comment += L"\n";
}
current_comment += L" => [" + convertToWideString(convertToHoursMinutes(getCurrentTime())) + L"] ";
current_comment += convertToWideString(newContent);
content->clear();
file_modified_flag = true;
} else if (std::regex_search(*content, matches, changeTaskPattern) && matches.size() > 1) {
if (!isGoodTaskNumber(matches, *iteration_range_values.front(), *iteration_range_values.back())) { return; }
int taskNumber = getTaskNumber(matches);
auto updatedTask = " " + matches.str(2);
auto ¤t_task = *checkbox_labels[taskNumber - 1];
current_task = updatedTask;
content->clear();
file_modified_flag = true;
} else {
incorrect_input_indicator = true;
// wait for 2 seconds
std::thread([&incorrect_input_indicator]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
incorrect_input_indicator = false;
}).detach();
}
};
auto updateButton = ftxui::Button("Update", onUpdate);
// auto filler_component = ftxui::Renderer([] { return ftxui::filler(); });
auto fileSelectorContainer = ftxui::Container::Vertical({
ftxui::Renderer(
[] {
return ftxui::hbox(
ftxui::text("File"),
ftxui::text(" "),
ftxui::separatorDouble(),
ftxui::text(" "),
ftxui::text("Start Fresh?"),
ftxui::text(" "),
ftxui::separatorDouble(),
ftxui::text(" "),
ftxui::text("Valid?"),
ftxui::text(" "));
}),
ftxui::Renderer([] { return ftxui::separator(); }),
ftxui::Container::Horizontal({
menuContainer,
ftxui::Renderer(
[] {
return ftxui::hbox(
ftxui::text(
" "),
ftxui::separatorDouble(),
ftxui::text(
" "));
}),
startFreshContainer,
ftxui::Renderer(
[] {
return ftxui::hbox(
ftxui::text(
" "),
ftxui::separatorDouble(),
ftxui::text(
" "));
}),
statusContainer,
ftxui::Renderer(
[] {
return ftxui::text(
" ");
})
})
}) | ftxui::border | ftxui::center;
std::string messageButtonString = "OK, take me back!";
auto messageButtonCallback = [&focus_selector]() {
focus_selector = 0;
};
auto errorMessageContainer = ftxui::Container::Vertical({
ftxui::Renderer([] {
return ftxui::text(
"Can't select file: Not compatible with Postopek!") |
ftxui::bold | ftxui::center;
}),
ftxui::Renderer(
[] { return ftxui::separatorHeavy(); }),
ftxui::Button(&messageButtonString,
messageButtonCallback)
}) | ftxui::border | ftxui::center;
auto helpDialogContainer = ftxui::Container::Vertical({
ftxui::Renderer([] {
return ftxui::text(
"Help Section (Esc to leave)") |
ftxui::bold | ftxui::center;
}),
ftxui::Renderer(
[] { return ftxui::separatorHeavy(); }),
ftxui::Renderer([] {
return ftxui::vbox(
ftxui::hbox(
ftxui::text("Ctrl+S"),
ftxui::text(" "),
ftxui::text("Save file")
),
ftxui::hbox(
ftxui::text("Alt+V"),
ftxui::text(" "),
ftxui::text("Focus on tasks")
),
ftxui::hbox(
ftxui::text("qqq"),
ftxui::text(" "),
ftxui::text("Quit")
));
})
}) | ftxui::border | ftxui::center;
auto statusBar = ftxui::Renderer(
[&show_saved_status, &incorrect_input_shortcut_indicator, &incorrect_input_indicator, &file_save_check_flag, &move_to_menu_save_flag] {
if (show_saved_status) {
return ftxui::text("Saved successfully!") | color(ftxui::Color::Green) | ftxui::bold;
} else if (file_save_check_flag) {
return ftxui::text("Unsaved changes! Save changes? (Y)es (N)o (A)bort") | color(ftxui::Color::Red) |
ftxui::bold;
} else if (move_to_menu_save_flag) {
return ftxui::text("Unsaved changes! Save with Ctrl+S first!") | color(ftxui::Color::Red) |
ftxui::bold;
} else if (incorrect_input_shortcut_indicator) {
return ftxui::text("Parse Error: Trouble parsing shortcut") | color(ftxui::Color::Red) |
ftxui::bold;
} else if (incorrect_input_indicator) {
return ftxui::text("Parse Error: Trouble parsing input command") | color(ftxui::Color::Red) |
ftxui::bold;
} else {
return ftxui::hbox(ftxui::text(
"Alt+H ▶️ Help "),
ftxui::text(
"ant # ▶️ Add new task after # "),
ftxui::text(
"acc # ▶️ Add comment for # "),
ftxui::text(
"ctt # ▶️ Change task for #"));
}
});
auto taskContainer = ftxui::Container::Vertical({
timeRenderer,
taskComponentContainer | ftxui::frame |
ftxui::vscroll_indicator
});
auto completeLayout = ftxui::Container::Vertical({
taskContainer | ftxui::flex,
ftxui::Container::Horizontal({
input_component |
ftxui::borderRounded,
updateButton
}),
statusBar
});
completeLayout |= ftxui::CatchEvent([&](const ftxui::Event &event) {
if (event == ftxui::Event::Tab) {
bool inputBarFocused = completeLayout->ChildAt(0)->ChildAt(1)->ChildAt(0)->Focused();
if (!inputBarFocused) {
completeLayout->ChildAt(0)->ChildAt(1)->ChildAt(0)->TakeFocus();
return true;
}
} else if (event == ftxui::Event::Special({0x1b, '[', '1', ';', '5', 'D'})) { // Special ASCII code for Ctrl+<-
if (file_modified_flag) {
move_to_menu_save_flag = true;
// wait for 2 seconds
std::thread([&move_to_menu_save_flag]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
move_to_menu_save_flag = false;
}).detach();
return true;
}
focus_selector = 0;
return true;
} else if (event == ftxui::Event::Special({0x13})) { // Special ASCII code for Ctrl+S
if (file_modified_flag) {
ApplicationMetaData::instance().filePath = mdPaths.at(menu_selector);
ApplicationMetaData::instance().task_header_ref = task_header;
ApplicationMetaData::instance().checkbox_statuses = checkbox_status;
ApplicationMetaData::instance().checkbox_labels = checkbox_labels;
ApplicationMetaData::instance().checkbox_comments = checkbox_comments;
// Perform the action associated with Ctrl+S
HandleSavingEvent(ApplicationMetaData::instance());
file_modified_flag = false;
show_saved_status = true;
// wait for 2 seconds
std::thread([&show_saved_status]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
show_saved_status = false;
}).detach();
}
return true;
} else if (event == ftxui::Event::Special("\x1Bv")) { // ASCII value for Alt+V
bool taskContainerFocused = completeLayout->ChildAt(0)->ChildAt(0)->ChildAt(0)->ChildAt(1)->Focused();
if (!taskContainerFocused) {
completeLayout->ChildAt(0)->ChildAt(0)->ChildAt(0)->ChildAt(1)->TakeFocus();
return true;
}
} else if (event == ftxui::Event::Special("\x1Bh")) { // ASCII value for Alt+H
// Render the help dialog.
focus_selector = 3;
return true;
}
return false;
});
// Replace the main component with the engine wrapper
auto applicationContainer = ftxui::Container::Tab({
fileSelectorContainer,
completeLayout,
errorMessageContainer,
helpDialogContainer
}, &focus_selector);
bool runEngine = true;
auto quitMethod = [&screen, &runEngine]() {
screen.ExitLoopClosure()();
runEngine = false;
ClearDOSPromptScreen();
};
static int qCounter = 0;
static bool inSaveCheckState = false;
applicationContainer |= ftxui::CatchEvent([&](const ftxui::Event &event) {
if (focus_selector == 3 && event == ftxui::Event::Escape) {
focus_selector = 1;
return true;
}
if (event == ftxui::Event::Character('q')) {
++qCounter; // increment the global variable
if (qCounter == 3) { // Only exit when 'q' is pressed 3 times
if (file_modified_flag) {
file_save_check_flag = true;
inSaveCheckState = true;
return true;
} else {
auto currentSetConfigFlag = *(shouldStartFreshStatusFlags.at(menu_selector));
if (currentSetConfigFlag) {
// Quit without saving but recently started fresh
auto filePathToRemove = mdPaths.at(menu_selector).string();
removeLineWithFilePath(filePathToRemove);
}
quitMethod();
}
}
} else if (inSaveCheckState && event == ftxui::Event::Character('y')) {
file_save_check_flag = false;
ApplicationMetaData::instance().filePath = mdPaths.at(menu_selector);
ApplicationMetaData::instance().task_header_ref = task_header;
ApplicationMetaData::instance().checkbox_statuses = checkbox_status;
ApplicationMetaData::instance().checkbox_labels = checkbox_labels;
ApplicationMetaData::instance().checkbox_comments = checkbox_comments;
// Perform the action associated with Ctrl+S
HandleSavingEvent(ApplicationMetaData::instance());
file_modified_flag = false;
show_saved_status = true;
// wait for 2 seconds
std::thread([&show_saved_status]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
show_saved_status = false;
}).detach();
inSaveCheckState = false;
quitMethod();
} else if (inSaveCheckState && event == ftxui::Event::Character('n')) {
quitMethod();
} else if (inSaveCheckState && event == ftxui::Event::Character('a')) {
file_save_check_flag = false;
inSaveCheckState = false;
qCounter = 0;
}
return false;
});
// Run the application in a loop.
std::thread([&] {
while (runEngine) {
std::this_thread::sleep_for(std::chrono::seconds(1));
screen.PostEvent(ftxui::Event::Custom);
}
}).detach();
// Start the event loop.
screen.Clear();
screen.Loop(applicationContainer);
return 0;
}
// Copyright Pralad Prasad