| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 1 | #include "config.h" // IWYU pragma: keep |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 2 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 3 | #include "CommandScreen.h" |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 7 | |
| 8 | #include "Macros.h" |
| 9 | #include "Panel.h" |
| 10 | #include "ProvideCurses.h" |
| 11 | #include "XUtils.h" |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 12 | |
| 13 | |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 14 | static void CommandScreen_scan(InfoScreen* this) { |
| 15 | Panel* panel = this->display; |
| 16 | int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0); |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 17 | Panel_prune(panel); |
| 18 | |
| Narendran Gopalakrishnan | 09fe94d | 2020-10-17 16:24:45 +0530 | [diff] [blame] | 19 | const char* p = Process_getCommand(this->process); |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 20 | char* line = xMalloc(COLS + 1); |
| 21 | int line_offset = 0, last_spc = -1, len; |
| 22 | for (; *p != '\0'; p++, line_offset++) { |
| ryenus | 214c742 | 2020-09-26 05:22:24 +0800 | [diff] [blame] | 23 | line[line_offset] = *p; |
| Benny Baumann | 4586951 | 2020-11-01 01:09:51 +0100 | [diff] [blame] | 24 | if (*p == ' ') { |
| 25 | last_spc = line_offset; |
| 26 | } |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 27 | |
| 28 | if (line_offset == COLS) { |
| 29 | len = (last_spc == -1) ? line_offset : last_spc; |
| ryenus | 214c742 | 2020-09-26 05:22:24 +0800 | [diff] [blame] | 30 | line[len] = '\0'; |
| 31 | InfoScreen_addLine(this, line); |
| 32 | |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 33 | line_offset -= len; |
| 34 | last_spc = -1; |
| ryenus | 214c742 | 2020-09-26 05:22:24 +0800 | [diff] [blame] | 35 | memcpy(line, p - line_offset, line_offset + 1); |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| ryenus | 214c742 | 2020-09-26 05:22:24 +0800 | [diff] [blame] | 39 | if (line_offset > 0) { |
| 40 | line[line_offset] = '\0'; |
| 41 | InfoScreen_addLine(this, line); |
| 42 | } |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 43 | |
| 44 | free(line); |
| 45 | Panel_setSelected(panel, idx); |
| 46 | } |
| 47 | |
| 48 | static void CommandScreen_draw(InfoScreen* this) { |
| Narendran Gopalakrishnan | 09fe94d | 2020-10-17 16:24:45 +0530 | [diff] [blame] | 49 | InfoScreen_drawTitled(this, "Command of process %d - %s", this->process->pid, Process_getCommand(this->process)); |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 50 | } |
| 51 | |
| Christian Göttsche | ba282cf | 2020-10-05 13:19:50 +0200 | [diff] [blame] | 52 | const InfoScreenClass CommandScreen_class = { |
| ryenus | f4bb502 | 2017-08-03 17:43:28 +0800 | [diff] [blame] | 53 | .super = { |
| 54 | .extends = Class(Object), |
| 55 | .delete = CommandScreen_delete |
| 56 | }, |
| 57 | .scan = CommandScreen_scan, |
| 58 | .draw = CommandScreen_draw |
| 59 | }; |
| 60 | |
| 61 | CommandScreen* CommandScreen_new(Process* process) { |
| 62 | CommandScreen* this = AllocThis(CommandScreen); |
| 63 | return (CommandScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 3, " "); |
| 64 | } |
| 65 | |
| 66 | void CommandScreen_delete(Object* this) { |
| 67 | free(InfoScreen_done((InfoScreen*)this)); |
| 68 | } |