2020-09-19 11:55:23 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
#include "InfoScreen.h"
|
|
|
|
|
2020-09-19 11:55:23 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
#include "CRT.h"
|
|
|
|
#include "IncSet.h"
|
|
|
|
#include "ListItem.h"
|
2020-09-19 11:55:23 +00:00
|
|
|
#include "Object.h"
|
|
|
|
#include "ProvideCurses.h"
|
2020-10-14 18:21:09 +00:00
|
|
|
#include "XUtils.h"
|
2016-01-12 08:00:58 +00:00
|
|
|
|
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const InfoScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL};
|
2016-01-12 08:00:58 +00:00
|
|
|
|
2017-07-23 02:41:19 +00:00
|
|
|
static const char* const InfoScreenKeys[] = {"F3", "F4", "F5", "Esc"};
|
2016-01-12 08:00:58 +00:00
|
|
|
|
2020-12-20 15:58:37 +00:00
|
|
|
static const int InfoScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
|
2016-01-12 08:00:58 +00:00
|
|
|
|
2020-10-07 17:02:15 +00:00
|
|
|
InfoScreen* InfoScreen_init(InfoScreen* this, const Process* process, FunctionBar* bar, int height, const char* panelHeader) {
|
2016-01-12 08:00:58 +00:00
|
|
|
this->process = process;
|
|
|
|
if (!bar) {
|
|
|
|
bar = FunctionBar_new(InfoScreenFunctions, InfoScreenKeys, InfoScreenEvents);
|
|
|
|
}
|
2021-01-02 22:51:53 +00:00
|
|
|
this->display = Panel_new(0, 1, COLS, height, Class(ListItem), false, bar);
|
2016-01-12 08:00:58 +00:00
|
|
|
this->inc = IncSet_new(bar);
|
2021-03-12 15:46:55 +00:00
|
|
|
this->lines = Vector_new(Vector_type(this->display->items), true, DEFAULT_SIZE);
|
2016-01-12 08:00:58 +00:00
|
|
|
Panel_setHeader(this->display, panelHeader);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoScreen* InfoScreen_done(InfoScreen* this) {
|
|
|
|
Panel_delete((Object*)this->display);
|
|
|
|
IncSet_delete(this->inc);
|
|
|
|
Vector_delete(this->lines);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:58:14 +00:00
|
|
|
void InfoScreen_drawTitled(InfoScreen* this, const char* fmt, ...) {
|
2016-01-12 08:00:58 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2020-10-02 16:12:31 +00:00
|
|
|
|
2020-12-16 20:46:11 +00:00
|
|
|
char title[COLS + 1];
|
|
|
|
int len = vsnprintf(title, sizeof(title), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2020-10-02 16:12:31 +00:00
|
|
|
if (len > COLS) {
|
|
|
|
memset(&title[COLS - 3], '.', 3);
|
|
|
|
}
|
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
attrset(CRT_colors[METER_TEXT]);
|
|
|
|
mvhline(0, 0, ' ', COLS);
|
2021-01-08 04:11:45 +00:00
|
|
|
mvaddstr(0, 0, title);
|
2016-01-12 08:00:58 +00:00
|
|
|
attrset(CRT_colors[DEFAULT_COLOR]);
|
2020-12-28 22:26:14 +00:00
|
|
|
Panel_draw(this->display, true, true, true, false);
|
2020-12-16 20:46:11 +00:00
|
|
|
|
2021-08-31 05:38:52 +00:00
|
|
|
IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
|
2016-01-12 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoScreen_addLine(InfoScreen* this, const char* line) {
|
|
|
|
Vector_add(this->lines, (Object*) ListItem_new(line, 0));
|
|
|
|
const char* incFilter = IncSet_filter(this->inc);
|
2022-03-25 15:24:24 +00:00
|
|
|
if (!incFilter || String_contains_i(line, incFilter, true)) {
|
2020-10-31 22:28:02 +00:00
|
|
|
Panel_add(this->display, Vector_get(this->lines, Vector_size(this->lines) - 1));
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2016-01-12 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoScreen_appendLine(InfoScreen* this, const char* line) {
|
2020-10-31 22:28:02 +00:00
|
|
|
ListItem* last = (ListItem*)Vector_get(this->lines, Vector_size(this->lines) - 1);
|
2016-01-12 08:00:58 +00:00
|
|
|
ListItem_append(last, line);
|
|
|
|
const char* incFilter = IncSet_filter(this->inc);
|
2022-03-25 15:24:24 +00:00
|
|
|
if (incFilter && Panel_get(this->display, Panel_size(this->display) - 1) != (Object*)last && String_contains_i(line, incFilter, true)) {
|
2016-01-12 08:00:58 +00:00
|
|
|
Panel_add(this->display, (Object*)last);
|
2020-11-01 00:09:51 +00:00
|
|
|
}
|
2016-01-12 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InfoScreen_run(InfoScreen* this) {
|
|
|
|
Panel* panel = this->display;
|
|
|
|
|
2020-10-31 22:28:02 +00:00
|
|
|
if (As_InfoScreen(this)->scan)
|
|
|
|
InfoScreen_scan(this);
|
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_draw(this);
|
|
|
|
|
|
|
|
bool looping = true;
|
|
|
|
while (looping) {
|
|
|
|
|
2020-12-28 22:26:14 +00:00
|
|
|
Panel_draw(panel, false, true, true, false);
|
2021-08-31 05:38:52 +00:00
|
|
|
IncSet_drawBar(this->inc, CRT_colors[FUNCTION_BAR]);
|
2016-01-12 08:00:58 +00:00
|
|
|
|
2021-08-31 05:38:52 +00:00
|
|
|
int ch = Panel_getCh(panel);
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
if (ch == ERR) {
|
|
|
|
if (As_InfoScreen(this)->onErr) {
|
|
|
|
InfoScreen_onErr(this);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 18:17:13 +00:00
|
|
|
#ifdef HAVE_GETMOUSE
|
2016-01-12 08:00:58 +00:00
|
|
|
if (ch == KEY_MOUSE) {
|
|
|
|
MEVENT mevent;
|
|
|
|
int ok = getmouse(&mevent);
|
2020-08-19 08:10:16 +00:00
|
|
|
if (ok == OK) {
|
2021-01-14 04:37:46 +00:00
|
|
|
if (mevent.bstate & BUTTON1_RELEASED) {
|
|
|
|
if (mevent.y >= panel->y && mevent.y < LINES - 1) {
|
|
|
|
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1);
|
|
|
|
ch = 0;
|
|
|
|
} else if (mevent.y == LINES - 1) {
|
|
|
|
ch = IncSet_synthesizeEvent(this->inc, mevent.x);
|
|
|
|
}
|
2021-01-14 07:25:23 +00:00
|
|
|
}
|
2021-01-14 04:37:46 +00:00
|
|
|
#if NCURSES_MOUSE_VERSION > 1
|
2021-01-14 07:25:23 +00:00
|
|
|
else if (mevent.bstate & BUTTON4_PRESSED) {
|
2021-01-14 04:37:46 +00:00
|
|
|
ch = KEY_WHEELUP;
|
|
|
|
} else if (mevent.bstate & BUTTON5_PRESSED) {
|
|
|
|
ch = KEY_WHEELDOWN;
|
2020-06-11 16:41:13 +00:00
|
|
|
}
|
2021-01-14 07:25:23 +00:00
|
|
|
#endif
|
2020-10-20 11:14:32 +00:00
|
|
|
}
|
2016-01-12 08:00:58 +00:00
|
|
|
}
|
2021-07-14 18:17:13 +00:00
|
|
|
#endif
|
2016-01-12 08:00:58 +00:00
|
|
|
|
|
|
|
if (this->inc->active) {
|
|
|
|
IncSet_handleKey(this->inc, ch, panel, IncSet_getListItemValue, this->lines);
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-31 16:39:12 +00:00
|
|
|
|
2021-11-04 19:40:39 +00:00
|
|
|
switch (ch) {
|
2016-01-12 08:00:58 +00:00
|
|
|
case ERR:
|
|
|
|
continue;
|
|
|
|
case KEY_F(3):
|
|
|
|
case '/':
|
|
|
|
IncSet_activate(this->inc, INC_SEARCH, panel);
|
|
|
|
break;
|
|
|
|
case KEY_F(4):
|
|
|
|
case '\\':
|
|
|
|
IncSet_activate(this->inc, INC_FILTER, panel);
|
|
|
|
break;
|
|
|
|
case KEY_F(5):
|
|
|
|
clear();
|
2021-02-15 19:32:01 +00:00
|
|
|
if (As_InfoScreen(this)->scan) {
|
|
|
|
Vector_prune(this->lines);
|
2020-11-01 00:09:51 +00:00
|
|
|
InfoScreen_scan(this);
|
2021-02-15 19:32:01 +00:00
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_draw(this);
|
|
|
|
break;
|
|
|
|
case '\014': // Ctrl+L
|
|
|
|
clear();
|
|
|
|
InfoScreen_draw(this);
|
|
|
|
break;
|
|
|
|
case 27:
|
2020-11-21 15:59:38 +00:00
|
|
|
case 'q':
|
2016-01-12 08:00:58 +00:00
|
|
|
case KEY_F(10):
|
|
|
|
looping = false;
|
|
|
|
break;
|
|
|
|
case KEY_RESIZE:
|
2020-10-31 22:28:02 +00:00
|
|
|
Panel_resize(panel, COLS, LINES - 2);
|
2021-02-15 19:32:01 +00:00
|
|
|
if (As_InfoScreen(this)->scan) {
|
|
|
|
Vector_prune(this->lines);
|
2020-10-31 22:28:02 +00:00
|
|
|
InfoScreen_scan(this);
|
2021-02-15 19:32:01 +00:00
|
|
|
}
|
2020-11-01 00:09:51 +00:00
|
|
|
|
2016-01-12 08:00:58 +00:00
|
|
|
InfoScreen_draw(this);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (As_InfoScreen(this)->onKey && InfoScreen_onKey(this, ch)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Panel_onKey(panel, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|