Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor *Screen classes, add InfoScreen superclass
  • Loading branch information
hishamhm committed Jan 12, 2016
1 parent faf2860 commit 466d4da
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 407 deletions.
19 changes: 11 additions & 8 deletions Action.c
Expand Up @@ -346,9 +346,9 @@ static Htop_Reaction actionSetup(State* st) {
static Htop_Reaction actionLsof(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
OpenFilesScreen* ts = OpenFilesScreen_new(p);
OpenFilesScreen_run(ts);
OpenFilesScreen_delete(ts);
OpenFilesScreen* ofs = OpenFilesScreen_new(p);
InfoScreen_run((InfoScreen*)ofs);
OpenFilesScreen_delete((Object*)ofs);
clear();
CRT_enableDelay();
return HTOP_REFRESH | HTOP_REDRAW_BAR;
Expand All @@ -358,8 +358,11 @@ static Htop_Reaction actionStrace(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
TraceScreen* ts = TraceScreen_new(p);
TraceScreen_run(ts);
TraceScreen_delete(ts);
bool ok = TraceScreen_forkTracer(ts);
if (ok) {
InfoScreen_run((InfoScreen*)ts);
}
TraceScreen_delete((Object*)ts);
clear();
CRT_enableDelay();
return HTOP_REFRESH | HTOP_REDRAW_BAR;
Expand Down Expand Up @@ -504,9 +507,9 @@ static Htop_Reaction actionTagAllChildren(State* st) {
static Htop_Reaction actionShowEnvScreen(State* st) {
Process* p = (Process*) Panel_getSelected(st->panel);
if (!p) return HTOP_OK;
EnvScreen* ts = EnvScreen_new(p);
EnvScreen_run(ts);
EnvScreen_delete(ts);
EnvScreen* es = EnvScreen_new(p);
InfoScreen_run((InfoScreen*)es);
EnvScreen_delete((Object*)es);
clear();
CRT_enableDelay();
return HTOP_REFRESH | HTOP_REDRAW_BAR;
Expand Down
130 changes: 20 additions & 110 deletions EnvScreen.c
Expand Up @@ -12,52 +12,37 @@
#include <unistd.h>

/*{
#include "ProcessList.h"
#include "Panel.h"
#include "FunctionBar.h"
#include "InfoScreen.h"
typedef struct EnvScreen_ {
Process* process;
Panel* display;
FunctionBar* bar;
InfoScreen super;
} EnvScreen;
}*/

static const char* EnvScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL};

static const char* EnvScreenKeys[] = {"F3", "F4", "F5", "Esc"};

static int EnvScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27};
InfoScreenClass EnvScreen_class = {
.super = {
.extends = Class(Object),
.delete = EnvScreen_delete
},
.scan = EnvScreen_scan,
.draw = EnvScreen_draw
};

EnvScreen* EnvScreen_new(Process* process) {
EnvScreen* this = malloc(sizeof(EnvScreen));
this->process = process;
FunctionBar* bar = FunctionBar_new(EnvScreenFunctions, EnvScreenKeys, EnvScreenEvents);
this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar);
return this;
Object_setClass(this, Class(EnvScreen));
return (EnvScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " ");
}

void EnvScreen_delete(EnvScreen* this) {
Panel_delete((Object*)this->display);
free(this);
void EnvScreen_delete(Object* this) {
free(InfoScreen_done((InfoScreen*)this));
}

static void EnvScreen_draw(EnvScreen* this, IncSet* inc) {
attrset(CRT_colors[METER_TEXT]);
mvhline(0, 0, ' ', COLS);
mvprintw(0, 0, "environment of process %d - %s", this->process->pid, this->process->comm);
attrset(CRT_colors[DEFAULT_COLOR]);
Panel_draw(this->display, true);
IncSet_drawBar(inc);
void EnvScreen_draw(InfoScreen* this) {
InfoScreen_drawTitled(this, "Environment of process %d - %s", this->process->pid, this->process->comm);
}

static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) {
Vector_add(lines, (Object*) ListItem_new(line, 0));
if (!incFilter || String_contains_i(line, incFilter))
Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1));
}

static void EnvScreen_scan(EnvScreen* this, Vector* lines, IncSet* inc) {
void EnvScreen_scan(InfoScreen* this) {
Panel* panel = this->display;
int idx = MAX(Panel_getSelectedIndex(panel), 0);

Expand All @@ -69,89 +54,14 @@ static void EnvScreen_scan(EnvScreen* this, Vector* lines, IncSet* inc) {
seteuid(euid);
if (env) {
for (char *p = env; *p; p = strrchr(p, 0)+1)
addLine(p, lines, panel, IncSet_filter(inc));
InfoScreen_addLine(this, p);
free(env);
}
else {
addLine("Could not read process environment.", lines, panel, IncSet_filter(inc));
InfoScreen_addLine(this, "Could not read process environment.");
}

Vector_insertionSort(lines);
Vector_insertionSort(this->lines);
Vector_insertionSort(panel->items);
Panel_setSelected(panel, idx);
}

void EnvScreen_run(EnvScreen* this) {
Panel* panel = this->display;
Panel_setHeader(panel, " ");

FunctionBar* bar = panel->defaultBar;
IncSet* inc = IncSet_new(bar);

Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE);

EnvScreen_scan(this, lines, inc);
EnvScreen_draw(this, inc);

bool looping = true;
while (looping) {

Panel_draw(panel, true);

if (inc->active)
move(LINES-1, CRT_cursorX);
int ch = getch();

if (ch == KEY_MOUSE) {
MEVENT mevent;
int ok = getmouse(&mevent);
if (ok == OK)
if (mevent.y >= panel->y && mevent.y < LINES - 1) {
Panel_setSelected(panel, mevent.y - panel->y + panel->scrollV);
ch = 0;
} if (mevent.y == LINES - 1)
ch = IncSet_synthesizeEvent(inc, mevent.x);
}

if (inc->active) {
IncSet_handleKey(inc, ch, panel, IncSet_getListItemValue, lines);
continue;
}

switch(ch) {
case ERR:
continue;
case KEY_F(3):
case '/':
IncSet_activate(inc, INC_SEARCH, panel);
break;
case KEY_F(4):
case '\\':
IncSet_activate(inc, INC_FILTER, panel);
break;
case KEY_F(5):
clear();
EnvScreen_scan(this, lines, inc);
EnvScreen_draw(this, inc);
break;
case '\014': // Ctrl+L
clear();
EnvScreen_draw(this, inc);
break;
case 'q':
case 27:
case KEY_F(10):
looping = false;
break;
case KEY_RESIZE:
Panel_resize(panel, COLS, LINES-2);
EnvScreen_draw(this, inc);
break;
default:
Panel_onKey(panel, ch);
}
}

Vector_delete(lines);
IncSet_delete(inc);
}
16 changes: 8 additions & 8 deletions EnvScreen.h
Expand Up @@ -3,20 +3,20 @@
#ifndef HEADER_EnvScreen
#define HEADER_EnvScreen

#include "ProcessList.h"
#include "Panel.h"
#include "FunctionBar.h"
#include "InfoScreen.h"

typedef struct EnvScreen_ {
Process* process;
Panel* display;
FunctionBar* bar;
InfoScreen super;
} EnvScreen;

extern InfoScreenClass EnvScreen_class;

EnvScreen* EnvScreen_new(Process* process);

void EnvScreen_delete(EnvScreen* this);
void EnvScreen_delete(Object* this);

void EnvScreen_draw(InfoScreen* this);

void EnvScreen_run(EnvScreen* this);
void EnvScreen_scan(InfoScreen* this);

#endif

0 comments on commit 466d4da

Please sign in to comment.