Compare commits
6 Commits
50ef8d9fda
...
v0.8
| Author | SHA1 | Date | |
|---|---|---|---|
| 575101780d | |||
| 48b4f9cda8 | |||
| 5173a05a09 | |||
| fe9735fe58 | |||
| e4d67c2163 | |||
| c61a354be8 |
@@ -29,7 +29,7 @@ $(BIN_PATH)/bakatui: $(OBJ_FILES)
|
|||||||
$(CPPC) $(CPPC_FLAGS) $^ -o $@
|
$(CPPC) $(CPPC_FLAGS) $^ -o $@
|
||||||
|
|
||||||
|
|
||||||
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
|
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp $(SRC_PATH)/%.h
|
||||||
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
|
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#ifndef VERSION
|
#ifndef VERSION
|
||||||
|
|
||||||
#define VERSION "0.7.2"
|
#define VERSION "0.8"
|
||||||
#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) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include <algorithm>
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
@@ -12,6 +13,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <panel.h>
|
#include <panel.h>
|
||||||
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -242,3 +244,29 @@ void move_panel_relative(PANEL *panel, int dy, int dx) {
|
|||||||
|
|
||||||
move_panel(panel, new_y, new_x);
|
move_panel(panel, new_y, new_x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string html_to_string(std::string html) {
|
||||||
|
{ // fix new lines
|
||||||
|
const std::string search = "<br />";
|
||||||
|
const std::string replace = "\n";
|
||||||
|
|
||||||
|
size_t pos = 0;
|
||||||
|
while ((pos = html.find(search, pos)) != std::string::npos) {
|
||||||
|
html.replace(pos, search.length(), replace);
|
||||||
|
pos += replace.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::regex linkPattern("<a\\s+href=[\"'](.*?)[\"'](.*?)>(.*?)</a>");
|
||||||
|
html = std::regex_replace(html, linkPattern,
|
||||||
|
"\033]8;;$1\033\\$3\033]8;;\033\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::regex tag("<[^>]*>");
|
||||||
|
html = std::regex_replace(html, tag, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return html;
|
||||||
|
}
|
||||||
@@ -33,4 +33,6 @@ wchar_t *char_to_wchar(const char *src);
|
|||||||
std::wstring string_to_wstring(const std::string &str);
|
std::wstring string_to_wstring(const std::string &str);
|
||||||
std::string wstring_to_string(const std::wstring &wstr);
|
std::string wstring_to_string(const std::wstring &wstr);
|
||||||
|
|
||||||
|
std::string html_to_string(std::string html);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+88
-33
@@ -1,29 +1,39 @@
|
|||||||
#include "komens.h"
|
#include "komens.h"
|
||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
#include "net.h"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <menu.h>
|
#include <menu.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#define WIN_HIGHT 40
|
||||||
|
#define DEFAULT_OFSET 4
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
std::vector<allocation> komens_allocated;
|
std::vector<allocation> komens_allocated;
|
||||||
|
|
||||||
void komens_page() {
|
void insert_content(WINDOW *content_window, size_t i, json &resp_from_api);
|
||||||
|
|
||||||
|
void komens_page(koment_type type) {
|
||||||
current_allocated = &komens_allocated;
|
current_allocated = &komens_allocated;
|
||||||
json resp_from_api;
|
json resp_from_api;
|
||||||
{
|
{
|
||||||
std::ifstream f("test-data/komens.json");
|
/*std::ifstream f("test-data/komens.json");
|
||||||
resp_from_api = json::parse(f);
|
resp_from_api = json::parse(f);
|
||||||
f.close();
|
f.close();*/
|
||||||
|
const char *types[] = {"/api/3/komens/messages/received",
|
||||||
|
"/api/3/komens/messages/sent",
|
||||||
|
"/api/3/komens/messages/noticeboard"};
|
||||||
|
|
||||||
|
const std::string endpoint = types[type];
|
||||||
|
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, POST);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize curses */
|
/* Initialize curses */
|
||||||
@@ -42,53 +52,89 @@ void komens_page() {
|
|||||||
ITEM **komens_items = new ITEM *[num_of_komens + 1];
|
ITEM **komens_items = new ITEM *[num_of_komens + 1];
|
||||||
komens_allocated.push_back({ITEM_ARRAY, komens_items, num_of_komens});
|
komens_allocated.push_back({ITEM_ARRAY, komens_items, num_of_komens});
|
||||||
|
|
||||||
char **komens_bufs = new char *[num_of_komens];
|
char **title_bufs = new char *[num_of_komens];
|
||||||
char title_buf[1500];
|
char **name_bufs = new char *[num_of_komens];
|
||||||
char name_buf[1500];
|
size_t max_item_lenght;
|
||||||
for (size_t i = 0; i < num_of_komens; i++) {
|
{
|
||||||
wcstombs(title_buf,
|
size_t max_title_lenght = 0;
|
||||||
string_to_wstring(
|
size_t max_name_lenght = 0;
|
||||||
resp_from_api["Messages"][i]["Title"].get<std::string>())
|
size_t tmp_lenght;
|
||||||
.c_str(),
|
char tmp_buf[1500];
|
||||||
sizeof(title_buf));
|
for (size_t i = 0; i < num_of_komens; i++) {
|
||||||
wcstombs(
|
wcstombs(tmp_buf,
|
||||||
name_buf,
|
string_to_wstring(
|
||||||
string_to_wstring(
|
resp_from_api["Messages"][i]["Title"].get<std::string>())
|
||||||
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>())
|
.c_str(),
|
||||||
.c_str(),
|
sizeof(tmp_buf));
|
||||||
sizeof(name_buf));
|
|
||||||
|
|
||||||
komens_bufs[i] = new char[strlen(title_buf) + strlen(name_buf) + 2];
|
tmp_lenght =
|
||||||
snprintf(komens_bufs[i], strlen(title_buf) + strlen(name_buf) + 1, "%s\n%s",
|
resp_from_api["Messages"][i]["Title"].get<std::string>().length();
|
||||||
title_buf, name_buf);
|
|
||||||
|
|
||||||
komens_items[i] = new_item(komens_bufs[i], "");
|
if (tmp_lenght > max_title_lenght) {
|
||||||
|
max_title_lenght = tmp_lenght;
|
||||||
|
}
|
||||||
|
|
||||||
|
title_bufs[i] = new char[strlen(tmp_buf) + 1];
|
||||||
|
strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
|
||||||
|
wcstombs(
|
||||||
|
tmp_buf,
|
||||||
|
string_to_wstring(
|
||||||
|
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>())
|
||||||
|
.c_str(),
|
||||||
|
sizeof(tmp_buf));
|
||||||
|
|
||||||
|
tmp_lenght = resp_from_api["Messages"][i]["Sender"]["Name"]
|
||||||
|
.get<std::string>()
|
||||||
|
.length();
|
||||||
|
|
||||||
|
if (tmp_lenght > max_name_lenght) {
|
||||||
|
max_name_lenght = tmp_lenght;
|
||||||
|
}
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
max_item_lenght = 3 + max_title_lenght + 1 + max_name_lenght;
|
||||||
}
|
}
|
||||||
komens_items[num_of_komens] = nullptr;
|
komens_items[num_of_komens] = nullptr;
|
||||||
|
|
||||||
MENU *komens_choise_menu = new_menu(komens_items);
|
MENU *komens_choise_menu = new_menu(komens_items);
|
||||||
komens_allocated.push_back({MENU_TYPE, komens_choise_menu, 0});
|
komens_allocated.push_back({MENU_TYPE, komens_choise_menu, 1});
|
||||||
|
|
||||||
WINDOW *komens_choise_menu_win = newwin(40, 80, 4, 4);
|
WINDOW *komens_choise_menu_win =
|
||||||
komens_allocated.push_back({WINDOW_TYPE, komens_choise_menu_win, 0});
|
newwin(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_win(komens_choise_menu, komens_choise_menu_win);
|
||||||
set_menu_sub(komens_choise_menu,
|
set_menu_sub(komens_choise_menu,
|
||||||
derwin(komens_choise_menu_win, 30, 78, 3, 1));
|
derwin(komens_choise_menu_win, WIN_HIGHT - 10, max_item_lenght,
|
||||||
set_menu_format(komens_choise_menu, 29, 1);
|
DEFAULT_OFSET - 1, DEFAULT_OFSET - 3));
|
||||||
|
set_menu_format(komens_choise_menu, WIN_HIGHT - 5, 1);
|
||||||
|
|
||||||
set_menu_mark(komens_choise_menu, " * ");
|
set_menu_mark(komens_choise_menu, " * ");
|
||||||
|
|
||||||
box(komens_choise_menu_win, 0, 0);
|
box(komens_choise_menu_win, 0, 0);
|
||||||
|
|
||||||
wprint_in_middle(komens_choise_menu_win, 1, 0, 40, L"Komens", COLOR_PAIR(1));
|
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);
|
mvwaddch(komens_choise_menu_win, 2, 0, ACS_LTEE);
|
||||||
mvwhline(komens_choise_menu_win, 2, 1, ACS_HLINE, 38);
|
mvwhline(komens_choise_menu_win, 2, 1, ACS_HLINE, max_item_lenght - 1);
|
||||||
mvwaddch(komens_choise_menu_win, 2, 39, ACS_RTEE);
|
mvwaddch(komens_choise_menu_win, 2, max_item_lenght, ACS_RTEE);
|
||||||
|
|
||||||
post_menu(komens_choise_menu);
|
post_menu(komens_choise_menu);
|
||||||
wrefresh(komens_choise_menu_win);
|
wrefresh(komens_choise_menu_win);
|
||||||
|
|
||||||
|
WINDOW *content_window =
|
||||||
|
newwin(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)),
|
||||||
|
resp_from_api);
|
||||||
|
wrefresh(content_window);
|
||||||
|
|
||||||
attron(COLOR_PAIR(COLOR_BLUE));
|
attron(COLOR_PAIR(COLOR_BLUE));
|
||||||
mvprintw(LINES - 2, 0,
|
mvprintw(LINES - 2, 0,
|
||||||
"Use PageUp and PageDown to scoll down or up a page of items");
|
"Use PageUp and PageDown to scoll down or up a page of items");
|
||||||
@@ -111,7 +157,16 @@ void komens_page() {
|
|||||||
menu_driver(komens_choise_menu, REQ_UP_ITEM);
|
menu_driver(komens_choise_menu, REQ_UP_ITEM);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
insert_content(content_window, item_index(current_item(komens_choise_menu)),
|
||||||
|
resp_from_api);
|
||||||
|
wrefresh(content_window);
|
||||||
wrefresh(komens_choise_menu_win);
|
wrefresh(komens_choise_menu_win);
|
||||||
}
|
}
|
||||||
delete_all(&komens_allocated);
|
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",
|
||||||
|
html_to_string(resp_from_api.at("Messages")[i]["Text"]).c_str());
|
||||||
}
|
}
|
||||||
+6
-1
@@ -1,4 +1,9 @@
|
|||||||
#ifndef _ba_ko_hg_
|
#ifndef _ba_ko_hg_
|
||||||
#define _ba_ko_hg_
|
#define _ba_ko_hg_
|
||||||
void komens_page();
|
enum koment_type {
|
||||||
|
RECEIVED,
|
||||||
|
SEND,
|
||||||
|
NOTICEBOARD,
|
||||||
|
};
|
||||||
|
void komens_page(koment_type type);
|
||||||
#endif
|
#endif
|
||||||
+2
-3
@@ -9,9 +9,8 @@
|
|||||||
|
|
||||||
void komens_menu() {
|
void komens_menu() {
|
||||||
wchar_t *choices[] = {
|
wchar_t *choices[] = {
|
||||||
L"received", L"send", L"noticeboard", L"Exit", nullptr,
|
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
|
||||||
};
|
};
|
||||||
void (*choicesFuncs[])() = {komens_page, nullptr, nullptr, nullptr, nullptr};
|
|
||||||
|
|
||||||
ITEM **my_items;
|
ITEM **my_items;
|
||||||
int c;
|
int c;
|
||||||
@@ -88,7 +87,7 @@ void komens_menu() {
|
|||||||
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
if (item_index(current_item(my_menu)) == n_choices - 1) {
|
||||||
goto close_menu;
|
goto close_menu;
|
||||||
}
|
}
|
||||||
choicesFuncs[item_index(current_item(my_menu))]();
|
komens_page(static_cast<koment_type>(item_index(current_item(my_menu))));
|
||||||
pos_menu_cursor(my_menu);
|
pos_menu_cursor(my_menu);
|
||||||
refresh();
|
refresh();
|
||||||
wrefresh(my_menu_win);
|
wrefresh(my_menu_win);
|
||||||
|
|||||||
+2
-2
@@ -153,7 +153,7 @@ void is_access_token_empty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// supports all endpoints that only require access_token
|
// supports all endpoints that only require access_token
|
||||||
json get_data_from_endpoint(std::string &endpoint, metod metod,
|
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
||||||
std::string additional_data) {
|
std::string additional_data) {
|
||||||
is_access_token_empty();
|
is_access_token_empty();
|
||||||
access_token_refreshed:
|
access_token_refreshed:
|
||||||
@@ -163,7 +163,7 @@ access_token_refreshed:
|
|||||||
req_data.append(std::format("&{}", additional_data));
|
req_data.append(std::format("&{}", additional_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto [response, http_code] = send_curl_request(endpoint, GET, req_data);
|
auto [response, http_code] = send_curl_request(endpoint, metod, req_data);
|
||||||
|
|
||||||
if (http_code != 200) {
|
if (http_code != 200) {
|
||||||
refresh_access_token();
|
refresh_access_token();
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ extern CURL *curl;
|
|||||||
namespace bakaapi {
|
namespace bakaapi {
|
||||||
void login(std::string username, std::string password);
|
void login(std::string username, std::string password);
|
||||||
void refresh_access_token();
|
void refresh_access_token();
|
||||||
json get_data_from_endpoint(std::string &endpoint, metod metod,
|
json get_data_from_endpoint(const std::string &endpoint, metod metod,
|
||||||
std::string additional_data = "");
|
std::string additional_data = "");
|
||||||
} // namespace bakaapi
|
} // namespace bakaapi
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user