Skip to content

Commit

Permalink
Add buffer working
Browse files Browse the repository at this point in the history
  • Loading branch information
xorg62 committed Feb 27, 2012
1 parent 8850b91 commit a6381e5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
12 changes: 7 additions & 5 deletions src/hftirc.h
Expand Up @@ -25,8 +25,9 @@
#include <ncurses/ncurses.h>
#endif

#define BUFSIZE (4096)
#define HISTOLEN (64)
#define BUFSIZE (4096)
#define HISTOLEN (64)
#define BUFHISTOLEN (128)

typedef unsigned long Flags;

Expand Down Expand Up @@ -68,7 +69,8 @@ struct nick
struct buffer_line
{
char *line;
SLIST_ENTRY(buffer_line) next;
int id;
STAILQ_ENTRY(buffer_line) next;
};

struct buffer
Expand All @@ -78,10 +80,10 @@ struct buffer
#define ACT_NO 0
#define ACT_NORMAL 1
#define ACT_HL 2
int act;
int act, nline;
char *name;
char *topic;
SLIST_HEAD(, buffer_line) lines;
STAILQ_HEAD(, buffer_line) lines;
SLIST_HEAD(, nick) nicks;
TAILQ_ENTRY(buffer) next;
};
Expand Down
63 changes: 50 additions & 13 deletions src/ui.c
Expand Up @@ -20,8 +20,9 @@ ui_buffer_new(struct session *session, char *name)
b->topic = NULL;
b->act = ACT_NO;
b->id = (p ? p->id + 1 : 0);
b->nline = 0;

SLIST_INIT(&b->lines);
STAILQ_INIT(&b->lines);
SLIST_INIT(&b->nicks);

TAILQ_INSERT_TAIL(&H.h.buffer, b, next);
Expand All @@ -37,10 +38,10 @@ ui_buffer_remove(struct buffer *b)
free(b->name);
free(b->topic);

while(!SLIST_EMPTY(&b->lines))
while(!STAILQ_EMPTY(&b->lines))
{
bl = SLIST_FIRST(&b->lines);
SLIST_REMOVE_HEAD(&b->lines, next);
bl = STAILQ_FIRST(&b->lines);
STAILQ_REMOVE_HEAD(&b->lines, next);
free(bl->line);
free(bl);
}
Expand Down Expand Up @@ -93,6 +94,9 @@ ui_init(void)
b->topic = xstrdup("HFTIrc2 -- status buffer");
H.bufsel = b;
H.flags ^= HFTIRC_FIRST_TIME;

b = ui_buffer_new(NULL, "b1");
b->topic = xstrdup("buffer 1");
}

setlocale(LC_ALL, "");
Expand Down Expand Up @@ -173,25 +177,41 @@ static void
ui_print_line(struct buffer_line *bl)
{
waddstr(H.ui.mainwin, bl->line);
wrefresh(H.ui.mainwin);
}

static void
ui_update_buf(void)
{
struct buffer_line *b;

SLIST_FOREACH(b, &H.bufsel->lines, next)
werase(H.ui.mainwin);

STAILQ_FOREACH(b, &H.bufsel->lines, next)
ui_print_line(b);

wrefresh(H.ui.mainwin);
}


static struct buffer_line*
ui_buffer_new_line(struct buffer *b, char *str)
ui_buffer_line_new(struct buffer *b, char *str)
{
struct buffer_line *bl = xcalloc(1, sizeof(struct buffer_line));
struct buffer_line *h, *bl = xcalloc(1, sizeof(struct buffer_line));

bl->line = str;
SLIST_INSERT_HEAD(&b->lines, bl, next);
bl->id = b->nline++;

STAILQ_INSERT_TAIL(&b->lines, bl, next);

/* Remove head if histo length is reached */
if(b->nline >= BUFHISTOLEN)
{
h = STAILQ_FIRST(&b->lines);
STAILQ_REMOVE_HEAD(&b->lines, next);
free(h->line);
free(h);
--b->nline;
}

return bl;
}
Expand All @@ -209,17 +229,34 @@ ui_print_buf(struct buffer *b, char *fmt, ...)

xasprintf(&fstr, "[dev:4:20] %s\n", str);

bl = ui_buffer_new_line(b, fstr);
bl = ui_buffer_line_new(b, fstr);

if(b == H.bufsel)
{
ui_print_line(bl);
wrefresh(H.ui.mainwin);
}

free(str);
}

void
ui_buffer_set(struct buffer *b)
{
b->act = ACT_NO;

H.bufsel = b;

if(b != TAILQ_FIRST(&H.h.buffer))
H.sessionsel = b->session;

ui_update_buf();
}

void
ui_get_input(void)
{
struct buffer *bf;
struct inputbuffer *ib = &H.ui.ib;
wchar_t tmpbuf[BUFSIZE], *cmp;
char buf[BUFSIZE];
Expand All @@ -232,7 +269,6 @@ ui_get_input(void)
default:
switch(c)
{

case KEY_HOME:
wmove(H.ui.inputwin, 0, 0);
ib->pos = ib->cpos = 0;
Expand Down Expand Up @@ -500,9 +536,10 @@ ui_get_input(void)
/* /<num> to go on the buffer num */
if(buf[0] == '/' &&
((isdigit(buf[1]) && (n= atoi(&buf[1])) >= 0 && n < 10) /* /n */
|| (buf[1] == ' ' && (n = atoi(&buf[2])) > 9))) /* / nn */
|| (buf[1] == ' ' && (n = atoi(&buf[2])) > 9)) /* / nn */
&& (bf = ui_buffer_gb_id(n)))
{
/*ui_buf_set(n);*/
ui_buffer_set(bf);
werase(H.ui.inputwin);
wmemset(ib->buffer, 0, BUFSIZE);
ib->pos = ib->cpos = ib->split = ib->hits = 0;
Expand Down
13 changes: 13 additions & 0 deletions src/ui.h
Expand Up @@ -35,5 +35,18 @@ void ui_buffer_remove(struct buffer *b);
void ui_get_input(void);
void ui_update(void);
void ui_print_buf(struct buffer *b, char *fmt, ...);
void ui_buffer_set(struct buffer *b);

static inline struct buffer*
ui_buffer_gb_id(int n)
{
struct buffer *b;

TAILQ_FOREACH(b, &H.h.buffer, next)
if(b->id == n)
return b;

return NULL;
}

#endif

0 comments on commit a6381e5

Please sign in to comment.