blob: d34282901f686f261fcca51ddc41e0ed81c7f6ef [file] [log] [blame]
Benny Baumann0f526292020-09-19 13:55:23 +02001#include "config.h" // IWYU pragma: keep
ryenusf4bb5022017-08-03 17:43:28 +08002
Benny Baumann0f526292020-09-19 13:55:23 +02003#include "CommandScreen.h"
ryenusf4bb5022017-08-03 17:43:28 +08004
5#include <stdlib.h>
6#include <string.h>
Benny Baumann0f526292020-09-19 13:55:23 +02007
8#include "Macros.h"
9#include "Panel.h"
10#include "ProvideCurses.h"
11#include "XUtils.h"
ryenusf4bb5022017-08-03 17:43:28 +080012
13
ryenusf4bb5022017-08-03 17:43:28 +080014static void CommandScreen_scan(InfoScreen* this) {
15 Panel* panel = this->display;
16 int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
ryenusf4bb5022017-08-03 17:43:28 +080017 Panel_prune(panel);
18
Narendran Gopalakrishnan09fe94d2020-10-17 16:24:45 +053019 const char* p = Process_getCommand(this->process);
ryenusf4bb5022017-08-03 17:43:28 +080020 char* line = xMalloc(COLS + 1);
21 int line_offset = 0, last_spc = -1, len;
22 for (; *p != '\0'; p++, line_offset++) {
ryenus214c7422020-09-26 05:22:24 +080023 line[line_offset] = *p;
Benny Baumann45869512020-11-01 01:09:51 +010024 if (*p == ' ') {
25 last_spc = line_offset;
26 }
ryenusf4bb5022017-08-03 17:43:28 +080027
28 if (line_offset == COLS) {
29 len = (last_spc == -1) ? line_offset : last_spc;
ryenus214c7422020-09-26 05:22:24 +080030 line[len] = '\0';
31 InfoScreen_addLine(this, line);
32
ryenusf4bb5022017-08-03 17:43:28 +080033 line_offset -= len;
34 last_spc = -1;
ryenus214c7422020-09-26 05:22:24 +080035 memcpy(line, p - line_offset, line_offset + 1);
ryenusf4bb5022017-08-03 17:43:28 +080036 }
37 }
38
ryenus214c7422020-09-26 05:22:24 +080039 if (line_offset > 0) {
40 line[line_offset] = '\0';
41 InfoScreen_addLine(this, line);
42 }
ryenusf4bb5022017-08-03 17:43:28 +080043
44 free(line);
45 Panel_setSelected(panel, idx);
46}
47
48static void CommandScreen_draw(InfoScreen* this) {
Narendran Gopalakrishnan09fe94d2020-10-17 16:24:45 +053049 InfoScreen_drawTitled(this, "Command of process %d - %s", this->process->pid, Process_getCommand(this->process));
ryenusf4bb5022017-08-03 17:43:28 +080050}
51
Christian Göttscheba282cf2020-10-05 13:19:50 +020052const InfoScreenClass CommandScreen_class = {
ryenusf4bb5022017-08-03 17:43:28 +080053 .super = {
54 .extends = Class(Object),
55 .delete = CommandScreen_delete
56 },
57 .scan = CommandScreen_scan,
58 .draw = CommandScreen_draw
59};
60
61CommandScreen* CommandScreen_new(Process* process) {
62 CommandScreen* this = AllocThis(CommandScreen);
63 return (CommandScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 3, " ");
64}
65
66void CommandScreen_delete(Object* this) {
67 free(InfoScreen_done((InfoScreen*)this));
68}