summaryrefslogtreecommitdiffstats
path: root/CommandScreen.c
blob: d8e709c4934af6e3def18295d45f1ce28d8071f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "CommandScreen.h"

#include "config.h"
#include "CRT.h"
#include "IncSet.h"
#include "ListItem.h"
#include "Platform.h"
#include "StringUtils.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>


static void CommandScreen_scan(InfoScreen* this) {
   Panel* panel = this->display;
   int idx = MAXIMUM(Panel_getSelectedIndex(panel), 0);
   Panel_prune(panel);

   const char* p = this->process->comm;
   char* line = xMalloc(COLS + 1);
   int line_offset = 0, last_spc = -1, len;
   for (; *p != '\0'; p++, line_offset++) {
      line[line_offset] = *p;
      if (*p == ' ') last_spc = line_offset;

      if (line_offset == COLS) {
         len = (last_spc == -1) ? line_offset : last_spc;
         line[len] = '\0';
         InfoScreen_addLine(this, line);

         line_offset -= len;
         last_spc = -1;
         memcpy(line, p - line_offset, line_offset + 1);
      }
   }

   if (line_offset > 0) {
      line[line_offset] = '\0';
      InfoScreen_addLine(this, line);
   }

   free(line);
   Panel_setSelected(panel, idx);
}

static void CommandScreen_draw(InfoScreen* this) {
   char* title = xMalloc(COLS + 1);
   int len = snprintf(title, COLS + 1, "Command of process %d - %s", this->process->pid, this->process->comm);
   if (len > COLS) {
      memset(&title[COLS - 3], '.', 3);
   }

   InfoScreen_drawTitled(this, "%s", title);
   free(title);
}

InfoScreenClass CommandScreen_class = {
   .super = {
      .extends = Class(Object),
      .delete = CommandScreen_delete
   },
   .scan = CommandScreen_scan,
   .draw = CommandScreen_draw
};

CommandScreen* CommandScreen_new(Process* process) {
   CommandScreen* this = AllocThis(CommandScreen);
   return (CommandScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 3, " ");
}

void CommandScreen_delete(Object* this) {
   free(InfoScreen_done((InfoScreen*)this));
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy