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