Compare commits
5 Commits
v0.1
..
4c383fce4c
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c383fce4c | |||
| 86b9ea812d | |||
| 0119429514 | |||
| 025c51c3ad | |||
| 1a882baab6 |
@@ -34,6 +34,7 @@ void safe_exit(int code) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
endwin();
|
||||||
|
|
||||||
exit(code);
|
exit(code);
|
||||||
}
|
}
|
||||||
@@ -109,4 +110,21 @@ void print_in_middle(WINDOW *win, int starty, int startx, int width,
|
|||||||
mvwprintw(win, y, x, "%s", string);
|
mvwprintw(win, y, x, "%s", string);
|
||||||
wattroff(win, color);
|
wattroff(win, color);
|
||||||
refresh();
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const std::string WHITESPACE = " \n\r\t\f\v";
|
||||||
|
|
||||||
|
std::string ltrim(const std::string &s) {
|
||||||
|
size_t start = s.find_first_not_of(WHITESPACE);
|
||||||
|
return (start == std::string::npos) ? "" : s.substr(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string rtrim(const std::string &s) {
|
||||||
|
size_t end = s.find_last_not_of(WHITESPACE);
|
||||||
|
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string rm_tr_le_whitespace(const std::string &s) {
|
||||||
|
return rtrim(ltrim(s));
|
||||||
}
|
}
|
||||||
+2
-1
@@ -5,4 +5,5 @@ std::string bool_to_string(bool bool_in);
|
|||||||
std::string SoRAuthFile(bool save, std::string data);
|
std::string SoRAuthFile(bool save, std::string data);
|
||||||
void get_input_and_login();
|
void get_input_and_login();
|
||||||
void print_in_middle(WINDOW *win, int starty, int startx, int width,
|
void print_in_middle(WINDOW *win, int starty, int startx, int width,
|
||||||
char *string, chtype color);
|
char *string, chtype color);
|
||||||
|
std::string rm_tr_le_whitespace(const std::string &s);
|
||||||
+89
-22
@@ -1,13 +1,17 @@
|
|||||||
#include "marks.h"
|
#include "marks.h"
|
||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
|
#include <format>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <menu.h>
|
#include <menu.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <panel.h>
|
#include <panel.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
@@ -40,14 +44,19 @@ SOFTWARE.
|
|||||||
#define NLINES 10
|
#define NLINES 10
|
||||||
#define NCOLS 40
|
#define NCOLS 40
|
||||||
|
|
||||||
|
#define DEFAULT_X_OFFSET 10
|
||||||
|
#define DEFAULT_Y_OFFSET 2
|
||||||
|
|
||||||
|
#define DEFAULT_PADDING 4
|
||||||
|
|
||||||
void init_wins(WINDOW **wins, int n, json marks_json);
|
void init_wins(WINDOW **wins, int n, json marks_json);
|
||||||
void win_show(WINDOW *win, char *label, int label_color);
|
void win_show(WINDOW *win, char *label, int label_color, int width, int height,
|
||||||
|
json marks_json, int SubjectIndex);
|
||||||
|
|
||||||
void marks_page() {
|
void marks_page() {
|
||||||
|
|
||||||
// DONT FORGET TO UNCOMMENT
|
// DONT FORGET TO UNCOMMENT
|
||||||
// json resp_from_api = bakaapi::get_grades();
|
// json resp_from_api = bakaapi::get_grades();
|
||||||
std::ifstream f("test-data/marks2.json");
|
std::ifstream f("test-data/marks3.json");
|
||||||
json resp_from_api = json::parse(f);
|
json resp_from_api = json::parse(f);
|
||||||
|
|
||||||
WINDOW **my_wins;
|
WINDOW **my_wins;
|
||||||
@@ -69,8 +78,10 @@ void marks_page() {
|
|||||||
noecho();
|
noecho();
|
||||||
keypad(stdscr, TRUE);
|
keypad(stdscr, TRUE);
|
||||||
|
|
||||||
|
std::clog << COLS << " " << LINES << std::endl;
|
||||||
|
|
||||||
/* Initialize all the colors */
|
/* Initialize all the colors */
|
||||||
for (size_t i = 0; i < 8; i++) {
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
init_pair(i, i, COLOR_BLACK);
|
init_pair(i, i, COLOR_BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +108,7 @@ void marks_page() {
|
|||||||
attroff(COLOR_PAIR(4));
|
attroff(COLOR_PAIR(4));
|
||||||
doupdate();
|
doupdate();
|
||||||
|
|
||||||
top = my_panels[2];
|
top = my_panels[resp_from_api["Subjects"].size() - 1];
|
||||||
while ((ch = getch()) != KEY_F(1)) {
|
while ((ch = getch()) != KEY_F(1)) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 9:
|
case 9:
|
||||||
@@ -109,6 +120,8 @@ void marks_page() {
|
|||||||
doupdate();
|
doupdate();
|
||||||
}
|
}
|
||||||
endwin();
|
endwin();
|
||||||
|
delete[] my_wins;
|
||||||
|
delete[] my_panels;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Put all the windows */
|
/* Put all the windows */
|
||||||
@@ -116,32 +129,59 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
|
|||||||
int x, y, i;
|
int x, y, i;
|
||||||
char label[1500];
|
char label[1500];
|
||||||
|
|
||||||
y = 2;
|
y = DEFAULT_Y_OFFSET;
|
||||||
x = 10;
|
x = DEFAULT_X_OFFSET;
|
||||||
uint8_t curent_color = 0;
|
uint8_t curent_color = 0;
|
||||||
for (i = 0; i < n; ++i) {
|
|
||||||
wins[i] = newwin(NLINES, NCOLS, y, x);
|
|
||||||
{
|
|
||||||
std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
|
|
||||||
std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
|
|
||||||
|
|
||||||
sprintf(label, "%s - avg: %s", sub_name.c_str(), sub_avg_s.c_str());
|
int MaxHight = 0;
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
|
||||||
|
// Calculate label and max_text_length to determine window width
|
||||||
|
std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
|
||||||
|
std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
|
||||||
|
sprintf(label, "%s - avg: %s", sub_name.c_str(), sub_avg_s.c_str());
|
||||||
|
|
||||||
|
size_t max_text_length = strlen(label);
|
||||||
|
for (int j = 0; j < marks_json["Subjects"][i]["Marks"].size(); j++) {
|
||||||
|
std::string caption = marks_json["Subjects"][i]["Marks"][j]["Caption"];
|
||||||
|
std::string theme = marks_json["Subjects"][i]["Marks"][j]["Theme"];
|
||||||
|
caption = rm_tr_le_whitespace(caption);
|
||||||
|
theme = rm_tr_le_whitespace(theme);
|
||||||
|
max_text_length =
|
||||||
|
std::max({max_text_length, caption.length(), theme.length()});
|
||||||
}
|
}
|
||||||
curent_color++;
|
|
||||||
if (curent_color >= 7) {
|
int width = max_text_length + DEFAULT_PADDING;
|
||||||
curent_color = 1;
|
|
||||||
|
// hanndle windows overflowing off screen
|
||||||
|
if (x + width > COLS) {
|
||||||
|
x = DEFAULT_X_OFFSET;
|
||||||
|
y += MaxHight + 2;
|
||||||
|
MaxHight = 0;
|
||||||
}
|
}
|
||||||
win_show(wins[i], label, curent_color);
|
|
||||||
x += 40;
|
if (marks_json["Subjects"][i]["Marks"].size() * 2 + DEFAULT_PADDING >
|
||||||
|
MaxHight) {
|
||||||
|
MaxHight =
|
||||||
|
marks_json["Subjects"][i]["Marks"].size() * 2 + DEFAULT_PADDING;
|
||||||
|
}
|
||||||
|
|
||||||
|
wins[i] = newwin(NLINES, NCOLS, y, x);
|
||||||
|
win_show(wins[i], label, curent_color + 1, width,
|
||||||
|
marks_json["Subjects"][i]["Marks"].size() * 2 + DEFAULT_PADDING,
|
||||||
|
marks_json, i);
|
||||||
|
|
||||||
|
curent_color = (curent_color + 1) % 7;
|
||||||
|
x += width + 5;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show the window with a border and a label */
|
/* Show the window with a border and a label */
|
||||||
void win_show(WINDOW *win, char *label, int label_color) {
|
void win_show(WINDOW *win, char *label, int label_color, int width, int height,
|
||||||
int startx, starty, height, width;
|
json marks_json, int SubjectIndex) {
|
||||||
height = 20;
|
int startx, starty;
|
||||||
|
|
||||||
wresize(win, height, strlen(label) + 4);
|
wresize(win, height, width);
|
||||||
|
|
||||||
getbegyx(win, starty, startx);
|
getbegyx(win, starty, startx);
|
||||||
getmaxyx(win, height, width);
|
getmaxyx(win, height, width);
|
||||||
@@ -152,4 +192,31 @@ void win_show(WINDOW *win, char *label, int label_color) {
|
|||||||
mvwaddch(win, 2, width - 1, ACS_RTEE);
|
mvwaddch(win, 2, width - 1, ACS_RTEE);
|
||||||
|
|
||||||
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
|
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));
|
||||||
|
|
||||||
|
char CaptionBuf[1500];
|
||||||
|
char ThemeBuf[1500];
|
||||||
|
std::string Caption;
|
||||||
|
int AdditionalOffset = 0;
|
||||||
|
for (size_t i = 0; i < marks_json["Subjects"][SubjectIndex]["Marks"].size();
|
||||||
|
i++) {
|
||||||
|
Caption = marks_json["Subjects"][SubjectIndex]["Marks"][i]["Caption"];
|
||||||
|
Caption = rm_tr_le_whitespace(Caption);
|
||||||
|
Caption.append(std::format(
|
||||||
|
" - {{{}}} [{}]",
|
||||||
|
marks_json["Subjects"][SubjectIndex]["Marks"][i]["MarkText"]
|
||||||
|
.get<std::string>(),
|
||||||
|
marks_json["Subjects"][SubjectIndex]["Marks"][i]["Weight"].get<int>()));
|
||||||
|
strncpy(CaptionBuf, Caption.c_str(), sizeof(CaptionBuf));
|
||||||
|
print_in_middle(win, 3 + i + AdditionalOffset, 0, width, CaptionBuf,
|
||||||
|
COLOR_PAIR(label_color));
|
||||||
|
|
||||||
|
strncpy(ThemeBuf,
|
||||||
|
marks_json["Subjects"][SubjectIndex]["Marks"][i]["Theme"]
|
||||||
|
.get<std::string>()
|
||||||
|
.c_str(),
|
||||||
|
sizeof(ThemeBuf));
|
||||||
|
print_in_middle(win, 3 + i + 1 + AdditionalOffset, 0, width, ThemeBuf,
|
||||||
|
COLOR_PAIR(label_color));
|
||||||
|
AdditionalOffset++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user