1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Added buffer handling. Buffer are initialized, pushed, and freed. No use is made of them so far

Signed-off-by: James Booth <boothj5@gmail.com>
This commit is contained in:
Immae 2014-06-23 00:44:35 +02:00 committed by James Booth
parent 66ad23b35b
commit 6a9e19303e
5 changed files with 142 additions and 1 deletions

View File

@ -17,6 +17,7 @@ core_sources = \
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
src/ui/console.c src/ui/notifier.c \
src/ui/windows.c src/ui/windows.h \
src/ui/buffer.c \
src/command/command.h src/command/command.c src/command/history.c \
src/command/commands.h src/command/commands.c \
src/command/history.h src/tools/parser.c \
@ -53,6 +54,7 @@ tests_sources = \
src/config/theme.c src/config/theme.h \
src/ui/windows.c src/ui/windows.h \
src/ui/window.c src/ui/window.h \
src/ui/buffer.c \
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
src/server_events.c src/server_events.h \

106
src/ui/buffer.c Normal file
View File

@ -0,0 +1,106 @@
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <glib.h>
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
#endif
#include "ui/window.h"
#include "ui/buffer.h"
ProfBuff*
buffer_create() {
ProfBuff* new_buff = malloc(sizeof(struct prof_buff_t));
new_buff->wrap = 0;
new_buff->current = 0;
return new_buff;
}
void
buffer_free(ProfBuff* buffer) {
int i = 0;
int imax = buffer->current;
if(buffer->wrap == 1) {
imax = BUFF_SIZE;
}
for(i = 0; i < imax; i++) {
free(buffer->entry[i].message);
free(buffer->entry[i].from);
}
free(buffer);
}
void
buffer_push(ProfBuff* buffer, const char show_char, GTimeVal *tstamp, int flags, int attrs, const char * const from, const char * const message) {
int *current = &(buffer->current);
ProfBuffEntry e = buffer->entry[*current];
if(buffer->wrap == 1) {
free(e.message);
free(e.from);
}
e.show_char = show_char;
e.tstamp = *tstamp;
e.flags = flags;
e.attrs = attrs;
e.from = malloc(strlen(from)+1);
strcpy(e.from, from);
e.message = malloc(strlen(message)+1);
strcpy(e.message, message);
*current += 1;
if(*current >= BUFF_SIZE) {
*current = 0;
buffer->wrap = 1;
}
}
int
buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list) {
//Returns the (line+1)-th last line
//e.g. if line == 0, returns the last line
//To correct for splitted lines...
int current = buffer->current;
int imax = current;
if(buffer->wrap == 1) {
imax = BUFF_SIZE;
}
int i = 1;
int j = 0;
while(i <= imax) {
ProfBuffEntry e = buffer->entry[(current - i) % BUFF_SIZE];
if(j == line && (e.flags & NO_EOL) == 0) {
//We found our line and we are at its last entry
int nb_entries = 1;
while(i + nb_entries <= imax) {
e = buffer->entry[(current - i - nb_entries) % BUFF_SIZE];
if((e.flags & NO_EOL) == 0) {
break;
}
nb_entries += 1;
}
if((buffer->wrap == 1) && (i + nb_entries > imax)) {
//The line is at the top of the buffer, we don't know if it's complete
return 0;
}
else {
*list = &(buffer->entry[(current - i - nb_entries + 1) % BUFF_SIZE]);
return nb_entries;
}
}
if((e.flags & NO_EOL) == 0) {
j++;
}
i++;
}
return 0;
}

29
src/ui/buffer.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef UI_BUFFER_H
#define UI_BUFFER_H
#include "config.h"
//#include "ui/window.h"
#define BUFF_SIZE 1000
typedef struct prof_buff_entry_t {
char show_char;
GTimeVal tstamp;
int flags;
int attrs;
char *from;
char *message;
} ProfBuffEntry;
typedef struct prof_buff_t {
ProfBuffEntry entry[BUFF_SIZE];
int wrap;
int current;
} ProfBuff;
ProfBuff* buffer_create();
void buffer_free(ProfBuff* buffer);
void buffer_push(ProfBuff* buffer, const char show_char, GTimeVal *tstamp, int flags, int attrs, const char * const from, const char * const message);
int buffer_yield(ProfBuff* buffer, int line, ProfBuffEntry** list);
#endif

View File

@ -44,6 +44,7 @@ win_create(const char * const title, int cols, win_type_t type)
new_win->from = strdup(title);
new_win->win = newpad(PAD_SIZE, cols);
wbkgd(new_win->win, COLOUR_TEXT);
new_win->buffer = buffer_create();
new_win->y_pos = 0;
new_win->x_pos = 0;
new_win->paged = 0;
@ -60,6 +61,7 @@ win_create(const char * const title, int cols, win_type_t type)
void
win_free(ProfWin* window)
{
buffer_free(window->buffer);
delwin(window->win);
free(window->from);
free(window);
@ -357,7 +359,7 @@ void win_save_print(ProfWin *window, const char show_char, GTimeVal *tstamp, int
date_fmt = g_date_time_format(time, "%H:%M:%S");
}
g_date_time_unref(time);
// buffer_push(window->buffer, time, from, message, attrs);
buffer_push(window->buffer, show_char, tstamp, flags, attrs, from, message);
if((flags & 2) == 0) {
wattron(window->win, COLOUR_TIME);
wprintw(window->win, "%s %c ", date_fmt, show_char);

View File

@ -32,6 +32,7 @@
#endif
#include "contact.h"
#include "ui/buffer.h"
#define NO_ME 1
#define NO_EOL 4
@ -53,6 +54,7 @@ typedef enum {
typedef struct prof_win_t {
char *from;
WINDOW *win;
ProfBuff *buffer;
win_type_t type;
gboolean is_otr;
gboolean is_trusted;