Simple output gathering design
[sysstatus.git] / src / common / common.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5
6 #include "common.h"
7
8
9 static int linebuf_sane(GlobalData *g)
10 {
11   return (g->linelen < g->linemax);
12 }
13
14
15 void line_clear(GlobalData *g)
16 {
17   assert(g->linemax > 0);
18
19   g->line[0] = '\0';
20   g->linelen = 0;
21 }
22
23
24 void line_append_strn(GlobalData *g, char *string, size_t len)
25 {
26   assert(linebuf_sane(g));
27
28   if (g->linemax <= g->linelen + len) {
29     /* Buffer full. Tough luck. */
30     return;
31   }
32
33   memcpy(&g->line[g->linelen], string, len);
34   g->linelen += len;
35   g->line[g->linelen] = '\0';
36 }
37
38
39 void line_append_str(GlobalData *g, char *string)
40 {
41   line_append_strn(g, string, strlen(string));
42 }
43
44
45
46 void line_append_item(GlobalData *g, StatusItem *s)
47 {
48   line_append_str(g, " ");
49
50   if (s->color) {
51     line_append_str(g, "^fg(");
52     line_append_str(g, s->color);
53     line_append_str(g, ")");
54   }
55
56   if (s->text) {
57     line_append_str(g, s->text);
58   }
59
60   line_append_str(g, " ");
61 }
62
63
64 void line_print(GlobalData *g)
65 {
66   puts(g->line);
67 }
68
69
70 void statusitem_init(StatusItem *s)
71 {
72   s->color = NULL;
73   s->text = NULL;
74 }