~rcr/rirc

ref: 3e478875afa86a8af3832419bddbd019ed7d91dd rirc/src/components/buffer.h -rw-r--r-- 2.3 KiB
3e478875 — Richard Robbins refactor namespacing of includes 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef RIRC_COMPONENTS_BUFFER_H
#define RIRC_COMPONENTS_BUFFER_H

#include <time.h>

#define TEXT_LENGTH_MAX 510 /* FIXME: remove max lengths in favour of growable buffer */
#define FROM_LENGTH_MAX 100

#ifndef BUFFER_LINES_MAX
#define BUFFER_LINES_MAX (1 << 10)
#endif

/* Buffer line types, in order of precedence */
enum buffer_line_t
{
	BUFFER_LINE_OTHER,        /* Default/all other lines */
	BUFFER_LINE_SERVER_INFO,  /* Server info message */
	BUFFER_LINE_SERVER_ERROR, /* Server error message */
	BUFFER_LINE_JOIN,         /* Irc JOIN message */
	BUFFER_LINE_NICK,         /* Irc NICK message */
	BUFFER_LINE_PART,         /* Irc PART message */
	BUFFER_LINE_QUIT,         /* Irc QUIT message */
	BUFFER_LINE_CHAT,         /* Line of text from another IRC user */
	BUFFER_LINE_PINGED,       /* Line of text from another IRC user containing current nick */
	BUFFER_LINE_T_SIZE
};

struct buffer_line
{
	enum buffer_line_t type;
	char prefix; /* TODO as part of `from` */
	char from[FROM_LENGTH_MAX + 1]; /* TODO: from/text as struct string */
	char text[TEXT_LENGTH_MAX + 1];
	size_t from_len;
	size_t text_len;
	time_t time;
	struct {
		unsigned int colour; /* Cached colour of `from` text */
		unsigned int rows;   /* Cached number of rows occupied when wrapping on w columns */
		unsigned int w;      /* Cached width for rows */
		unsigned int initialized : 1;
	} cached;
};

struct buffer
{
	unsigned int head;
	unsigned int tail;
	unsigned int scrollback; /* Index of the current line between [tail, head) for scrollback */
	size_t pad;              /* Pad 'from' when printing to be at least this wide */
	struct buffer_line buffer_lines[BUFFER_LINES_MAX];
};

float buffer_scrollback_status(struct buffer*);

int buffer_page_back(struct buffer*, unsigned int, unsigned int);
int buffer_page_forw(struct buffer*, unsigned int, unsigned int);

unsigned int buffer_line_rows(struct buffer_line*, unsigned int);

void buffer(struct buffer*);

struct buffer_line* buffer_head(struct buffer*);
struct buffer_line* buffer_tail(struct buffer*);
struct buffer_line* buffer_line(struct buffer*, unsigned int);

void buffer_newline(
	struct buffer*,
	enum buffer_line_t,
	const char*,
	const char*,
	size_t,
	size_t,
	char);

void
buffer_line_split(
	struct buffer_line *line,
	unsigned *head_w,
	unsigned *text_w,
	unsigned cols,
	unsigned pad);

#endif