-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctbuf.c
100 lines (90 loc) · 1.63 KB
/
ctbuf.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010-13 Edward Cree
See quirc.c for license information
ctbuf: coloured text buffers
*/
#include <stdio.h>
#include <stdlib.h>
#include "ctbuf.h"
void ct_init_char(ctchar **buf, size_t *l, size_t *i)
{
*l=80;
*buf=malloc(*l*sizeof(ctchar));
(*buf)[0].d=0;
*i=0;
}
void ct_append_char(ctchar **buf, size_t *l, size_t *i, char d)
{
ct_append_char_c(buf, l, i, (*buf&&*i)?(*buf)[(*i)-1].c:(colour){.fore=7,.back=0,.hi=false,.ul=false}, d);
}
void ct_append_char_c(ctchar **buf, size_t *l, size_t *i, colour c, char d)
{
if(*buf)
{
(*buf)[*i]=(ctchar){.c=c, .d=d};
(*i)++;
}
else
{
ct_init_char(buf, l, i);
ct_append_char_c(buf, l, i, c, d);
}
ctchar *nbuf=*buf;
if((*i)>=(*l))
{
*l=*i*2;
nbuf=realloc(*buf, *l*sizeof(ctchar));
}
if(nbuf)
{
*buf=nbuf;
(*buf)[*i].d=0;
}
else
{
free(*buf);
ct_init_char(buf, l, i);
}
}
void ct_append_str(ctchar **buf, size_t *l, size_t *i, const char *str)
{
while(str && *str)
{
ct_append_char(buf, l, i, *str++);
}
}
void ct_append_str_c(ctchar **buf, size_t *l, size_t *i, colour c, const char *str)
{
while(str && *str)
{
ct_append_char_c(buf, l, i, c, *str++);
}
}
void ct_putchar(ctchar a)
{
setcolour(a.c);
putchar(a.d);
}
void ct_puts(const ctchar *buf)
{
if(!buf) return;
size_t i=0;
while(buf[i].d)
{
if(!(i&&eq_colour(buf[i].c, buf[i-1].c)))
setcolour(buf[i].c);
putchar(buf[i++].d);
}
}
void ct_putsn(const ctchar *buf, size_t n)
{
if(!buf) return;
size_t i=0;
while((buf[i].d)&&(i<n))
{
if(!(i&&eq_colour(buf[i].c, buf[i-1].c)))
setcolour(buf[i].c);
putchar(buf[i++].d);
}
}