-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
174 lines (149 loc) · 5.33 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cstdio>
#include <curses.h>
#include <ncurses.h>
#include <chrono>
#include <thread>
#include "./src/Request.hpp"
#include "./src/StatusBar.hpp"
#include "./src/DisplayNews.hpp"
#include "src/Link.cpp"
#include "src/Modal.cpp"
#include "./src/json.hpp"
using json = nlohmann::json;
int main() {
initscr();
noecho();
cbreak();
use_default_colors();
curs_set(0);
/*Status Bar */
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_BLACK, COLOR_BLACK);
init_pair(3, COLOR_BLACK, COLOR_RED);
init_pair(4, COLOR_WHITE, COLOR_BLACK);
/*current selected element for the list*/
int current_element = -1;
/*current top/bot elements for scrolling behaviour*/
int current_element_top_visible = 0;
int current_element_bottom_visible = 0;
int x_max = getmaxx(stdscr);
int y_max = getmaxy(stdscr);
Request request;
request.getRequests();
std::ifstream jsonFile("./data/data.json");
json myJson;
jsonFile >> myJson;
jsonFile.close();
/*Create thread t1 to not interfere with main getch*/
/*runs every x time to get new data from the API*/
std::string last_update_time;
bool refreshed = true;
std::thread t1([&myJson,&last_update_time,¤t_element,&refreshed]() {
Request request;
DisplayNews displaynews;
StatusBar statusbar;
while (true) {
clear();
/*update json with new data from API*/
request.getRequests();
std::ifstream jsonFile("./data/data.json");
jsonFile >> myJson;
jsonFile.close();
/*remove the highlighted selection after refresh*/
refreshed = true;
displaynews.draw(myJson, current_element, refreshed, 0, 0, 0);
/*StatusBar*/
statusbar.setAttributes(COLOR_PAIR(2));
statusbar.drawBg();
statusbar.draw();
statusbar.setAttributes(COLOR_PAIR(4));
/*Save time each update is done*/
last_update_time = CurrentDate();
statusbar.setLastUpdate(last_update_time);
statusbar.drawLastUpdate();
/*quit*/
statusbar.setAttributes(COLOR_PAIR(3));
statusbar.drawKey(" q ", 1);
statusbar.setAttributes(COLOR_PAIR(4));
statusbar.drawKey(" quit ", 4);
/*open in browser*/
statusbar.setAttributes(COLOR_PAIR(3));
statusbar.drawKey(" o ", 11);
statusbar.setAttributes(COLOR_PAIR(4));
statusbar.drawKey(" open in browser ", 14);
refresh();
/*Sleep before next update*/
std::this_thread::sleep_for(std::chrono::minutes(15));
}
});
t1.detach();
// AFTER getch
DisplayNews displaynews;
int key;
while ((key = getch()) != 'q') {
switch (key) {
case 'j':
if(current_element <= myJson["articles"].size() - 2 || current_element== - 1){
current_element++;
if (current_element >= 0 && current_element < myJson["articles"].size() - 1) {
if (current_element >= y_max - 3) {
if (current_element_bottom_visible < myJson["articles"].size() - 1) {
current_element_bottom_visible++;
}
if (current_element_top_visible < myJson["articles"].size() - 1) {
current_element_top_visible++;
}
}
} }
break;
case 'k':
if (current_element >= 1) {
current_element--;
if (current_element < y_max - 3) {
if (current_element_bottom_visible > 0) {
current_element_bottom_visible--;
}
if (current_element_top_visible > 0) {
current_element_top_visible--;
}
}
} break;
case '\n':
display_modal(current_element, myJson);
break;
case 'o':
open_link(current_element, myJson);
}
clear();
refreshed = false;
// read json file
std::ifstream jsonFile("./data/data.json");
jsonFile >> myJson;
jsonFile.close();
// display news
displaynews.draw(myJson, current_element, refreshed, y_max,
current_element_bottom_visible,
current_element_top_visible);
StatusBar statusbar;
statusbar.setAttributes(COLOR_PAIR(2));
statusbar.drawBg();
statusbar.draw();
statusbar.setAttributes(COLOR_PAIR(4));
statusbar.setLastUpdate(last_update_time);
statusbar.drawLastUpdate();
/*quit*/
statusbar.setAttributes(COLOR_PAIR(3));
statusbar.drawKey(" q ", 1);
statusbar.setAttributes(COLOR_PAIR(4));
statusbar.drawKey(" quit ", 4);
/*open in browser*/
statusbar.setAttributes(COLOR_PAIR(3));
statusbar.drawKey(" o ", 11);
statusbar.setAttributes(COLOR_PAIR(4));
statusbar.drawKey(" open in browser ", 14);
refresh();
}
endwin();
return 0;
}