add attachment view
This commit is contained in:
parent
575101780d
commit
1992b0e166
160
src/komens.cpp
160
src/komens.cpp
@ -2,24 +2,31 @@
|
||||
#include "helper_funcs.h"
|
||||
#include "memory.h"
|
||||
#include "net.h"
|
||||
#include "types.h"
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
#include <cwchar>
|
||||
#include <menu.h>
|
||||
#include <ncurses.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define WIN_HIGHT 40
|
||||
#define MAIN_WIN_PROPORTION 0.714f
|
||||
|
||||
#define MAIN_WIN_HIGHT (roundf(LINES * MAIN_WIN_PROPORTION))
|
||||
|
||||
#define DEFAULT_OFSET 4
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
std::vector<allocation> komens_allocated;
|
||||
|
||||
void insert_content(WINDOW *content_window, size_t i, json &resp_from_api);
|
||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i,
|
||||
json &resp_from_api);
|
||||
|
||||
void komens_page(koment_type type) {
|
||||
current_allocated = &komens_allocated;
|
||||
@ -48,9 +55,13 @@ void komens_page(koment_type type) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
init_pair(i, i, COLOR_BLACK);
|
||||
}
|
||||
|
||||
complete_menu komens_choise_menu;
|
||||
|
||||
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});
|
||||
komens_choise_menu.items = new ITEM *[num_of_komens + 1];
|
||||
komens_allocated.push_back(
|
||||
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
|
||||
|
||||
char **title_bufs = new char *[num_of_komens];
|
||||
char **name_bufs = new char *[num_of_komens];
|
||||
@ -94,46 +105,48 @@ void komens_page(koment_type type) {
|
||||
name_bufs[i] = new char[strlen(tmp_buf) + 1];
|
||||
strlcpy(name_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
||||
|
||||
komens_items[i] = new_item(title_bufs[i], name_bufs[i]);
|
||||
komens_choise_menu.items[i] = new_item(title_bufs[i], name_bufs[i]);
|
||||
}
|
||||
max_item_lenght = 3 + max_title_lenght + 1 + max_name_lenght;
|
||||
}
|
||||
komens_items[num_of_komens] = nullptr;
|
||||
komens_choise_menu.items[num_of_komens] = nullptr;
|
||||
|
||||
MENU *komens_choise_menu = new_menu(komens_items);
|
||||
komens_allocated.push_back({MENU_TYPE, komens_choise_menu, 1});
|
||||
komens_choise_menu.menu = new_menu(komens_choise_menu.items);
|
||||
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
|
||||
|
||||
WINDOW *komens_choise_menu_win =
|
||||
newwin(WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
|
||||
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu_win, 1});
|
||||
komens_choise_menu.win =
|
||||
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
|
||||
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu.win, 1});
|
||||
|
||||
set_menu_win(komens_choise_menu, komens_choise_menu_win);
|
||||
set_menu_sub(komens_choise_menu,
|
||||
derwin(komens_choise_menu_win, WIN_HIGHT - 10, max_item_lenght,
|
||||
DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
||||
set_menu_format(komens_choise_menu, WIN_HIGHT - 5, 1);
|
||||
set_menu_win(komens_choise_menu.menu, komens_choise_menu.win);
|
||||
set_menu_sub(komens_choise_menu.menu,
|
||||
derwin(komens_choise_menu.win, MAIN_WIN_HIGHT - 10,
|
||||
max_item_lenght, DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
||||
set_menu_format(komens_choise_menu.menu, MAIN_WIN_HIGHT - 5, 1);
|
||||
|
||||
set_menu_mark(komens_choise_menu, " * ");
|
||||
set_menu_mark(komens_choise_menu.menu, " * ");
|
||||
|
||||
box(komens_choise_menu_win, 0, 0);
|
||||
box(komens_choise_menu.win, 0, 0);
|
||||
|
||||
wprint_in_middle(komens_choise_menu_win, 1, 0, max_item_lenght, L"Komens",
|
||||
wprint_in_middle(komens_choise_menu.win, 1, 0, max_item_lenght, L"Komens",
|
||||
COLOR_PAIR(1));
|
||||
mvwaddch(komens_choise_menu_win, 2, 0, ACS_LTEE);
|
||||
mvwhline(komens_choise_menu_win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
||||
mvwaddch(komens_choise_menu_win, 2, max_item_lenght, ACS_RTEE);
|
||||
mvwaddch(komens_choise_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(komens_choise_menu.win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
||||
mvwaddch(komens_choise_menu.win, 2, max_item_lenght, ACS_RTEE);
|
||||
|
||||
post_menu(komens_choise_menu);
|
||||
wrefresh(komens_choise_menu_win);
|
||||
post_menu(komens_choise_menu.menu);
|
||||
wrefresh(komens_choise_menu.win);
|
||||
|
||||
WINDOW *content_window =
|
||||
newwin(WIN_HIGHT, COLS - max_item_lenght - DEFAULT_OFSET - 1,
|
||||
WINDOW *content_win =
|
||||
newwin(MAIN_WIN_HIGHT, COLS - max_item_lenght - DEFAULT_OFSET - 1,
|
||||
DEFAULT_OFSET, DEFAULT_OFSET + max_item_lenght + 1);
|
||||
komens_allocated.push_back({WINDOW_TYPE, content_window, 1});
|
||||
box(content_window, 0, 0);
|
||||
insert_content(content_window, item_index(current_item(komens_choise_menu)),
|
||||
komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
|
||||
|
||||
WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
|
||||
|
||||
insert_content(content_win, attachment_win,
|
||||
item_index(current_item(komens_choise_menu.menu)),
|
||||
resp_from_api);
|
||||
wrefresh(content_window);
|
||||
|
||||
attron(COLOR_PAIR(COLOR_BLUE));
|
||||
mvprintw(LINES - 2, 0,
|
||||
@ -148,25 +161,94 @@ void komens_page(koment_type type) {
|
||||
case KEY_DOWN:
|
||||
case KEY_NPAGE:
|
||||
case 'j':
|
||||
menu_driver(komens_choise_menu, REQ_DOWN_ITEM);
|
||||
menu_driver(komens_choise_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
case 'k':
|
||||
menu_driver(komens_choise_menu, REQ_UP_ITEM);
|
||||
menu_driver(komens_choise_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
}
|
||||
insert_content(content_window, item_index(current_item(komens_choise_menu)),
|
||||
insert_content(content_win, attachment_win,
|
||||
item_index(current_item(komens_choise_menu.menu)),
|
||||
resp_from_api);
|
||||
wrefresh(content_window);
|
||||
wrefresh(komens_choise_menu_win);
|
||||
wrefresh(komens_choise_menu.win);
|
||||
}
|
||||
delete_all(&komens_allocated);
|
||||
}
|
||||
|
||||
void insert_content(WINDOW *content_window, size_t i, json &resp_from_api) {
|
||||
wclear(content_window);
|
||||
mvwprintw(content_window, 0, 0, "%s",
|
||||
void insert_content(WINDOW *content_win, WINDOW *attachment_win, size_t i,
|
||||
json &resp_from_api) {
|
||||
wclear(content_win);
|
||||
mvwprintw(content_win, 0, 0, "%s",
|
||||
html_to_string(resp_from_api.at("Messages")[i]["Text"]).c_str());
|
||||
}
|
||||
wrefresh(content_win);
|
||||
if (!resp_from_api.at("Messages")[i]["Attachments"].empty()) {
|
||||
|
||||
size_t max_item_lenght = 0;
|
||||
{
|
||||
size_t max_name_lenght = 0;
|
||||
size_t max_size_lenght = 0;
|
||||
size_t tmp_lenght;
|
||||
|
||||
for (size_t j = 0;
|
||||
j < resp_from_api.at("Messages")[i]["Attachments"].size(); j++) {
|
||||
tmp_lenght = resp_from_api.at("Messages")[i]["Attachments"][j]["Name"]
|
||||
.get<std::string>()
|
||||
.length();
|
||||
if (tmp_lenght > max_name_lenght) {
|
||||
max_name_lenght = tmp_lenght;
|
||||
}
|
||||
|
||||
tmp_lenght =
|
||||
std::to_string(
|
||||
resp_from_api.at("Messages")[i]["Attachments"][j]["Size"]
|
||||
.get<size_t>())
|
||||
.length();
|
||||
if (tmp_lenght > max_size_lenght) {
|
||||
max_size_lenght = tmp_lenght;
|
||||
}
|
||||
}
|
||||
|
||||
max_item_lenght = 3 + max_name_lenght + 1 + max_size_lenght;
|
||||
}
|
||||
|
||||
mvwin(attachment_win, MAIN_WIN_HIGHT + DEFAULT_OFSET + 1,
|
||||
COLS - max_item_lenght - 2);
|
||||
wresize(attachment_win, LINES - (MAIN_WIN_HIGHT + DEFAULT_OFSET + 1),
|
||||
max_item_lenght + 2);
|
||||
|
||||
wborder(attachment_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
|
||||
print_in_middle(attachment_win, 0, 0, max_item_lenght + 2, "Attachments",
|
||||
COLOR_RED);
|
||||
for (size_t j = 0;
|
||||
j < resp_from_api.at("Messages")[i]["Attachments"].size(); j++) {
|
||||
|
||||
mvwprintw(attachment_win, j + 1, 2, "%s %s",
|
||||
resp_from_api.at("Messages")[i]["Attachments"][j]["Name"]
|
||||
.get<std::string>()
|
||||
.c_str(),
|
||||
std::to_string(
|
||||
resp_from_api.at("Messages")[i]["Attachments"][j]["Size"]
|
||||
.get<size_t>())
|
||||
.c_str());
|
||||
|
||||
wattron(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
||||
mvwprintw(attachment_win, j + 1, 0, "%zu>", j + 1);
|
||||
wattroff(attachment_win, COLOR_PAIR(COLOR_MAGENTA));
|
||||
}
|
||||
{ // remove duplicating spaces
|
||||
unsigned short attachment_win_top, attachment_win_left,
|
||||
attachment_win_height, attachment_win_width;
|
||||
getbegyx(attachment_win, attachment_win_top, attachment_win_left);
|
||||
getmaxyx(attachment_win, attachment_win_height, attachment_win_width);
|
||||
|
||||
mvvline(attachment_win_top, attachment_win_left - 1, ' ',
|
||||
attachment_win_height);
|
||||
}
|
||||
refresh();
|
||||
|
||||
wrefresh(attachment_win);
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "helper_funcs.h"
|
||||
#include "komens.h"
|
||||
#include "net.h"
|
||||
#include "types.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
@ -12,10 +13,9 @@ void komens_menu() {
|
||||
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
|
||||
};
|
||||
|
||||
ITEM **my_items;
|
||||
complete_menu komens_menu;
|
||||
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
@ -30,37 +30,37 @@ void komens_menu() {
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
my_items[i] =
|
||||
komens_menu.items[i] =
|
||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
komens_menu.menu = new_menu(komens_menu.items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(12, 40, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
komens_menu.win = newwin(12, 40, 4, 4);
|
||||
keypad(komens_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_win(komens_menu.menu, komens_menu.win);
|
||||
set_menu_sub(komens_menu.menu, derwin(komens_menu.win, 8, 38, 3, 1));
|
||||
set_menu_format(komens_menu.menu, 7, 1);
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(my_menu, " * ");
|
||||
set_menu_mark(komens_menu.menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
box(komens_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);
|
||||
wprint_in_middle(komens_menu.win, 1, 0, 40, L"Komens Menu", COLOR_PAIR(1));
|
||||
mvwaddch(komens_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(komens_menu.win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(komens_menu.win, 2, 39, ACS_RTEE);
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
post_menu(komens_menu.menu);
|
||||
wrefresh(komens_menu.win);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 2, 0,
|
||||
@ -74,33 +74,34 @@ void komens_menu() {
|
||||
case KEY_DOWN:
|
||||
case KEY_NPAGE:
|
||||
case 'j':
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
menu_driver(komens_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
case 'k':
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
menu_driver(komens_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: // ENTER
|
||||
clear();
|
||||
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
||||
if (item_index(current_item(komens_menu.menu)) == n_choices - 1) {
|
||||
goto close_menu;
|
||||
}
|
||||
komens_page(static_cast<koment_type>(item_index(current_item(my_menu))));
|
||||
pos_menu_cursor(my_menu);
|
||||
komens_page(
|
||||
static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
|
||||
pos_menu_cursor(komens_menu.menu);
|
||||
refresh();
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(komens_menu.win);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(komens_menu.win);
|
||||
}
|
||||
close_menu:
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
unpost_menu(komens_menu.menu);
|
||||
free_menu(komens_menu.menu);
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
free_item(komens_menu.items[i]);
|
||||
endwin();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include "marks.h"
|
||||
#include "net.h"
|
||||
#include "timetable.h"
|
||||
#include "types.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <curses.h>
|
||||
@ -15,10 +16,10 @@ void main_menu() {
|
||||
};
|
||||
void (*choicesFuncs[])() = {nullptr, marks_page, timetable_page, komens_menu,
|
||||
nullptr, nullptr, nullptr, nullptr};
|
||||
ITEM **my_items;
|
||||
|
||||
complete_menu main_menu;
|
||||
|
||||
int c;
|
||||
MENU *my_menu;
|
||||
WINDOW *my_menu_win;
|
||||
int n_choices, i;
|
||||
|
||||
/* Initialize curses */
|
||||
@ -33,37 +34,37 @@ void main_menu() {
|
||||
|
||||
/* Create items */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
my_items[i] =
|
||||
main_menu.items[i] =
|
||||
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
|
||||
|
||||
/* Crate menu */
|
||||
my_menu = new_menu((ITEM **)my_items);
|
||||
main_menu.menu = new_menu((ITEM **)main_menu.items);
|
||||
|
||||
/* Create the window to be associated with the menu */
|
||||
my_menu_win = newwin(12, 40, 4, 4);
|
||||
keypad(my_menu_win, TRUE);
|
||||
main_menu.win = newwin(12, 40, 4, 4);
|
||||
keypad(main_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_win(main_menu.menu, main_menu.win);
|
||||
set_menu_sub(main_menu.menu, derwin(main_menu.win, 8, 38, 3, 1));
|
||||
set_menu_format(main_menu.menu, 7, 1);
|
||||
|
||||
/* Set menu mark to the string " * " */
|
||||
set_menu_mark(my_menu, " * ");
|
||||
set_menu_mark(main_menu.menu, " * ");
|
||||
|
||||
/* Print a border around the main window and print a title */
|
||||
box(my_menu_win, 0, 0);
|
||||
box(main_menu.win, 0, 0);
|
||||
|
||||
wprint_in_middle(my_menu_win, 1, 0, 40, L"Main 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);
|
||||
wprint_in_middle(main_menu.win, 1, 0, 40, L"Main Menu", COLOR_PAIR(1));
|
||||
mvwaddch(main_menu.win, 2, 0, ACS_LTEE);
|
||||
mvwhline(main_menu.win, 2, 1, ACS_HLINE, 38);
|
||||
mvwaddch(main_menu.win, 2, 39, ACS_RTEE);
|
||||
|
||||
/* Post the menu */
|
||||
post_menu(my_menu);
|
||||
wrefresh(my_menu_win);
|
||||
post_menu(main_menu.menu);
|
||||
wrefresh(main_menu.win);
|
||||
|
||||
attron(COLOR_PAIR(2));
|
||||
mvprintw(LINES - 2, 0,
|
||||
@ -77,33 +78,33 @@ void main_menu() {
|
||||
case KEY_DOWN:
|
||||
case KEY_NPAGE:
|
||||
case 'j':
|
||||
menu_driver(my_menu, REQ_DOWN_ITEM);
|
||||
menu_driver(main_menu.menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
|
||||
case KEY_UP:
|
||||
case KEY_PPAGE:
|
||||
case 'k':
|
||||
menu_driver(my_menu, REQ_UP_ITEM);
|
||||
menu_driver(main_menu.menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case 10: // ENTER
|
||||
clear();
|
||||
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
||||
if (item_index(current_item(main_menu.menu)) == n_choices - 1) {
|
||||
goto close_menu;
|
||||
}
|
||||
choicesFuncs[item_index(current_item(my_menu))]();
|
||||
pos_menu_cursor(my_menu);
|
||||
choicesFuncs[item_index(current_item(main_menu.menu))]();
|
||||
pos_menu_cursor(main_menu.menu);
|
||||
refresh();
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(main_menu.win);
|
||||
break;
|
||||
}
|
||||
wrefresh(my_menu_win);
|
||||
wrefresh(main_menu.win);
|
||||
}
|
||||
close_menu:
|
||||
|
||||
/* Unpost and free all the memory taken up */
|
||||
unpost_menu(my_menu);
|
||||
free_menu(my_menu);
|
||||
unpost_menu(main_menu.menu);
|
||||
free_menu(main_menu.menu);
|
||||
for (i = 0; i < n_choices; ++i)
|
||||
free_item(my_items[i]);
|
||||
free_item(main_menu.items[i]);
|
||||
endwin();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#define _ba_ty_hg_
|
||||
|
||||
#include <istream>
|
||||
#include <menu.h>
|
||||
struct Config {
|
||||
bool verbose = false;
|
||||
bool ignoressl = false;
|
||||
@ -144,4 +145,10 @@ struct SelectorType {
|
||||
: x(xArg, min_limit_x, max_limit_x), y(yArg, min_limit_y, max_limit_y) {}
|
||||
};
|
||||
|
||||
struct complete_menu {
|
||||
WINDOW *win;
|
||||
ITEM **items;
|
||||
MENU *menu;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user