21 Commits

Author SHA1 Message Date
PoliEcho ab854ed5ed version 1.0.0
/ sync-to-origin (push) Has been cancelled
2025-11-08 15:51:52 +01:00
PoliEcho 0573510962 add absence page 2025-11-08 15:40:33 +01:00
PoliEcho 290b9344d2 fix password entry prompt
/ sync-to-origin (push) Has been cancelled
2025-11-07 11:49:55 +01:00
PoliEcho 24e4528b4c fix exit to komens menu 2025-11-07 11:49:13 +01:00
PoliEcho 15b0344dc8 hide cursor on marks page
/ sync-to-origin (push) Has been cancelled
2025-06-18 17:32:46 +02:00
PoliEcho 73fed3df0e version 0.8.2
/ sync-to-origin (push) Has been cancelled
2025-05-24 12:29:19 +02:00
PoliEcho 6b3d47ceed add color help for timetable
/ sync-to-origin (push) Waiting to run
2025-05-24 12:27:41 +02:00
PoliEcho 89c67035ca fix menu disappearing after exiting any page 2025-05-24 10:22:07 +02:00
PoliEcho 0486d9fb22 fix space before operator warning 2025-05-24 10:18:59 +02:00
PoliEcho e51088eb26 add download progress bar
/ sync-to-origin (push) Has been cancelled
2025-04-14 20:03:48 +02:00
PoliEcho ad6f748899 add homework checkbox to README
/ sync-to-origin (push) Waiting to run
2025-04-14 07:26:06 +00:00
PoliEcho 1d0d4e2362 update README to reflect new aditions
/ sync-to-origin (push) Waiting to run
2025-04-14 09:24:23 +02:00
PoliEcho a35242b420 fix attachment window not disapearing 2025-04-14 09:22:39 +02:00
PoliEcho e78c8d7efa version 0.8.1
/ sync-to-origin (push) Has been cancelled
2025-04-14 09:12:25 +02:00
PoliEcho f01422eaf5 fix memory leaks
/ sync-to-origin (push) Waiting to run
2025-04-14 09:10:56 +02:00
PoliEcho 9b8bdbf850 add asan build rule to Makefile 2025-04-14 09:10:37 +02:00
PoliEcho c9aef90130 add beter regex to gitignore 2025-04-14 09:10:18 +02:00
PoliEcho d9f545ed09 remove random C89 apperence 2025-04-13 19:31:32 +02:00
PoliEcho d533b526bf fix timetable const issue 2025-04-13 14:51:25 +02:00
PoliEcho ea083e31fc make more timetable variables const 2025-04-13 14:46:50 +02:00
PoliEcho 05c406ed6a extract komens usage message to function 2025-04-13 14:44:45 +02:00
16 changed files with 481 additions and 144 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
.cache .cache
.vscode .vscode
compile_commands.json compile_commands.json
core* *core*
build build
test-data test-data
log log
+5 -1
View File
@@ -2,6 +2,7 @@
CPPC = g++ CPPC = g++
CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings CPPC_FLAGS = -std=c++23 -s -O3 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings DEBUG_FLAGS = -ggdb -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
DEBUG_ASANITIZE = -fsanitize=address -ggdb -fno-omit-frame-pointer -std=c++23 -lncursesw -lcurl -lmenuw -lpanel -Wall -Wextra -Wno-write-strings
SRC_PATH := src SRC_PATH := src
@@ -19,6 +20,9 @@ all: make-build-dir $(BIN_PATH)/bakatui
debug: CPPC_FLAGS = $(DEBUG_FLAGS) debug: CPPC_FLAGS = $(DEBUG_FLAGS)
debug: make-build-dir $(BIN_PATH)/bakatui debug: make-build-dir $(BIN_PATH)/bakatui
asan: CPPC_FLAGS = $(DEBUG_ASANITIZE)
asan: make-build-dir $(BIN_PATH)/bakatui
make-build-dir: make-build-dir:
mkdir -p $(OBJ_PATH) mkdir -p $(OBJ_PATH)
@@ -39,4 +43,4 @@ install:
clean: clean:
rm -fr build rm -fr build
.PHONY: all clean install debug .PHONY: all clean install debug asan
+2 -1
View File
@@ -10,5 +10,6 @@
> - [x] Login > - [x] Login
> - [x] Marks > - [x] Marks
> - [x] Timetable > - [x] Timetable
> - [ ] Komens > - [x] Komens
> - [ ] Absence > - [ ] Absence
> - [ ] Homework
+168
View File
@@ -0,0 +1,168 @@
#include "memory.h"
#include "helper_funcs.h"
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <curses.h>
#include <nlohmann/json.hpp>
#include "net.h"
#include <nlohmann/json_fwd.hpp>
#include <ncurses.h>
#include <string>
using nlohmann::json;
std::vector<allocation> absence_allocated;
#define DATE_LEN 10//DD.MM.YYYY
#define SUM_FIELD_NUM 7
#define NUM_PRESSISON 3
#define FLOAT_PRESSISON 6
#define BASE_ABSENCE_WIN_SIZE 1+NUM_PRESSISON+1+NUM_PRESSISON+1+FLOAT_PRESSISON+1+1
#define ABSENCE_WIN_HIGHT 5
constexpr char sum_field_names[SUM_FIELD_NUM] = {'/', 'X','N','P','O', '-', 'D'};
constexpr chtype sum_field_colors[SUM_FIELD_NUM] = {
COLOR_PAIR(COLOR_RED),
COLOR_PAIR(COLOR_GREEN),
COLOR_PAIR(COLOR_RED),
COLOR_PAIR(COLOR_YELLOW),
COLOR_PAIR(COLOR_MAGENTA),
COLOR_PAIR(COLOR_GREEN),
COLOR_PAIR(COLOR_CYAN)
};
constexpr char* sum_field_strings[SUM_FIELD_NUM] = {"Unsolved","Ok","Missed","Late","Soon","School","DistanceTeaching"};
constexpr char date_str[] = "Date";
void absence_page() {
current_allocated = &absence_allocated;
const json resp_from_api = [&]() -> json {
const std::string endpoint = "api/3/absence/student";
return bakaapi::get_data_from_endpoint(endpoint, GET);
}();
/* Initialize curses */
setlocale(LC_ALL, "");
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
/* Initialize all the colors */
for (uint8_t i = 0; i < 8; i++) {
init_pair(i, i, COLOR_BLACK);
}
const uint16_t sum_win_height = (resp_from_api["Absences"].size()*2)+1;
const uint16_t sum_win_width = 1+DATE_LEN+1+(SUM_FIELD_NUM*4);
const float absence_threshold = resp_from_api["PercentageThreshold"].get<float>()*100.0f;
{
WINDOW*sum_win = newwin(sum_win_height,sum_win_width,0,0);
absence_allocated.push_back({WINDOW_TYPE, sum_win, 1});
box(sum_win, 0, 0);
print_in_middle(sum_win, 1, 1, DATE_LEN, date_str, COLOR_PAIR(COLOR_RED));
for (uint16_t i = 0; i < SUM_FIELD_NUM; i++) {
mvwaddch(sum_win, 0, 1+DATE_LEN+(i*4), ACS_TTEE);
mvwaddch(sum_win, 1, 1+DATE_LEN+(i*4), ACS_VLINE);
mvwaddch(sum_win, sum_win_height-1, 1+DATE_LEN+(i*4), ACS_BTEE);
wattron(sum_win, sum_field_colors[i]);
mvwaddch(sum_win, 1, 1+DATE_LEN+2+(i*4),sum_field_names[i]);
wattroff(sum_win, sum_field_colors[i]);
}
for (size_t i = 0; resp_from_api["Absences"].size()-1 > i; i++) {
mvwaddch(sum_win, 2+(i*2), 0, ACS_LTEE);
mvwhline(sum_win, 2+(i*2), 1, ACS_HLINE, DATE_LEN);
mvwaddch(sum_win, 2+(i*2), 1+DATE_LEN, ACS_PLUS);
for(size_t j=0;j<SUM_FIELD_NUM;j++) {
mvwhline(sum_win, 2+(i*2),1+DATE_LEN+1+(j*4), ACS_HLINE,3);
mvwaddch(sum_win, 2+(i*2),1+DATE_LEN+4+(j*4), ACS_PLUS);
mvwaddch(sum_win, 3+(i*2),1+DATE_LEN+(j*4), ACS_VLINE);
mvwprintw(sum_win,3+(i*2),1+DATE_LEN+1+(j*4),"%3d",resp_from_api["Absences"][i][sum_field_strings[j]].get<int>());
}
mvwaddch(sum_win, 2+(i*2), sum_win_width-1, ACS_RTEE);
const std::string date_str = resp_from_api["Absences"][i]["Date"].get<std::string>();
const char* date_cstr = date_str.c_str();
mvwprintw(sum_win, 2+(i*2)+1, 1, "%.2s.%.2s.%.4s", date_cstr+8,date_cstr+5,date_cstr);
}
wrefresh(sum_win);
}
WINDOW** subject_wins = new WINDOW* [resp_from_api["AbsencesPerSubject"].size()];
absence_allocated.push_back({WINDOW_ARRAY, subject_wins, resp_from_api["AbsencesPerSubject"].size()});
uint16_t window_y_offset = 0;
uint16_t window_x_offset = sum_win_width+1;
for (size_t i = 0;resp_from_api["AbsencesPerSubject"].size() > i; i++) {
const uint16_t subject_name_lenght = string_to_wstring(resp_from_api["AbsencesPerSubject"][i]["SubjectName"].get<std::string>()).length();
const uint16_t subject_win_width = 2 + subject_name_lenght > BASE_ABSENCE_WIN_SIZE ? subject_name_lenght+2 : BASE_ABSENCE_WIN_SIZE;
if (window_x_offset + subject_win_width > COLS) {
window_x_offset = sum_win_width+1;
window_y_offset += ABSENCE_WIN_HIGHT;
}
subject_wins[i] = newwin(ABSENCE_WIN_HIGHT,subject_win_width, window_y_offset, window_x_offset);
box(subject_wins[i],0,0);
window_x_offset +=subject_win_width;
const uint16_t absence_per_subject_base = resp_from_api["AbsencesPerSubject"][i]["Base"].get<uint16_t>();
const uint16_t absence_per_subject_lessons_count = resp_from_api["AbsencesPerSubject"][i]["LessonsCount"].get<uint16_t>();
const float absence_per_subject_percentage = ((resp_from_api["AbsencesPerSubject"][i]["Base"].get<float>()/resp_from_api["AbsencesPerSubject"][i]["LessonsCount"].get<float>())*100.0);
mvwaddch(subject_wins[i],2,0,ACS_LTEE);
mvwhline(subject_wins[i],2,1,ACS_HLINE,subject_win_width-2);
mvwaddch(subject_wins[i],2,subject_win_width-1,ACS_RTEE);
mvwaddch(subject_wins[i], 2, 1+NUM_PRESSISON, ACS_TTEE);
mvwaddch(subject_wins[i], 3, 1+NUM_PRESSISON, ACS_VLINE);
mvwaddch(subject_wins[i], 4, 1+NUM_PRESSISON, ACS_BTEE);
mvwaddch(subject_wins[i], 2, 1+(NUM_PRESSISON*2)+1, ACS_TTEE);
mvwaddch(subject_wins[i], 3, 1+(NUM_PRESSISON*2)+1, ACS_VLINE);
mvwaddch(subject_wins[i], 4, 1+(NUM_PRESSISON*2)+1, ACS_BTEE);
if (subject_win_width != BASE_ABSENCE_WIN_SIZE) {
mvwaddch(subject_wins[i], 2, subject_win_width-FLOAT_PRESSISON-3, ACS_TTEE);
mvwaddch(subject_wins[i], 3, subject_win_width-FLOAT_PRESSISON-3, ACS_VLINE);
mvwaddch(subject_wins[i], 4, subject_win_width-FLOAT_PRESSISON-3, ACS_BTEE);
}
mvwprintw(subject_wins[i], 3, 1, "%3d",absence_per_subject_base);
mvwprintw(subject_wins[i], 3, 1+NUM_PRESSISON+1, "%3d",absence_per_subject_lessons_count);
chtype text_color;
if (absence_threshold <= absence_per_subject_percentage) {
text_color = COLOR_PAIR(COLOR_RED);
} else if (absence_threshold*0.6f <= absence_per_subject_percentage) {
text_color = COLOR_PAIR(COLOR_YELLOW);
} else {
text_color = COLOR_PAIR(COLOR_GREEN);
}
wprint_in_middle(subject_wins[i],1, 1, subject_win_width-1, string_to_wstring(resp_from_api["AbsencesPerSubject"][i]["SubjectName"].get<std::string>()).c_str() , text_color);
wattron(subject_wins[i], text_color);
mvwprintw(subject_wins[i], 3, subject_win_width-FLOAT_PRESSISON-1-1, "%6.2f%%",absence_per_subject_percentage);
wattroff(subject_wins[i], text_color);
wrefresh(subject_wins[i]);
}
attron(COLOR_PAIR(4));
mvprintw(LINES-1, 0, "F1 to exit | Absence threshold: %.2f%% ", absence_threshold);
attroff(COLOR_PAIR(4));
for (uint8_t i = 0; i < SUM_FIELD_NUM; i++) {
attron(COLOR_PAIR(4));
addch('|');
attroff(COLOR_PAIR(4));
wattron(stdscr, sum_field_colors[i]);
printw( " %c:%s ", sum_field_names[i], sum_field_strings[i]);
wattroff(stdscr, sum_field_colors[i]);
}
refresh();
while (getch() != KEY_F(1));
delete_all(&absence_allocated);
clear();
}
+2
View File
@@ -0,0 +1,2 @@
#pragma once
void absence_page();
+2 -2
View File
@@ -1,7 +1,7 @@
#include <string_view> #include <string_view>
#ifndef VERSION #ifndef VERSION
#define VERSION "0.8" #define VERSION "1.0.0"
#define NAME "bakatui" #define NAME "bakatui"
inline constexpr auto hash_djb2a(const std::string_view sv) { inline constexpr auto hash_djb2a(const std::string_view sv) {
@@ -12,7 +12,7 @@ inline constexpr auto hash_djb2a(const std::string_view sv) {
return hash; return hash;
} }
inline constexpr auto operator"" _sh(const char *str, size_t len) { inline constexpr auto operator""_sh(const char *str, size_t len) {
return hash_djb2a(std::string_view{str, len}); return hash_djb2a(std::string_view{str, len});
} }
+6
View File
@@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <codecvt> #include <codecvt>
#include <csignal> #include <csignal>
#include <cstring>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <filesystem> #include <filesystem>
@@ -207,6 +208,11 @@ char *wchar_to_char(const wchar_t *src) {
size_t len = wcslen(src) + 1; // +1 for null terminator size_t len = wcslen(src) + 1; // +1 for null terminator
char *dest = new char[len * MB_CUR_MAX]; char *dest = new char[len * MB_CUR_MAX];
current_allocated->push_back(allocation{
GENERIC_ARRAY,
dest,
len * MB_CUR_MAX,
});
std::wcstombs(dest, src, len * MB_CUR_MAX); std::wcstombs(dest, src, len * MB_CUR_MAX);
return dest; return dest;
+68 -28
View File
@@ -5,10 +5,12 @@
#include "types.h" #include "types.h"
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <curses.h> #include <curses.h>
#include <cwchar> #include <cwchar>
#include <future>
#include <menu.h> #include <menu.h>
#include <ncurses.h> #include <ncurses.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@@ -26,22 +28,28 @@ using nlohmann::json;
std::vector<allocation> komens_allocated; std::vector<allocation> komens_allocated;
void insert_content(WINDOW *content_win, WINDOW *attachment_win, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &resp_from_api); const json &resp_from_api);
void komens_print_usage_message() {
attron(COLOR_PAIR(COLOR_BLUE));
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(COLOR_BLUE));
refresh();
}
void komens_page(koment_type type) { void komens_page(koment_type type) {
current_allocated = &komens_allocated; current_allocated = &komens_allocated;
json resp_from_api;
{ const json resp_from_api = [&]() -> json {
/*std::ifstream f("test-data/komens.json");
resp_from_api = json::parse(f);
f.close();*/
const char *types[] = {"/api/3/komens/messages/received", const char *types[] = {"/api/3/komens/messages/received",
"/api/3/komens/messages/sent", "/api/3/komens/messages/sent",
"/api/3/komens/messages/noticeboard"}; "/api/3/komens/messages/noticeboard"};
const std::string endpoint = types[type]; const std::string endpoint = types[type];
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, POST); return bakaapi::get_data_from_endpoint(endpoint, POST);
} }();
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@@ -57,14 +65,16 @@ void komens_page(koment_type type) {
} }
complete_menu komens_choise_menu; complete_menu komens_choise_menu;
komens_allocated.push_back({COMPLETE_MENU_TYPE, &komens_choise_menu, 1});
size_t num_of_komens = resp_from_api["Messages"].size(); size_t num_of_komens = resp_from_api["Messages"].size();
komens_choise_menu.items = new ITEM *[num_of_komens + 1]; komens_choise_menu.items = new ITEM *[num_of_komens + 1];
komens_allocated.push_back( komens_choise_menu.items_size = num_of_komens + 1;
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
char **title_bufs = new char *[num_of_komens]; char **title_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, title_bufs, num_of_komens});
char **name_bufs = new char *[num_of_komens]; char **name_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, name_bufs, num_of_komens});
size_t max_item_lenght; size_t max_item_lenght;
{ {
size_t max_title_lenght = 0; size_t max_title_lenght = 0;
@@ -72,10 +82,8 @@ void komens_page(koment_type type) {
size_t tmp_lenght; size_t tmp_lenght;
char tmp_buf[1500]; char tmp_buf[1500];
for (size_t i = 0; i < num_of_komens; i++) { for (size_t i = 0; i < num_of_komens; i++) {
wcstombs(tmp_buf, strlcpy(tmp_buf,
string_to_wstring( resp_from_api["Messages"][i]["Title"].get<std::string>().c_str(),
resp_from_api["Messages"][i]["Title"].get<std::string>())
.c_str(),
sizeof(tmp_buf)); sizeof(tmp_buf));
tmp_lenght = tmp_lenght =
@@ -87,10 +95,10 @@ void komens_page(koment_type type) {
title_bufs[i] = new char[strlen(tmp_buf) + 1]; title_bufs[i] = new char[strlen(tmp_buf) + 1];
strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1); strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
wcstombs(
tmp_buf, strlcpy(tmp_buf,
string_to_wstring( resp_from_api["Messages"][i]["Sender"]["Name"]
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>()) .get<std::string>()
.c_str(), .c_str(),
sizeof(tmp_buf)); sizeof(tmp_buf));
@@ -112,7 +120,6 @@ void komens_page(koment_type type) {
komens_choise_menu.items[num_of_komens] = nullptr; komens_choise_menu.items[num_of_komens] = nullptr;
komens_choise_menu.menu = new_menu(komens_choise_menu.items); komens_choise_menu.menu = new_menu(komens_choise_menu.items);
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
komens_choise_menu.win = komens_choise_menu.win =
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET); newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
@@ -143,17 +150,13 @@ void komens_page(koment_type type) {
komens_allocated.push_back({WINDOW_TYPE, content_win, 1}); komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
WINDOW *attachment_win = newwin(1, 1, LINES, COLS); WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
komens_allocated.push_back({WINDOW_TYPE, attachment_win, 1});
insert_content(content_win, attachment_win, insert_content(content_win, attachment_win,
resp_from_api["Messages"][item_index( resp_from_api["Messages"][item_index(
current_item(komens_choise_menu.menu))]); current_item(komens_choise_menu.menu))]);
attron(COLOR_PAIR(COLOR_BLUE)); komens_print_usage_message();
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(COLOR_BLUE));
refresh();
int c; int c;
while ((c = getch()) != KEY_F(1)) { while ((c = getch()) != KEY_F(1)) {
@@ -198,12 +201,43 @@ void komens_page(koment_type type) {
clrtoeol(); clrtoeol();
refresh(); refresh();
char progress_bar[20];
std::fill(progress_bar, progress_bar + 20, '.');
LimitedInt progress_index(0, 0, sizeof(progress_bar) - 1);
// Download the attachment // Download the attachment
bakaapi::download_attachment( auto future = std::async(
std::launch::async, bakaapi::download_attachment,
resp_from_api["Messages"][item_index(current_item( resp_from_api["Messages"][item_index(current_item(
komens_choise_menu.menu))]["Attachments"][index]["Id"] komens_choise_menu.menu))]["Attachments"][index]["Id"]
.get<std::string>(), .get<std::string>(),
path); path);
while (true) {
if (future.wait_for(std::chrono::seconds(1)) ==
std::future_status::ready) {
// Future has completed
int result = future.get();
if (result != 0) {
attron(COLOR_PAIR(COLOR_RED));
mvprintw(LINES - 1, 0, "Download failed with error code: %d",
result);
attroff(COLOR_PAIR(COLOR_RED));
} else {
attron(COLOR_PAIR(COLOR_GREEN));
mvprintw(LINES - 1, 0, "Download completed successfully");
attroff(COLOR_PAIR(COLOR_GREEN));
}
break;
} else {
progress_bar[progress_index] = '#';
// Future is still running
mvprintw(LINES - 1, 0, "%s", progress_bar);
progress_bar[progress_index] = '.';
progress_index++;
}
}
komens_print_usage_message();
} }
} }
break; break;
@@ -214,11 +248,13 @@ void komens_page(koment_type type) {
current_item(komens_choise_menu.menu))]); current_item(komens_choise_menu.menu))]);
wrefresh(komens_choise_menu.win); wrefresh(komens_choise_menu.win);
} }
unpost_menu(komens_choise_menu.menu);
endwin();
delete_all(&komens_allocated); delete_all(&komens_allocated);
} }
void insert_content(WINDOW *content_win, WINDOW *attachment_win, void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &message) { const json &message) {
wclear(content_win); wclear(content_win);
mvwprintw(content_win, 0, 0, "%s", mvwprintw(content_win, 0, 0, "%s",
html_to_string(message.at("Text")).c_str()); html_to_string(message.at("Text")).c_str());
@@ -269,7 +305,7 @@ void insert_content(WINDOW *content_win, WINDOW *attachment_win,
mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1); mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1);
wattroff(attachment_win, COLOR_PAIR(COLOR_MAGENTA)); wattroff(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
} }
{ // remove duplicating spaces { // remove duplicating edges
unsigned short attachment_win_top, attachment_win_left, unsigned short attachment_win_top, attachment_win_left,
attachment_win_height, attachment_win_width; attachment_win_height, attachment_win_width;
getbegyx(attachment_win, attachment_win_top, attachment_win_left); getbegyx(attachment_win, attachment_win_top, attachment_win_left);
@@ -280,6 +316,10 @@ void insert_content(WINDOW *content_win, WINDOW *attachment_win,
} }
refresh(); refresh();
wrefresh(attachment_win);
} else {
// remove attachment window if there are no attachments
wclear(attachment_win);
wrefresh(attachment_win); wrefresh(attachment_win);
} }
} }
+16 -7
View File
@@ -1,6 +1,7 @@
#include "komens_menu.h" #include "komens_menu.h"
#include "helper_funcs.h" #include "helper_funcs.h"
#include "komens.h" #include "komens.h"
#include "memory.h"
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#include <cstdlib> #include <cstdlib>
@@ -8,15 +9,19 @@
#include <curses.h> #include <curses.h>
#include <menu.h> #include <menu.h>
std::vector<allocation> komens_menu_allocated;
void komens_menu() { void komens_menu() {
current_allocated = &komens_menu_allocated;
wchar_t *choices[] = { wchar_t *choices[] = {
L"received", L"sent", L"noticeboard", L"Exit", nullptr, L"received", L"sent", L"noticeboard", L"Exit", nullptr,
}; };
complete_menu komens_menu; complete_menu komens_menu;
komens_menu_allocated.push_back({COMPLETE_MENU_TYPE, &komens_menu, 1});
int c; int c;
int n_choices, i; int n_choices;
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@@ -30,8 +35,11 @@ void komens_menu() {
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices); n_choices = ARRAY_SIZE(choices);
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); komens_menu.items = new ITEM *[ARRAY_SIZE(choices)];
for (i = 0; i < n_choices; ++i) komens_menu.items_size = ARRAY_SIZE(choices);
for (int i = 0; i < n_choices; ++i)
komens_menu.items[i] = komens_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i])); new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
@@ -89,9 +97,12 @@ void komens_menu() {
} }
komens_page( komens_page(
static_cast<koment_type>(item_index(current_item(komens_menu.menu)))); static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
current_allocated = &komens_menu_allocated;
clear();
pos_menu_cursor(komens_menu.menu); pos_menu_cursor(komens_menu.menu);
refresh();
wrefresh(komens_menu.win); wrefresh(komens_menu.win);
refresh();
redrawwin(komens_menu.win);
break; break;
} }
wrefresh(komens_menu.win); wrefresh(komens_menu.win);
@@ -100,8 +111,6 @@ close_menu:
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(komens_menu.menu); unpost_menu(komens_menu.menu);
free_menu(komens_menu.menu); delete_all(&komens_menu_allocated);
for (i = 0; i < n_choices; ++i)
free_item(komens_menu.items[i]);
endwin(); endwin();
} }
+23 -17
View File
@@ -1,26 +1,30 @@
#include "helper_funcs.h" #include "helper_funcs.h"
#include "komens_menu.h" #include "komens_menu.h"
#include "marks.h" #include "marks.h"
#include "memory.h"
#include "net.h" #include "net.h"
#include "timetable.h" #include "timetable.h"
#include "types.h" #include "types.h"
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <curses.h> #include <curses.h>
#include <menu.h> #include <menu.h>
#include "absence.h"
std::vector<allocation> main_menu_allocated;
void main_menu() { void main_menu() {
wchar_t *choices[] = { current_allocated = &main_menu_allocated;
L"login", L"Marks", L"timetable", L"Komens", const wchar_t *choices[] = {
L"Marks", L"timetable", L"Komens",
L"Homework", L"Absence", L"Exit", nullptr, L"Homework", L"Absence", L"Exit", nullptr,
}; };
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu, void (*choicesFuncs[])() = {marks_page, timetable_page, komens_menu,
nullptr, nullptr, nullptr, nullptr}; nullptr, absence_page, nullptr, nullptr};
complete_menu main_menu; complete_menu main_menu;
main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1});
int c;
int n_choices, i;
/* Initialize curses */ /* Initialize curses */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
@@ -33,14 +37,15 @@ void main_menu() {
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */ /* Create items */
n_choices = ARRAY_SIZE(choices);
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); main_menu.items = new ITEM *[ARRAY_SIZE(choices)];
for (i = 0; i < n_choices; ++i) main_menu.items_size = ARRAY_SIZE(choices);
for (size_t i = 0; i < ARRAY_SIZE(choices); ++i) {
main_menu.items[i] = main_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i])); new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
}
/* Crate menu */ /* Crate menu */
main_menu.menu = new_menu((ITEM **)main_menu.items); main_menu.menu = new_menu(main_menu.items);
/* Create the window to be associated with the menu */ /* Create the window to be associated with the menu */
main_menu.win = newwin(12, 40, 4, 4); main_menu.win = newwin(12, 40, 4, 4);
@@ -73,6 +78,7 @@ void main_menu() {
attroff(COLOR_PAIR(2)); attroff(COLOR_PAIR(2));
refresh(); refresh();
int c;
while ((c = getch()) != KEY_F(1)) { while ((c = getch()) != KEY_F(1)) {
switch (c) { switch (c) {
case KEY_DOWN: case KEY_DOWN:
@@ -88,13 +94,15 @@ void main_menu() {
break; break;
case 10: // ENTER case 10: // ENTER
clear(); clear();
if (item_index(current_item(main_menu.menu)) == n_choices - 1) { if (item_index(current_item(main_menu.menu)) == ARRAY_SIZE(choices) - 1) {
goto close_menu; goto close_menu;
} }
choicesFuncs[item_index(current_item(main_menu.menu))](); choicesFuncs[item_index(current_item(main_menu.menu))]();
current_allocated = &main_menu_allocated;
pos_menu_cursor(main_menu.menu); pos_menu_cursor(main_menu.menu);
refresh();
wrefresh(main_menu.win); wrefresh(main_menu.win);
refresh();
redrawwin(main_menu.win);
break; break;
} }
wrefresh(main_menu.win); wrefresh(main_menu.win);
@@ -103,8 +111,6 @@ close_menu:
/* Unpost and free all the memory taken up */ /* Unpost and free all the memory taken up */
unpost_menu(main_menu.menu); unpost_menu(main_menu.menu);
free_menu(main_menu.menu); delete_all(&main_menu_allocated);
for (i = 0; i < n_choices; ++i)
free_item(main_menu.items[i]);
endwin(); endwin();
} }
+30 -23
View File
@@ -31,22 +31,26 @@ using nlohmann::json;
std::vector<allocation> marks_allocated; std::vector<allocation> marks_allocated;
void init_wins(WINDOW **wins, int n, json marks_json); void init_wins(WINDOW **wins, const int n, const json &marks_json);
void win_show(WINDOW *win, wchar_t *label, int label_color, int width, void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int height, json marks_json, int SubjectIndex); int width, int height, const json &marks_json,
const int SubjectIndex);
void marks_page() { void marks_page() {
current_allocated = &marks_allocated; current_allocated = &marks_allocated;
json resp_from_api; curs_set(0);
{
std::string endpoint = "api/3/marks"; // thanks to lambda i can make this const
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, GET); const json resp_from_api = [&]() -> json {
} const std::string endpoint = "api/3/marks";
size_t size_my_wins = resp_from_api["Subjects"].size(); return bakaapi::get_data_from_endpoint(endpoint, GET);
}();
const size_t size_my_wins = resp_from_api["Subjects"].size();
WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins]; WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins];
marks_allocated.push_back({WINDOW_ARRAY, my_wins, size_my_wins}); marks_allocated.push_back({WINDOW_ARRAY, my_wins, size_my_wins});
size_t size_my_panels = resp_from_api["Subjects"].size(); const size_t size_my_panels = size_my_wins;
PANEL **my_panels = new (std::nothrow) PANEL *[size_my_panels]; PANEL **my_panels = new (std::nothrow) PANEL *[size_my_panels];
marks_allocated.push_back({PANEL_ARRAY, my_panels, size_my_panels}); marks_allocated.push_back({PANEL_ARRAY, my_panels, size_my_panels});
@@ -129,10 +133,11 @@ void marks_page() {
endwin(); endwin();
clear(); clear();
delete_all(&marks_allocated); delete_all(&marks_allocated);
curs_set(1);
} }
/* Put all the windows */ /* Put all the windows */
void init_wins(WINDOW **wins, int n, json marks_json) { void init_wins(WINDOW **wins, const int n, const json &marks_json) {
int x, y, i; int x, y, i;
wchar_t label[1500]; wchar_t label[1500];
@@ -145,12 +150,12 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
// Calculate label and max_text_length to determine window width // Calculate label and max_text_length to determine window width
std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"]; const std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"]; const std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
// Convert to wchar_t // Convert to wchar_t
std::wstring wsub_name = string_to_wstring(sub_name); const std::wstring wsub_name = string_to_wstring(sub_name);
std::wstring wsub_avg_s = string_to_wstring(sub_avg_s); const std::wstring wsub_avg_s = string_to_wstring(sub_avg_s);
// Using swprintf for wide character formatting // Using swprintf for wide character formatting
swprintf(label, sizeof(label) / sizeof(label[0]), L"%ls - avg: %ls", swprintf(label, sizeof(label) / sizeof(label[0]), L"%ls - avg: %ls",
@@ -160,17 +165,18 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (unsigned int j = 0; j < static_cast<unsigned int>( for (unsigned int j = 0; j < static_cast<unsigned int>(
marks_json["Subjects"][i]["Marks"].size()); marks_json["Subjects"][i]["Marks"].size());
j++) { j++) {
std::string caption = const std::string caption =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Caption"]); rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Caption"]);
std::string theme = const std::string theme =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Theme"]); rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Theme"]);
std::wstring wcaption = string_to_wstring(caption); const std::wstring wcaption = string_to_wstring(caption);
std::wstring wtheme = string_to_wstring(theme); const std::wstring wtheme = string_to_wstring(theme);
// Some code that does something and fixes some edge cases // Some code that does something and fixes some edge cases
std::string testCaption = caption + std::format(" {{{}}} [{}]", "X", 0); const std::string testCaption =
std::wstring wTestCaption = string_to_wstring(testCaption); caption + std::format(" {{{}}} [{}]", "X", 0);
const std::wstring wTestCaption = string_to_wstring(testCaption);
max_text_length = max_text_length =
std::max({max_text_length, wTestCaption.length(), wtheme.length()}); std::max({max_text_length, wTestCaption.length(), wtheme.length()});
} }
@@ -203,8 +209,9 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
} }
/* Show the window with a border and a label */ /* Show the window with a border and a label */
void win_show(WINDOW *win, wchar_t *label, int label_color, int width, void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int height, json marks_json, int SubjectIndex) { int width, int height, const json &marks_json,
const int SubjectIndex) {
// is the compiler smoking weed or something, why is it thinking starty is not // is the compiler smoking weed or something, why is it thinking starty is not
// used ?? // used ??
+47 -14
View File
@@ -1,9 +1,34 @@
#include "memory.h" #include "memory.h"
#include "color.h" #include "color.h"
#include "types.h"
#include <curses.h>
#include <iostream> #include <iostream>
#include <menu.h>
#include <ncurses.h> #include <ncurses.h>
#include <panel.h> #include <panel.h>
template <typename T> struct NcursesDeleter {
static void delete_element(T obj);
};
template <> void NcursesDeleter<WINDOW *>::delete_element(WINDOW *win) {
delwin(win);
}
template <> void NcursesDeleter<PANEL *>::delete_element(PANEL *pan) {
del_panel(pan);
}
template <> void NcursesDeleter<ITEM *>::delete_element(ITEM *item) {
free_item(item);
}
template <typename T> void delete_ncurses_arrays(void *ptr, std::size_t size) {
T *array = static_cast<T *>(ptr);
for (std::size_t j = 0; j < size; ++j) {
NcursesDeleter<T>::delete_element(array[j]);
}
delete[] array;
}
void delete_all(std::vector<allocation> *allocated) { void delete_all(std::vector<allocation> *allocated) {
if (allocated == nullptr) { if (allocated == nullptr) {
return; return;
@@ -11,27 +36,26 @@ void delete_all(std::vector<allocation> *allocated) {
for (long long i = allocated->size() - 1; i >= 0; i--) { for (long long i = allocated->size() - 1; i >= 0; i--) {
switch (allocated->at(i).type) { switch (allocated->at(i).type) {
case WINDOW_ARRAY: { case WINDOW_ARRAY: {
WINDOW **windows = static_cast<WINDOW **>(allocated->at(i).ptr); delete_ncurses_arrays<WINDOW *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
delwin(windows[j]);
}
delete[] windows;
break; break;
} }
case PANEL_ARRAY: { case PANEL_ARRAY: {
PANEL **panels = static_cast<PANEL **>(allocated->at(i).ptr); delete_ncurses_arrays<PANEL *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
del_panel(panels[j]);
}
delete[] panels;
break; break;
} }
case ITEM_ARRAY: { case ITEM_ARRAY: {
ITEM **items = static_cast<ITEM **>(allocated->at(i).ptr); delete_ncurses_arrays<ITEM *>(allocated->at(i).ptr,
for (std::size_t j = 0; j < allocated->at(i).size; j++) { allocated->at(i).size);
free_item(items[j]); break;
} }
delete[] items; case CHAR_PTR_ARRAY: {
char **array = static_cast<char **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; ++j) {
delete[] array[j];
}
delete[] array;
break; break;
} }
case GENERIC_ARRAY: case GENERIC_ARRAY:
@@ -46,6 +70,15 @@ void delete_all(std::vector<allocation> *allocated) {
case MENU_TYPE: case MENU_TYPE:
free_menu(static_cast<MENU *>(allocated->at(i).ptr)); free_menu(static_cast<MENU *>(allocated->at(i).ptr));
break; break;
case COMPLETE_MENU_TYPE: {
free_menu(static_cast<complete_menu *>(allocated->at(i).ptr)->menu);
delwin(static_cast<complete_menu *>(allocated->at(i).ptr)->win);
delete_ncurses_arrays<ITEM *>(
static_cast<complete_menu *>(allocated->at(i).ptr)->items,
static_cast<complete_menu *>(allocated->at(i).ptr)->items_size);
break;
}
case GENERIC_TYPE: case GENERIC_TYPE:
delete static_cast<char *>(allocated->at(i).ptr); delete static_cast<char *>(allocated->at(i).ptr);
break; break;
+2
View File
@@ -9,10 +9,12 @@ enum AllocationType {
WINDOW_ARRAY, WINDOW_ARRAY,
PANEL_ARRAY, PANEL_ARRAY,
ITEM_ARRAY, ITEM_ARRAY,
CHAR_PTR_ARRAY,
GENERIC_ARRAY, GENERIC_ARRAY,
WINDOW_TYPE, WINDOW_TYPE,
PANEL_TYPE, PANEL_TYPE,
MENU_TYPE, MENU_TYPE,
COMPLETE_MENU_TYPE,
GENERIC_TYPE GENERIC_TYPE
}; };
+16 -4
View File
@@ -9,6 +9,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/header.h>
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
@@ -111,11 +112,8 @@ std::tuple<std::string, int> send_curl_request(
safe_exit(EINVAL); safe_exit(EINVAL);
} }
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
}
CURLcode curl_return_code = curl_easy_perform(curl); CURLcode curl_return_code = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (curl_return_code != CURLE_OK) { if (curl_return_code != CURLE_OK) {
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: " std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
<< curl_easy_strerror(curl_return_code) << "\n"; << curl_easy_strerror(curl_return_code) << "\n";
@@ -129,6 +127,14 @@ std::tuple<std::string, int> send_curl_request(
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return {response, http_code}; return {response, http_code};
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
// prevent compiler warning
return {"", -1};
}
} }
namespace bakaapi { namespace bakaapi {
@@ -181,7 +187,13 @@ void refresh_access_token() {
if (http_code != 200) { if (http_code != 200) {
std::cerr << RED "[ERROR] " << RESET << http_code std::cerr << RED "[ERROR] " << RESET << http_code
<< "is non 200 response\n"; << "is non 200 response\n";
def_prog_mode();
endwin();
get_input_and_login(); get_input_and_login();
reset_prog_mode();
refresh();
doupdate();
return;
} }
SoRAuthFile(true, response); SoRAuthFile(true, response);
+74 -28
View File
@@ -6,6 +6,7 @@
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#include <bits/chrono.h> #include <bits/chrono.h>
#include <climits>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
@@ -26,22 +27,34 @@ using nlohmann::json;
#define DEFAULT_OFFSET 3 #define DEFAULT_OFFSET 3
#define REMOVED_COLOR_PAIR COLOR_GREEN
#define ROOMCHANGED_COLOR_PAIR COLOR_MAGENTA
#define SUBSTITUTION_COLOR_PAIR COLOR_YELLOW
#define ADDED_COLOR_PAIR COLOR_BLUE
#define HELP_TEXT \
"Arrows/hjkl to select | ENTER to show info | p/n to select weeks |F1 to " \
"exit"
#define HELP_TEXT_LENGTH sizeof(HELP_TEXT) - 1
std::vector<allocation> timetable_allocated; std::vector<allocation> timetable_allocated;
const wchar_t *day_abriviations[] = {nullptr, L"Mo", L"Tu", L"We", const wchar_t *day_abriviations[] = {nullptr, L"Mo", L"Tu", L"We",
L"Th", L"Fr", L"Sa", L"Su"}; L"Th", L"Fr", L"Sa", L"Su"};
void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days, void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
json &resp_from_api); const json &resp_from_api);
void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns, void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
uint16_t cell_width, std::vector<uint8_t> &HourIdLookupTable, uint16_t cell_width,
json &resp_from_api); const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api);
void draw_cells(uint8_t num_of_columns, uint8_t num_of_days, void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
uint16_t cell_width, uint16_t cell_height, uint16_t cell_width, uint16_t cell_height,
std::vector<std::vector<WINDOW *>> &cells, std::vector<std::vector<WINDOW *>> &cells,
std::vector<uint8_t> &HourIdLookupTable, json &resp_from_api); const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api);
uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable, uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable,
uint8_t id) { uint8_t id) {
@@ -53,8 +66,8 @@ uint8_t hour_id_to_index(const std::vector<uint8_t> &HourIdLookupTable,
return 0; return 0;
} }
json *partial_json_by_id(json &resp_from_api, const std::string &what, const json *partial_json_by_id(const json &resp_from_api,
const std::string &id) { const std::string &what, const std::string &id) {
for (uint8_t i = 0; i < resp_from_api[what].size(); i++) { for (uint8_t i = 0; i < resp_from_api[what].size(); i++) {
if (resp_from_api[what][i]["Id"].get<std::string>() == id) { if (resp_from_api[what][i]["Id"].get<std::string>() == id) {
return &resp_from_api[what][i]; return &resp_from_api[what][i];
@@ -63,7 +76,7 @@ json *partial_json_by_id(json &resp_from_api, const std::string &what,
return nullptr; return nullptr;
} }
std::wstring get_data_for_atom(json &resp_from_api, json *atom, std::wstring get_data_for_atom(const json &resp_from_api, const json *atom,
const std::string &from_where, const std::string &from_where,
const std::string &id_key, const std::string &id_key,
const std::string &what) { const std::string &what) {
@@ -74,7 +87,8 @@ std::wstring get_data_for_atom(json &resp_from_api, json *atom,
.get<std::string>()); .get<std::string>());
} }
json *find_atom_by_indexes(json &resp_from_api, uint8_t day_index, const json *
find_atom_by_indexes(const json &resp_from_api, uint8_t day_index,
uint8_t hour_index, uint8_t hour_index,
const std::vector<uint8_t> &HourIdLookupTable) { const std::vector<uint8_t> &HourIdLookupTable) {
for (uint8_t k = 0; k < resp_from_api["Days"][day_index]["Atoms"].size(); for (uint8_t k = 0; k < resp_from_api["Days"][day_index]["Atoms"].size();
@@ -91,6 +105,7 @@ void timetable_page() {
current_allocated = &timetable_allocated; current_allocated = &timetable_allocated;
auto dateSelected = std::chrono::system_clock::now(); auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week: reload_for_new_week:
clear();
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected); std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t); std::tm local_time = *std::localtime(&date_time_t);
@@ -100,7 +115,7 @@ reload_for_new_week:
std::string date_string = "date=" + date_stringstream.str(); std::string date_string = "date=" + date_stringstream.str();
std::string endpoint = "api/3/timetable/actual"; std::string endpoint = "api/3/timetable/actual";
json resp_from_api = const json resp_from_api =
bakaapi::get_data_from_endpoint(endpoint, GET, date_string); bakaapi::get_data_from_endpoint(endpoint, GET, date_string);
// this may be unnecessary but i dont have enaugh data to test it // this may be unnecessary but i dont have enaugh data to test it
@@ -275,9 +290,32 @@ reload_for_new_week:
} }
} }
attron(COLOR_PAIR(COLOR_BLUE)); attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(LINES - 2, 0, mvprintw(LINES - 2, 0, HELP_TEXT);
"Arrows/hjkl to select | ENTER to show info | p/n to select weeks " attroff(COLOR_PAIR(COLOR_BLUE));
"|F1 to exit");
{
constexpr char *change_types[] = {"Canceled/Removed", "RoomChanged",
"Substitution", "Added"};
constexpr uint8_t change_types_colors[] = {
REMOVED_COLOR_PAIR, ROOMCHANGED_COLOR_PAIR, SUBSTITUTION_COLOR_PAIR,
ADDED_COLOR_PAIR};
for (uint8_t i = 0; i < ARRAY_SIZE(change_types); i++) {
init_pair(UCHAR_MAX - i, COLOR_BLACK, change_types_colors[i]);
}
uint8_t text_offset = 1;
for (uint8_t i = 0; i < ARRAY_SIZE(change_types); i++) {
attron(COLOR_PAIR(UCHAR_MAX - i));
mvprintw(LINES - 2, HELP_TEXT_LENGTH + text_offset, "%s",
change_types[i]);
attroff(COLOR_PAIR(UCHAR_MAX - i));
text_offset += strlen(change_types[i]) + 1;
}
}
attron(COLOR_PAIR(COLOR_BLUE));
{ {
std::tm end_week = local_time; std::tm end_week = local_time;
std::tm start_week = local_time; std::tm start_week = local_time;
@@ -292,12 +330,13 @@ reload_for_new_week:
.get<uint8_t>(); .get<uint8_t>();
// Calculate days back to start day (handles week wraparound) // Calculate days back to start day (handles week wraparound)
int days_back = (current_wday >= start_day) uint8_t days_back = (current_wday >= start_day)
? (current_wday - start_day) ? (current_wday - start_day)
: (current_wday + 7 - start_day); : (current_wday + 7 - start_day);
// Calculate days forward to end day (handles week wraparound) // Calculate days forward to end day (handles week wraparound)
int days_forward = (current_wday <= end_day) ? (end_day - current_wday) uint8_t days_forward = (current_wday <= end_day)
? (end_day - current_wday)
: (end_day + 7 - current_wday); : (end_day + 7 - current_wday);
// Adjust dates // Adjust dates
@@ -384,8 +423,8 @@ reload_for_new_week:
goto reload_for_new_week; goto reload_for_new_week;
break; break;
case 10: // ENTER case 10: // ENTER
json *atom = find_atom_by_indexes(resp_from_api, selected_cell.y, const json *atom = find_atom_by_indexes(
selected_cell.x, HourIdLookupTable); resp_from_api, selected_cell.y, selected_cell.x, HourIdLookupTable);
if (atom == nullptr) { if (atom == nullptr) {
std::cerr << RED "[ERROR]" << RESET " Selector at invalid position\n"; std::cerr << RED "[ERROR]" << RESET " Selector at invalid position\n";
safe_exit(129); safe_exit(129);
@@ -535,7 +574,7 @@ reload_for_new_week:
} }
void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days, void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
json &resp_from_api) { const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_days; i++) { for (uint8_t i = 0; i < num_of_days; i++) {
// this wont draw left boarder window making it so it looks partially // this wont draw left boarder window making it so it looks partially
// offscreen // offscreen
@@ -549,8 +588,9 @@ void draw_days(WINDOW **&day_windows, uint16_t cell_height, uint8_t num_of_days,
} }
void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns, void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
uint16_t cell_width, std::vector<uint8_t> &HourIdLookupTable, uint16_t cell_width,
json &resp_from_api) { const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_columns; i++) { for (uint8_t i = 0; i < num_of_columns; i++) {
wborder(lesson_windows[i], 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, 0, 0); wborder(lesson_windows[i], 0, 0, ' ', 0, ACS_VLINE, ACS_VLINE, 0, 0);
std::wstring caption; std::wstring caption;
@@ -594,11 +634,13 @@ void draw_lessons(WINDOW **&lesson_windows, uint8_t num_of_columns,
void draw_cells(uint8_t num_of_columns, uint8_t num_of_days, void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
uint16_t cell_width, uint16_t cell_height, uint16_t cell_width, uint16_t cell_height,
std::vector<std::vector<WINDOW *>> &cells, std::vector<std::vector<WINDOW *>> &cells,
std::vector<uint8_t> &HourIdLookupTable, json &resp_from_api) { const std::vector<uint8_t> &HourIdLookupTable,
const json &resp_from_api) {
for (uint8_t i = 0; i < num_of_days; i++) { for (uint8_t i = 0; i < num_of_days; i++) {
for (uint8_t j = 0; j < num_of_columns; j++) { for (uint8_t j = 0; j < num_of_columns; j++) {
json *atom = find_atom_by_indexes(resp_from_api, i, j, HourIdLookupTable); const json *atom =
find_atom_by_indexes(resp_from_api, i, j, HourIdLookupTable);
if (atom == nullptr) { if (atom == nullptr) {
continue; continue;
} }
@@ -612,20 +654,24 @@ void draw_cells(uint8_t num_of_columns, uint8_t num_of_days,
hash_djb2a(atom->at("Change")["ChangeType"].get<std::string>())) { hash_djb2a(atom->at("Change")["ChangeType"].get<std::string>())) {
case "Canceled"_sh: case "Canceled"_sh:
case "Removed"_sh: case "Removed"_sh:
wattron(cells[i][j], COLOR_PAIR(COLOR_GREEN)); wattron(cells[i][j], COLOR_PAIR(REMOVED_COLOR_PAIR));
box(cells[i][j], 0, 0); box(cells[i][j], 0, 0);
wattroff(cells[i][j], COLOR_PAIR(COLOR_GREEN)); wattroff(cells[i][j], COLOR_PAIR(REMOVED_COLOR_PAIR));
break; break;
case "RoomChanged"_sh: case "RoomChanged"_sh:
case "Substitution"_sh: wattron(cells[i][j], COLOR_PAIR(ROOMCHANGED_COLOR_PAIR));
wattron(cells[i][j], COLOR_PAIR(COLOR_YELLOW));
box(cells[i][j], 0, 0); box(cells[i][j], 0, 0);
wattroff(cells[i][j], COLOR_PAIR(COLOR_YELLOW)); wattroff(cells[i][j], COLOR_PAIR(ROOMCHANGED_COLOR_PAIR));
break;
case "Substitution"_sh:
wattron(cells[i][j], COLOR_PAIR(SUBSTITUTION_COLOR_PAIR));
box(cells[i][j], 0, 0);
wattroff(cells[i][j], COLOR_PAIR(SUBSTITUTION_COLOR_PAIR));
break; break;
case "Added"_sh: case "Added"_sh:
wattron(cells[i][j], COLOR_PAIR(COLOR_BLUE)); wattron(cells[i][j], COLOR_PAIR(ADDED_COLOR_PAIR));
box(cells[i][j], 0, 0); box(cells[i][j], 0, 0);
wattroff(cells[i][j], COLOR_PAIR(COLOR_BLUE)); wattroff(cells[i][j], COLOR_PAIR(ADDED_COLOR_PAIR));
break; break;
default: default:
// TODO add error handling // TODO add error handling
+1
View File
@@ -148,6 +148,7 @@ struct SelectorType {
struct complete_menu { struct complete_menu {
WINDOW *win; WINDOW *win;
ITEM **items; ITEM **items;
size_t items_size;
MENU *menu; MENU *menu;
}; };