From 4b7644bd4f27d5e38a8f235d59ad51f090120637 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sat, 17 May 2025 17:56:57 +0200 Subject: [PATCH] add filename loading --- .gitignore | 1 + src/menu.cpp | 110 +++++++++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 7f60c9a..dc1c4df 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ compile_commands.json core* build log +complaints \ No newline at end of file diff --git a/src/menu.cpp b/src/menu.cpp index a408b0d..6aa9771 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,3 +1,15 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "const.h" #include "cups.h" #include "editor_easy.h" @@ -5,21 +17,9 @@ #include "memory.h" #include "strings.h" #include "types.h" -#include -#include -#include -#include -#include -#include -#include -#include std::vector main_menu_allocated; - -const char *choices[] = {"paradox", "kompromis", "Stereotyp", "Žena"}; -const char *date[] = {"2023-10-01", "2023-10-02", "2023-10-03", "2023-10-04"}; -const char *choices2[] = {"PRINT", "EDIT", "DELETE", "Žena"}; - +#define COMPLAINTS_DIR "complaints" void menu() { current_allocated = &main_menu_allocated; /* Initialize curses */ @@ -29,24 +29,40 @@ void menu() { start_color(); cbreak(); noecho(); - curs_set(0); // Makes cursor invisible + curs_set(0); // Makes cursor invisible keypad(stdscr, TRUE); for (uint8_t i = 0; i < 8; i++) { init_pair(i, i, COLOR_BLACK); } async_clock_init(); - /* Create items */ - size_t n_choices = ARRAY_SIZE(choices); + // Create items complete_menu main_menu = {nullptr, nullptr, 0, nullptr}; main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1}); - - main_menu.items = new ITEM *[ARRAY_SIZE(choices) + 1]; - main_menu.items_size = ARRAY_SIZE(choices) + 1; - for (size_t i = 0; i < n_choices; ++i) { - main_menu.items[i] = new_item(choices[i], date[i]); + std::vector items; + if (std::filesystem::exists(COMPLAINTS_DIR)) { + for (const auto& directroy_entry : + std::filesystem::directory_iterator(COMPLAINTS_DIR)) { + if (directroy_entry.is_regular_file()) { + std::string name_date[2]; + std::stringstream ssfn(directroy_entry.path().filename().string()); + for (uint8_t i = 0; i < 2; i++) { + std::getline(ssfn, name_date[i], '_'); + } + char* name = new char[name_date[0].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, name, 1}); + char* date = new char[name_date[1].length() + 1]; + main_menu_allocated.push_back({GENERIC_TYPE, date, 1}); + strcpy(name, name_date[0].c_str()); + strcpy(date, name_date[1].c_str()); + items.push_back(new_item(name, date)); + } + } + } else { + std::filesystem::create_directory(COMPLAINTS_DIR); } - main_menu.items[n_choices] = nullptr; + items.push_back(nullptr); + main_menu.items = items.data(); /* Crate menu */ main_menu.menu = new_menu(main_menu.items); @@ -79,33 +95,33 @@ void menu() { int c; while ((c = wgetch(main_menu.win)) != KEY_F(1)) { switch (c) { - case KEY_DOWN: - menu_driver(main_menu.menu, REQ_DOWN_ITEM); - break; - case KEY_UP: - menu_driver(main_menu.menu, REQ_UP_ITEM); - break; - case 10: + case KEY_DOWN: + menu_driver(main_menu.menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(main_menu.menu, REQ_UP_ITEM); + break; + case 10: - refresh(); - break; - case ':': - switch (hash_djb2a(spawncmd())) { - case "print"_sh: - case "p"_sh: - // DONT FORGET TO PRINT ACTUAL DOCUMENT - printDocument("test"); - current_allocated = &main_menu_allocated; + refresh(); break; - case "easy_edit"_sh: - case "ee"_sh: - std::clog << vytvorTrestniOznameni(); - break; - default: - print_in_middle(main_menu.win, 10, 0, 40, loc_strings->unknown_command, - COLOR_PAIR(1)); - break; - } + case ':': + switch (hash_djb2a(spawncmd())) { + case "print"_sh: + case "p"_sh: + // DONT FORGET TO PRINT ACTUAL DOCUMENT + printDocument("test"); + current_allocated = &main_menu_allocated; + break; + case "easy_edit"_sh: + case "ee"_sh: + std::clog << vytvorTrestniOznameni(); + break; + default: + print_in_middle(main_menu.win, 10, 0, 40, + loc_strings->unknown_command, COLOR_PAIR(1)); + break; + } } redrawwin(main_menu.win); wrefresh(main_menu.win);