From 466d4da0c6e7255a38b8de3c3b9dc0373805cc41 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 12 Jan 2016 06:00:58 -0200 Subject: refactor *Screen classes, add InfoScreen superclass --- OpenFilesScreen.c | 133 +++++++++--------------------------------------------- 1 file changed, 22 insertions(+), 111 deletions(-) (limited to 'OpenFilesScreen.c') diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index 2a5e20af..096ed939 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -9,7 +9,6 @@ in the source distribution for its full text. #include "CRT.h" #include "ProcessList.h" -#include "ListItem.h" #include "IncSet.h" #include "StringUtils.h" #include "FunctionBar.h" @@ -25,8 +24,7 @@ in the source distribution for its full text. #include /*{ -#include "Process.h" -#include "Panel.h" +#include "InfoScreen.h" typedef struct OpenFiles_Data_ { char* data[256]; @@ -44,43 +42,37 @@ typedef struct OpenFiles_FileData_ { } OpenFiles_FileData; typedef struct OpenFilesScreen_ { - Process* process; + InfoScreen super; pid_t pid; - Panel* display; } OpenFilesScreen; }*/ -static const char* OpenFilesScreenFunctions[] = {"Search ", "Filter ", "Refresh", "Done ", NULL}; - -static const char* OpenFilesScreenKeys[] = {"F3", "F4", "F5", "Esc"}; - -static int OpenFilesScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(5), 27}; +InfoScreenClass OpenFilesScreen_class = { + .super = { + .extends = Class(Object), + .delete = OpenFilesScreen_delete + }, + .scan = OpenFilesScreen_scan, + .draw = OpenFilesScreen_draw +}; OpenFilesScreen* OpenFilesScreen_new(Process* process) { OpenFilesScreen* this = malloc(sizeof(OpenFilesScreen)); - this->process = process; - FunctionBar* bar = FunctionBar_new(OpenFilesScreenFunctions, OpenFilesScreenKeys, OpenFilesScreenEvents); - this->display = Panel_new(0, 1, COLS, LINES-3, false, Class(ListItem), bar); + Object_setClass(this, Class(OpenFilesScreen)); if (Process_isThread(process)) this->pid = process->tgid; else this->pid = process->pid; - return this; + return (OpenFilesScreen*) InfoScreen_init(&this->super, process, NULL, LINES-3, " FD TYPE DEVICE SIZE NODE NAME"); } -void OpenFilesScreen_delete(OpenFilesScreen* this) { - Panel_delete((Object*)this->display); - free(this); +void OpenFilesScreen_delete(Object* this) { + free(InfoScreen_done((InfoScreen*)this)); } -static void OpenFilesScreen_draw(OpenFilesScreen* this, IncSet* inc) { - attrset(CRT_colors[METER_TEXT]); - mvhline(0, 0, ' ', COLS); - mvprintw(0, 0, "Snapshot of files open in process %d - %s", this->pid, this->process->comm); - attrset(CRT_colors[DEFAULT_COLOR]); - Panel_draw(this->display, true); - IncSet_drawBar(inc); +void OpenFilesScreen_draw(InfoScreen* this) { + InfoScreen_drawTitled(this, "Snapshot of files open in process %d - %s", ((OpenFilesScreen*)this)->pid, this->process->comm); } static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { @@ -122,27 +114,21 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { return pdata; } -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 inline void OpenFiles_Data_clear(OpenFiles_Data* data) { for (int i = 0; i < 255; i++) if (data->data[i]) free(data->data[i]); } -static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* inc) { +void OpenFilesScreen_scan(InfoScreen* this) { Panel* panel = this->display; int idx = Panel_getSelectedIndex(panel); Panel_prune(panel); - OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(this->pid); + OpenFiles_ProcessData* pdata = OpenFilesScreen_getProcessData(((OpenFilesScreen*)this)->pid); if (pdata->error == 127) { - addLine("Could not execute 'lsof'. Please make sure it is available in your $PATH.", lines, panel, IncSet_filter(inc)); + InfoScreen_addLine(this, "Could not execute 'lsof'. Please make sure it is available in your $PATH."); } else if (pdata->error == 1) { - addLine("Failed listing open files.", lines, panel, IncSet_filter(inc)); + InfoScreen_addLine(this, "Failed listing open files."); } else { OpenFiles_FileData* fdata = pdata->files; while (fdata) { @@ -155,7 +141,7 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* i data['s'] ? data['s'] : "", data['i'] ? data['i'] : "", data['n'] ? data['n'] : ""); - addLine(entry, lines, panel, IncSet_filter(inc)); + InfoScreen_addLine(this, entry); OpenFiles_Data_clear(&fdata->data); OpenFiles_FileData* old = fdata; fdata = fdata->next; @@ -164,82 +150,7 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* i OpenFiles_Data_clear(&pdata->data); } free(pdata); - Vector_insertionSort(lines); + Vector_insertionSort(this->lines); Vector_insertionSort(panel->items); Panel_setSelected(panel, idx); } - -void OpenFilesScreen_run(OpenFilesScreen* this) { - Panel* panel = this->display; - Panel_setHeader(panel, " FD TYPE DEVICE SIZE NODE NAME"); - - FunctionBar* bar = panel->defaultBar; - IncSet* inc = IncSet_new(bar); - - Vector* lines = Vector_new(panel->items->type, true, DEFAULT_SIZE); - - OpenFilesScreen_scan(this, lines, inc); - OpenFilesScreen_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(); - OpenFilesScreen_scan(this, lines, inc); - OpenFilesScreen_draw(this, inc); - break; - case '\014': // Ctrl+L - clear(); - OpenFilesScreen_draw(this, inc); - break; - case 'q': - case 27: - case KEY_F(10): - looping = false; - break; - case KEY_RESIZE: - Panel_resize(panel, COLS, LINES-2); - OpenFilesScreen_draw(this, inc); - break; - default: - Panel_onKey(panel, ch); - } - } - - Vector_delete(lines); - IncSet_delete(inc); -} -- cgit v1.2.3