| 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 | |
| Christian Göttsche | 7e7a53c | 2020-12-20 16:58:37 +0100 | [diff] [blame] | 22 | static const int InfoScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27}; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 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 | } |
| Christian Göttsche | ce9e7fd | 2021-01-02 23:51:53 +0100 | [diff] [blame] | 29 | this->display = Panel_new(0, 1, COLS, height, Class(ListItem), false, bar); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 30 | this->inc = IncSet_new(bar); |
| Christian Göttsche | d37d66b | 2021-03-12 16:46:55 +0100 | [diff] [blame^] | 31 | this->lines = Vector_new(Vector_type(this->display->items), true, DEFAULT_SIZE); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 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 | |
| Christian Göttsche | e3862aa | 2020-12-16 21:46:11 +0100 | [diff] [blame] | 47 | char title[COLS + 1]; |
| 48 | int len = vsnprintf(title, sizeof(title), fmt, ap); |
| 49 | va_end(ap); |
| 50 | |
| ryenus | ff455b0 | 2020-10-03 00:12:31 +0800 | [diff] [blame] | 51 | if (len > COLS) { |
| 52 | memset(&title[COLS - 3], '.', 3); |
| 53 | } |
| 54 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 55 | attrset(CRT_colors[METER_TEXT]); |
| 56 | mvhline(0, 0, ' ', COLS); |
| V | bfcb8ca | 2021-01-08 05:11:45 +0100 | [diff] [blame] | 57 | mvaddstr(0, 0, title); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 58 | attrset(CRT_colors[DEFAULT_COLOR]); |
| Christian Göttsche | a3cced9 | 2020-12-28 23:26:14 +0100 | [diff] [blame] | 59 | Panel_draw(this->display, true, true, true, false); |
| Christian Göttsche | e3862aa | 2020-12-16 21:46:11 +0100 | [diff] [blame] | 60 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 61 | IncSet_drawBar(this->inc); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 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 | a3cced9 | 2020-12-28 23:26:14 +0100 | [diff] [blame] | 92 | Panel_draw(panel, false, true, true, false); |
| Christian Göttsche | e3862aa | 2020-12-16 21:46:11 +0100 | [diff] [blame] | 93 | IncSet_drawBar(this->inc); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 94 | |
| Hisham Muhammad | 8c65321 | 2018-02-18 10:38:49 -0300 | [diff] [blame] | 95 | if (this->inc->active) { |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 96 | (void) move(LINES - 1, CRT_cursorX); |
| Hisham Muhammad | 8c65321 | 2018-02-18 10:38:49 -0300 | [diff] [blame] | 97 | } |
| Hisham | 645057d | 2016-05-19 16:09:47 -0300 | [diff] [blame] | 98 | set_escdelay(25); |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 99 | int ch = getch(); |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 100 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 101 | if (ch == ERR) { |
| 102 | if (As_InfoScreen(this)->onErr) { |
| 103 | InfoScreen_onErr(this); |
| 104 | continue; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (ch == KEY_MOUSE) { |
| 109 | MEVENT mevent; |
| 110 | int ok = getmouse(&mevent); |
| Nathan Scott | 36ef4d4 | 2020-08-19 18:10:16 +1000 | [diff] [blame] | 111 | if (ok == OK) { |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 112 | if (mevent.bstate & BUTTON1_RELEASED) { |
| 113 | if (mevent.y >= panel->y && mevent.y < LINES - 1) { |
| 114 | Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV - 1); |
| 115 | ch = 0; |
| 116 | } else if (mevent.y == LINES - 1) { |
| 117 | ch = IncSet_synthesizeEvent(this->inc, mevent.x); |
| 118 | } |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 119 | } |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 120 | #if NCURSES_MOUSE_VERSION > 1 |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 121 | else if (mevent.bstate & BUTTON4_PRESSED) { |
| Youngjae Lee | 3c61813 | 2021-01-14 13:37:46 +0900 | [diff] [blame] | 122 | ch = KEY_WHEELUP; |
| 123 | } else if (mevent.bstate & BUTTON5_PRESSED) { |
| 124 | ch = KEY_WHEELDOWN; |
| Jorge Pereira | 8de0498 | 2020-06-11 13:41:13 -0300 | [diff] [blame] | 125 | } |
| YJ Lee | 34da6fd | 2021-01-14 16:25:23 +0900 | [diff] [blame] | 126 | #endif |
| Christian Göttsche | f8208f2 | 2020-10-20 13:14:32 +0200 | [diff] [blame] | 127 | } |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (this->inc->active) { |
| 131 | IncSet_handleKey(this->inc, ch, panel, IncSet_getListItemValue, this->lines); |
| 132 | continue; |
| 133 | } |
| Daniel Flanagan | dd33444 | 2019-10-31 11:39:12 -0500 | [diff] [blame] | 134 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 135 | switch(ch) { |
| 136 | case ERR: |
| 137 | continue; |
| 138 | case KEY_F(3): |
| 139 | case '/': |
| 140 | IncSet_activate(this->inc, INC_SEARCH, panel); |
| 141 | break; |
| 142 | case KEY_F(4): |
| 143 | case '\\': |
| 144 | IncSet_activate(this->inc, INC_FILTER, panel); |
| 145 | break; |
| 146 | case KEY_F(5): |
| 147 | clear(); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 148 | if (As_InfoScreen(this)->scan) { |
| 149 | Vector_prune(this->lines); |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 150 | InfoScreen_scan(this); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 151 | } |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 152 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 153 | InfoScreen_draw(this); |
| 154 | break; |
| 155 | case '\014': // Ctrl+L |
| 156 | clear(); |
| 157 | InfoScreen_draw(this); |
| 158 | break; |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 159 | case 27: |
| Benny Baumann | c49ca61 | 2020-11-21 16:59:38 +0100 | [diff] [blame] | 160 | case 'q': |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 161 | case KEY_F(10): |
| 162 | looping = false; |
| 163 | break; |
| 164 | case KEY_RESIZE: |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 165 | Panel_resize(panel, COLS, LINES - 2); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 166 | if (As_InfoScreen(this)->scan) { |
| 167 | Vector_prune(this->lines); |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 168 | InfoScreen_scan(this); |
| Øystein Hiåsen | d8d8303 | 2021-02-15 20:32:01 +0100 | [diff] [blame] | 169 | } |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 170 | |
| Hisham Muhammad | 466d4da | 2016-01-12 06:00:58 -0200 | [diff] [blame] | 171 | InfoScreen_draw(this); |
| 172 | break; |
| 173 | default: |
| 174 | if (As_InfoScreen(this)->onKey && InfoScreen_onKey(this, ch)) { |
| 175 | continue; |
| 176 | } |
| 177 | Panel_onKey(panel, ch); |
| 178 | } |
| 179 | } |
| 180 | } |