| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 1 | #include "InfoScreen.h" |
| 2 | |
| 3 | #include "config.h" |
| 4 | #include "Object.h" |
| 5 | #include "CRT.h" |
| 6 | #include "IncSet.h" |
| 7 | #include "ListItem.h" |
| 8 | #include "Platform.h" |
| 9 | #include "StringUtils.h" |
| 10 | |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdarg.h> |
| 15 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 16 | |
| Richard | d5faf64 | 2017-07-22 21:41:19 -0500 | [diff] [blame] | 17 | static const char* const InfoScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 18 | |
| Richard | d5faf64 | 2017-07-22 21:41:19 -0500 | [diff] [blame] | 19 | static const char* const InfoScreenKeys[] = {"F3", "F4", "F5", "Esc"}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 20 | |
| 21 | static int InfoScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27}; |
| 22 | |
| Christian Göttsche | 11f558f | 2020-08-20 21:58:14 +0200 | [diff] [blame] | 23 | InfoScreen* InfoScreen_init(InfoScreen* this, Process* process, FunctionBar* bar, int height, const char* panelHeader) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 24 | this->process = process; |
| 25 | if (!bar) { |
| 26 | bar = FunctionBar_new(InfoScreenFunctions, InfoScreenKeys, InfoScreenEvents); |
| 27 | } |
| 28 | this->display = Panel_new(0, 1, COLS, height, false, Class(ListItem), bar); |
| 29 | this->inc = IncSet_new(bar); |
| 30 | this->lines = Vector_new(this->display->items->type, true, DEFAULT_SIZE); |
| 31 | Panel_setHeader(this->display, panelHeader); |
| 32 | return this; |
| 33 | } |
| 34 | |
| 35 | InfoScreen* InfoScreen_done(InfoScreen* this) { |
| 36 | Panel_delete((Object*)this->display); |
| 37 | IncSet_delete(this->inc); |
| 38 | Vector_delete(this->lines); |
| 39 | return this; |
| 40 | } |
| 41 | |
| Christian Göttsche | 11f558f | 2020-08-20 21:58:14 +0200 | [diff] [blame] | 42 | void InfoScreen_drawTitled(InfoScreen* this, const char* fmt, ...) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 43 | va_list ap; |
| 44 | va_start(ap, fmt); |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame^] | 45 | |
| 46 | char* title = xMalloc(COLS + 1); |
| 47 | int len = vsnprintf(title, COLS + 1, fmt, ap); |
| 48 | if (len > COLS) { |
| 49 | memset(&title[COLS - 3], '.', 3); |
| 50 | } |
| 51 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 52 | attrset(CRT_colors[METER_TEXT]); |
| 53 | mvhline(0, 0, ' ', COLS); |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame^] | 54 | mvwprintw(stdscr, 0, 0, title); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 55 | attrset(CRT_colors[DEFAULT_COLOR]); |
| Hisham | 7f9c82f | 2016-06-23 13:25:58 -0300 | [diff] [blame] | 56 | this->display->needsRedraw = true; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 57 | Panel_draw(this->display, true); |
| 58 | IncSet_drawBar(this->inc); |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame^] | 59 | free(title); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 60 | va_end(ap); |
| 61 | } |
| 62 | |
| 63 | void InfoScreen_addLine(InfoScreen* this, const char* line) { |
| 64 | Vector_add(this->lines, (Object*) ListItem_new(line, 0)); |
| 65 | const char* incFilter = IncSet_filter(this->inc); |
| 66 | if (!incFilter || String_contains_i(line, incFilter)) |
| Christian Göttsche | 8439491 | 2020-09-28 12:17:52 +0200 | [diff] [blame] | 67 | Panel_add(this->display, Vector_get(this->lines, Vector_size(this->lines)-1)); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void InfoScreen_appendLine(InfoScreen* this, const char* line) { |
| 71 | ListItem* last = (ListItem*)Vector_get(this->lines, Vector_size(this->lines)-1); |
| 72 | ListItem_append(last, line); |
| 73 | const char* incFilter = IncSet_filter(this->inc); |
| 74 | if (incFilter && Panel_get(this->display, Panel_size(this->display)-1) != (Object*)last && String_contains_i(line, incFilter)) |
| 75 | Panel_add(this->display, (Object*)last); |
| 76 | } |
| 77 | |
| 78 | void InfoScreen_run(InfoScreen* this) { |
| 79 | Panel* panel = this->display; |
| 80 | |
| 81 | if (As_InfoScreen(this)->scan) InfoScreen_scan(this); |
| 82 | InfoScreen_draw(this); |
| 83 | |
| 84 | bool looping = true; |
| 85 | while (looping) { |
| 86 | |
| 87 | Panel_draw(panel, true); |
| 88 | |
| Hisham Muhammad | 8c65321 | 2018-02-18 10:38:49 -0300 | [diff] [blame] | 89 | if (this->inc->active) { |
| 90 | (void) move(LINES-1, CRT_cursorX); |
| 91 | } |
| Hisham | 645057d | 2016-05-19 16:09:47 -0300 | [diff] [blame] | 92 | set_escdelay(25); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 93 | int ch = getch(); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 94 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 95 | if (ch == ERR) { |
| 96 | if (As_InfoScreen(this)->onErr) { |
| 97 | InfoScreen_onErr(this); |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (ch == KEY_MOUSE) { |
| 103 | MEVENT mevent; |
| 104 | int ok = getmouse(&mevent); |
| Nathan Scott | 36ef4d4 | 2020-08-19 18:10:16 +1000 | [diff] [blame] | 105 | if (ok == OK) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 106 | if (mevent.y >= panel->y && mevent.y < LINES - 1) { |
| 107 | Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV); |
| 108 | ch = 0; |
| Nathan Scott | 9aa8168 | 2020-08-20 15:16:47 +1000 | [diff] [blame] | 109 | } else if (mevent.y == LINES - 1) { |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 110 | ch = IncSet_synthesizeEvent(this->inc, mevent.x); |
| Jorge Pereira | 8de0498 | 2020-06-11 13:41:13 -0300 | [diff] [blame] | 111 | } |
| Nathan Scott | 36ef4d4 | 2020-08-19 18:10:16 +1000 | [diff] [blame] | 112 | } |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (this->inc->active) { |
| 116 | IncSet_handleKey(this->inc, ch, panel, IncSet_getListItemValue, this->lines); |
| 117 | continue; |
| 118 | } |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 119 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 120 | switch(ch) { |
| 121 | case ERR: |
| 122 | continue; |
| 123 | case KEY_F(3): |
| 124 | case '/': |
| 125 | IncSet_activate(this->inc, INC_SEARCH, panel); |
| 126 | break; |
| 127 | case KEY_F(4): |
| 128 | case '\\': |
| 129 | IncSet_activate(this->inc, INC_FILTER, panel); |
| 130 | break; |
| 131 | case KEY_F(5): |
| 132 | clear(); |
| 133 | if (As_InfoScreen(this)->scan) InfoScreen_scan(this); |
| 134 | InfoScreen_draw(this); |
| 135 | break; |
| 136 | case '\014': // Ctrl+L |
| 137 | clear(); |
| 138 | InfoScreen_draw(this); |
| 139 | break; |
| 140 | case 'q': |
| 141 | case 27: |
| 142 | case KEY_F(10): |
| 143 | looping = false; |
| 144 | break; |
| 145 | case KEY_RESIZE: |
| 146 | Panel_resize(panel, COLS, LINES-2); |
| Christian Göttsche | 8efc885 | 2020-09-29 10:25:20 +0200 | [diff] [blame] | 147 | if (As_InfoScreen(this)->scan) InfoScreen_scan(this); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 148 | InfoScreen_draw(this); |
| 149 | break; |
| 150 | default: |
| 151 | if (As_InfoScreen(this)->onKey && InfoScreen_onKey(this, ch)) { |
| 152 | continue; |
| 153 | } |
| 154 | Panel_onKey(panel, ch); |
| 155 | } |
| 156 | } |
| 157 | } |