Skip to content

Commit

Permalink
syncronize to use same RichString as htop
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Nov 23, 2012
1 parent 1c9924c commit c0deb31
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 86 deletions.
63 changes: 36 additions & 27 deletions Display.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdbool.h>

#if HAVE_CURSES_H
#if HAVE_CURSES

static SCREEN* CRT_term;

Expand All @@ -26,13 +26,22 @@ static Hashtable* Display_terminalSequences;
#endif

/*{
#ifdef HAVE_LIBNCURSES
#define HAVE_CURSES_H 1
#endif
#if HAVE_CURSES_H
#include "config.h"
#ifdef HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#define HAVE_CURSES 1
#undef mvaddchnstr
#define mvaddchnstr mvadd_wchnstr
#elif HAVE_NCURSES_NCURSES_H
#include <ncurses/ncurses.h>
#define HAVE_CURSES 1
#elif HAVE_NCURSES_H
#include <ncurses.h>
#define HAVE_CURSES 1
#elif HAVE_CURSES
#include <curses.h>
#define HAVE_CURSES 1
#else
typedef unsigned long chtype;
Expand Down Expand Up @@ -191,7 +200,7 @@ typedef struct mevent {
}*/

void Display_getScreenSize(int* w, int* h) {
#if HAVE_CURSES_H
#if HAVE_CURSES
*w = COLS;
*h = LINES;
#else
Expand All @@ -207,7 +216,7 @@ void Display_getScreenSize(int* w, int* h) {
#endif
}

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_printAt mvprintw
#else
void Display_printAt(int y, int x, const char* fmt, ...) {
Expand All @@ -219,7 +228,7 @@ void Display_printAt(int y, int x, const char* fmt, ...) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_writeAt mvaddstr
#define Display_writeAtn mvaddnstr
#define Display_writeChAt mvaddch
Expand All @@ -238,7 +247,7 @@ void Display_writeChAt(int y, int x, const char ch) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_move move
#else
void Display_move(int y, int x) {
Expand All @@ -248,7 +257,7 @@ void Display_move(int y, int x) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_mvhline mvhline
#else
void Display_mvhline(int y, int x, char c, int qty) {
Expand All @@ -258,7 +267,7 @@ void Display_mvhline(int y, int x, char c, int qty) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_mvvline mvvline
#else
void Display_mvvline(int y, int x, char c, int qty) {
Expand All @@ -268,7 +277,7 @@ void Display_mvvline(int y, int x, char c, int qty) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_attrset attrset
#else

Expand Down Expand Up @@ -314,18 +323,18 @@ void Display_attrset(unsigned long attr) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_clear clear
#else
void Display_clear() {
fwrite("\033[H\033[2J", 6, 1, stdout);
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_writeChstrAtn mvaddchnstr
#else
void Display_writeChstrAtn(int y, int x, const chtype *chstr, int n) {
void Display_writeChstrAtn(int y, int x, CharType* chstr, int n) {
printf("\033[%d;%df", y+1, x+1, y, x, n);
/*
char str[n];
Expand Down Expand Up @@ -360,15 +369,15 @@ void Display_writeChstrAtn(int y, int x, const chtype *chstr, int n) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_clearToEol clrtoeol
#else
void Display_clearToEol() {
fwrite("\033[K", 3, 1, stdout);
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
void Display_getyx(int* y, int* x) {
getyx(stdscr, *y, *x);
}
Expand All @@ -379,7 +388,7 @@ void Display_getyx(int* y, int* x) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_getch getch
#else
int Display_getch() {
Expand All @@ -404,31 +413,31 @@ int Display_getch() {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_defineKey define_key
#else
void Display_defineKey(const char* sequence, int keynum) {
Hashtable_putString(Display_terminalSequences, sequence, (void*) keynum);
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_bkgdset bkgdset
#else
void Display_bkgdset(int color) {
// TODO
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_beep beep
#else
void Display_beep() {
fwrite("\007", 1, 1, stdout);
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_getmouse getmouse
#else
int Display_getmouse(MEVENT* mevent) {
Expand All @@ -437,15 +446,15 @@ int Display_getmouse(MEVENT* mevent) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
#define Display_refresh refresh
#else
void Display_refresh() {
// TODO
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
bool Display_init(char* term) {
CRT_term = newterm(term, stdout, stdin);
raw();
Expand Down Expand Up @@ -491,7 +500,7 @@ bool Display_init(char* term) {
}
#endif

#if HAVE_CURSES_H
#if HAVE_CURSES
void Display_done() {
curs_set(1);
endwin();
Expand Down
1 change: 1 addition & 0 deletions Hashtable.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>

Expand Down
5 changes: 3 additions & 2 deletions Line.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
Expand Down Expand Up @@ -148,8 +149,8 @@ void Line_display(Object* cast, RichString* str) {
}

if (str && outIndex >= scrollH) {
RichString_append(str, out + scrollH, outIndex - scrollH);
RichString_setAttrs(str, attrs + scrollH);
RichString_appendn(str, 0, out + scrollH, outIndex - scrollH);
RichString_paintAttrs(str, attrs + scrollH);
}
this->context = Highlight_getContext(hl);
}
Expand Down
2 changes: 1 addition & 1 deletion Object.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void Object_delete(Object* this) {
void Object_display(Object* this, RichString* out) {
unsigned char objAddress[50];
sprintf((char*)objAddress, "O:%p C:%p", (void*) this, (void*) this->class);
RichString_write(out, objAddress);
RichString_write(out, 0, objAddress);
}

bool Object_equals(const Object* this, const Object* o) {
Expand Down
35 changes: 21 additions & 14 deletions Panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Panel* Panel_new(int x, int y, int w, int h, int color, ListItemClass* class, bo

void Panel_delete(Object* cast) {
Panel* this = (Panel*)cast;
RichString_end(this->header);
Panel_done(this);
free(this);
}
Expand All @@ -79,7 +80,7 @@ void Panel_init(Panel* this, int x, int y, int w, int h, int color, ListItemClas
this->cursorX = 0;
this->displaying = 0;
this->focus = true;
RichString_prune(&(this->header));
RichString_beginAllocated(this->header);
}

void Panel_done(Panel* this) {
Expand Down Expand Up @@ -108,7 +109,7 @@ void Panel_move(Panel* this, int x, int y) {
void Panel_resize(Panel* this, int w, int h) {
assert (this != NULL);

if (this->header.len > 0)
if (RichString_sizeVal(this->header) > 0)
h--;
this->w = w;
this->h = h;
Expand Down Expand Up @@ -228,13 +229,14 @@ void Panel_draw(Panel* this) {
assert(first >= 0);
assert(last <= itemCount);

if (this->header.len > 0) {
int headerLen = RichString_sizeVal(this->header);
if (headerLen > 0) {
Display_attrset(CRT_colors[HeaderColor]);
Display_mvhline(y, x, ' ', w);
if (scrollH < this->header.len) {
assert(this->header.len > 0);
if (scrollH < headerLen) {
assert(headerLen > 0);
Display_writeChstrAtn(y, x, this->header.chstr + scrollH,
MIN(this->header.len - scrollH, w));
MIN(headerLen - scrollH, w));
}
y++;
}
Expand All @@ -254,10 +256,10 @@ void Panel_draw(Panel* this) {
for(int i = first, j = 0; j < h && i < last; i++, j++) {
Object* itemObj = (Object*) List_get(this->items, i);
assert(itemObj);
RichString itemRef; RichString_init(&itemRef);
RichString_begin(itemRef);
this->displaying = i;
Msg(Object, display, itemObj, &itemRef);
int amt = MIN(itemRef.len - scrollH, w);
int amt = MIN(RichString_sizeVal(itemRef) - scrollH, w);
if (i == this->selected) {
if (this->highlightBar) {
Display_attrset(highlight);
Expand All @@ -275,6 +277,7 @@ void Panel_draw(Panel* this) {
if (amt > 0)
Display_writeChstrAtn(y+j, x+0, itemRef.chstr + scrollH, amt);
}
RichString_end(itemRef);
}
for (int i = y + (last - first); i < y + h; i++)
Display_mvhline(i, x+0, ' ', w);
Expand Down Expand Up @@ -305,26 +308,30 @@ void Panel_draw(Panel* this) {

} else {
Object* oldObj = (Object*) List_get(this->items, this->oldSelected);
RichString oldRef; RichString_init(&oldRef);
RichString_begin(oldRef);
this->displaying = this->oldSelected;
Msg(Object, display, oldObj, &oldRef);
Object* newObj = (Object*) List_get(this->items, this->selected);
RichString newRef; RichString_init(&newRef);
RichString_begin(newRef);
this->displaying = this->selected;
Msg(Object, display, newObj, &newRef);
Display_mvhline(y+ this->oldSelected - this->scrollV, x+0, ' ', w);
if (scrollH < oldRef.len)
Display_writeChstrAtn(y+ this->oldSelected - this->scrollV, x+0, oldRef.chstr + scrollH, MIN(oldRef.len - scrollH, w));
int oldLen = RichString_sizeVal(oldRef);
if (scrollH < oldLen)
Display_writeChstrAtn(y+ this->oldSelected - this->scrollV, x+0, oldRef.chstr + scrollH, MIN(oldLen - scrollH, w));
if (this->highlightBar)
Display_attrset(highlight);
cursorY = y+this->selected - this->scrollV;
Display_mvhline(cursorY, x+0, ' ', w);
if (this->highlightBar)
RichString_setAttr(&newRef, highlight);
if (scrollH < newRef.len)
Display_writeChstrAtn(y+this->selected - this->scrollV, x+0, newRef.chstr + scrollH, MIN(newRef.len - scrollH, w));
int newLen = RichString_sizeVal(newRef);
if (scrollH < newLen)
Display_writeChstrAtn(y+this->selected - this->scrollV, x+0, newRef.chstr + scrollH, MIN(newLen - scrollH, w));
if (this->highlightBar)
Display_attrset(this->color);
RichString_end(oldRef);
RichString_end(newRef);
}
this->oldSelected = this->selected;

Expand Down
Loading

0 comments on commit c0deb31

Please sign in to comment.