bit more work on komens
Some checks failed
/ sync-to-origin (push) Has been cancelled

This commit is contained in:
PoliEcho 2025-04-05 11:49:01 +02:00
parent b005631d20
commit b352daabfb
9 changed files with 221 additions and 32 deletions

View File

@ -199,7 +199,7 @@ std::wstring wrm_tr_le_whitespace(const std::wstring &s) {
}
// Conversion utilities
char *wchar_to_char(wchar_t *src) {
char *wchar_to_char(const wchar_t *src) {
if (!src)
return nullptr;
@ -210,7 +210,7 @@ char *wchar_to_char(wchar_t *src) {
return dest;
}
wchar_t *char_to_wchar(char *src) {
wchar_t *char_to_wchar(const char *src) {
if (!src)
return nullptr;

View File

@ -27,8 +27,8 @@ void wprint_in_middle(WINDOW *win, int starty, int startx, int width,
std::wstring wrm_tr_le_whitespace(const std::wstring &s);
// Conversion utilities
char *wchar_to_char(wchar_t *src);
wchar_t *char_to_wchar(char *src);
char *wchar_to_char(const wchar_t *src);
wchar_t *char_to_wchar(const char *src);
std::wstring string_to_wstring(const std::string &str);
std::string wstring_to_string(const std::wstring &wstr);

View File

@ -1,18 +1,14 @@
#include "const.h"
#include "komens.h"
#include "helper_funcs.h"
#include "memory.h"
#include "net.h"
#include <bits/chrono.h>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <curses.h>
#include <cwchar>
#include <ncurses.h>
#include <fstream>
#include <iostream>
#include <menu.h>
#include <nlohmann/json.hpp>
#include <panel.h>
#include <string>
#include <sys/types.h>
#include <vector>
using nlohmann::json;
@ -23,11 +19,13 @@ void komens_page() {
current_allocated = &komens_allocated;
json resp_from_api;
{
std::string endpoint = "api/3/komens";
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, GET);
std::ifstream f("test-data/komens.json");
resp_from_api = json::parse(f);
f.close();
}
setlocale(LC_ALL, "");
/* Initialize curses */
setlocale(LC_ALL, "");
initscr();
start_color();
cbreak();
@ -38,4 +36,64 @@ void komens_page() {
for (uint8_t i = 0; i < 8; i++) {
init_pair(i, i, COLOR_BLACK);
}
size_t num_of_komens = resp_from_api["Messages"].size();
ITEM **komens_items = new ITEM *[num_of_komens + 1];
komens_allocated.push_back({ITEM_ARRAY, komens_items, num_of_komens});
for (size_t i = 0; i < num_of_komens; i++) {
komens_items[i] = new_item(
resp_from_api["Messages"][i]["Title"].get<std::string>().c_str(),
resp_from_api["Messages"][i]["Sender"]["Name"]
.get<std::string>()
.c_str());
}
komens_items[num_of_komens] = nullptr;
MENU *komens_choise_menu = new_menu(komens_items);
komens_allocated.push_back({MENU_TYPE, komens_choise_menu, 0});
WINDOW *komens_choise_menu_win = newwin(20, 40, 4, 4);
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu_win, 0});
set_menu_win(komens_choise_menu, komens_choise_menu_win);
set_menu_sub(komens_choise_menu, derwin(komens_choise_menu_win, 8, 38, 3, 1));
set_menu_format(komens_choise_menu, 7, 1);
set_menu_mark(komens_choise_menu, " * ");
box(komens_choise_menu_win, 0, 0);
wprint_in_middle(komens_choise_menu_win, 1, 0, 40, L"Komens", COLOR_PAIR(1));
mvwaddch(komens_choise_menu_win, 2, 0, ACS_LTEE);
mvwhline(komens_choise_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(komens_choise_menu_win, 2, 39, ACS_RTEE);
post_menu(komens_choise_menu);
wrefresh(komens_choise_menu_win);
attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0,
"Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
int c;
while ((c = getch()) != KEY_F(1)) {
switch (c) {
case KEY_DOWN:
case KEY_NPAGE:
case 'j':
menu_driver(komens_choise_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
case KEY_PPAGE:
case 'k':
menu_driver(komens_choise_menu, REQ_UP_ITEM);
break;
}
wrefresh(komens_choise_menu_win);
}
delete_all(&komens_allocated);
}

4
src/komens.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef _ba_ko_hg_
#define _ba_ko_hg_
void komens_page();
#endif

107
src/komens_menu.cpp Normal file
View File

@ -0,0 +1,107 @@
#include "komens_menu.h"
#include "helper_funcs.h"
#include "komens.h"
#include "net.h"
#include <cstdlib>
#include <cstring>
#include <curses.h>
#include <menu.h>
void komens_menu() {
wchar_t *choices[] = {
L"received", L"send", L"noticeboard", L"Exit", nullptr,
};
void (*choicesFuncs[])() = {komens_page, nullptr, nullptr, nullptr, nullptr};
ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *my_menu_win;
int n_choices, i;
/* Initialize curses */
setlocale(LC_ALL, "");
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for (i = 0; i < n_choices; ++i)
my_items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
/* Crate menu */
my_menu = new_menu((ITEM **)my_items);
/* Create the window to be associated with the menu */
my_menu_win = newwin(12, 40, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 8, 38, 3, 1));
set_menu_format(my_menu, 7, 1);
/* Set menu mark to the string " * " */
set_menu_mark(my_menu, " * ");
/* Print a border around the main window and print a title */
box(my_menu_win, 0, 0);
wprint_in_middle(my_menu_win, 1, 0, 40, L"Komens Menu", COLOR_PAIR(1));
mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
mvwaddch(my_menu_win, 2, 39, ACS_RTEE);
/* Post the menu */
post_menu(my_menu);
wrefresh(my_menu_win);
attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0,
"Use PageUp and PageDown to scoll down or up a page of items");
mvprintw(LINES - 1, 0, "Arrow Keys to navigate (F1 to Exit)");
attroff(COLOR_PAIR(2));
refresh();
while ((c = getch()) != KEY_F(1)) {
switch (c) {
case KEY_DOWN:
case KEY_NPAGE:
case 'j':
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
case KEY_PPAGE:
case 'k':
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: // ENTER
clear();
if (item_index(current_item(my_menu)) == n_choices - 1) {
goto close_menu;
}
choicesFuncs[item_index(current_item(my_menu))]();
pos_menu_cursor(my_menu);
refresh();
wrefresh(my_menu_win);
break;
}
wrefresh(my_menu_win);
}
close_menu:
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);
free_menu(my_menu);
for (i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}

6
src/komens_menu.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _ba_km_hg_
#define _ba_km_hg_
void komens_menu();
#endif

View File

@ -1,24 +1,20 @@
#include "helper_funcs.h"
#include "komens_menu.h"
#include "marks.h"
#include "net.h"
#include "timetable.h"
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <curses.h>
#include <menu.h>
#include "marks.h"
#define CTRLD 4
wchar_t *choices[] = {
L"login", L"Marks", L"timetable", L"Komens",
L"Homework", L"Absence", L"Exit", nullptr,
};
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, nullptr,
nullptr, nullptr, nullptr, nullptr};
void main_menu() {
wchar_t *choices[] = {
L"login", L"Marks", L"timetable", L"Komens",
L"Homework", L"Absence", L"Exit", nullptr,
};
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu,
nullptr, nullptr, nullptr, nullptr};
ITEM **my_items;
int c;
MENU *my_menu;
@ -85,12 +81,15 @@ void main_menu() {
break;
case KEY_UP:
case KEY_PPAGE:
case KEY_PPAGE:
case 'k':
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: // ENTER
clear();
if (item_index(current_item(my_menu)) == n_choices - 1) {
goto close_menu;
}
choicesFuncs[item_index(current_item(my_menu))]();
pos_menu_cursor(my_menu);
refresh();
@ -99,6 +98,7 @@ void main_menu() {
}
wrefresh(my_menu_win);
}
close_menu:
/* Unpost and free all the memory taken up */
unpost_menu(my_menu);

View File

@ -6,7 +6,7 @@
void delete_all(std::vector<allocation> *allocated) {
if (allocated == nullptr) {
return;
return;
}
for (long long i = allocated->size() - 1; i >= 0; i--) {
switch (allocated->at(i).type) {
@ -26,6 +26,14 @@ void delete_all(std::vector<allocation> *allocated) {
delete[] panels;
break;
}
case ITEM_ARRAY: {
ITEM **items = static_cast<ITEM **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; j++) {
free_item(items[j]);
}
delete[] items;
break;
}
case GENERIC_ARRAY:
delete[] static_cast<char *>(allocated->at(i).ptr);
break;
@ -35,6 +43,9 @@ void delete_all(std::vector<allocation> *allocated) {
case PANEL_TYPE:
del_panel(static_cast<PANEL *>(allocated->at(i).ptr));
break;
case MENU_TYPE:
free_menu(static_cast<MENU *>(allocated->at(i).ptr));
break;
case GENERIC_TYPE:
delete static_cast<char *>(allocated->at(i).ptr);
break;

View File

@ -3,13 +3,16 @@
#define _ba_me_hg_
#include <cstddef>
#include <menu.h>
#include <vector>
enum AllocationType {
WINDOW_ARRAY,
PANEL_ARRAY,
ITEM_ARRAY,
GENERIC_ARRAY,
WINDOW_TYPE,
PANEL_TYPE,
MENU_TYPE,
GENERIC_TYPE
};