summaryrefslogtreecommitdiff
path: root/src/common/common.c
blob: 755fe7b42088127cb653b4b61419fb8e8c9bce5d (plain)
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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "common.h"


static int linebuf_sane(GlobalData *g)
{
  return (g->linelen < g->linemax);
}


void line_clear(GlobalData *g)
{
  assert(g->linemax > 0);

  g->line[0] = '\0';
  g->linelen = 0;
  g->firstItemDone = 0;

  line_append_str(g, "[");
}


void line_append_strn(GlobalData *g, char *string, size_t len)
{
  assert(linebuf_sane(g));

  if (g->linemax <= g->linelen + len) {
    /* Buffer full. Tough luck. */
    return;
  }

  memcpy(&g->line[g->linelen], string, len);
  g->linelen += len;
  g->line[g->linelen] = '\0';
}


void line_append_str(GlobalData *g, char *string)
{
  line_append_strn(g, string, strlen(string));
}



void line_append_item(GlobalData *g, StatusItem *s)
{
  if (g->firstItemDone) {
    line_append_str(g, ",");
  }
  g->firstItemDone = 1;

  line_append_str(g, "{");

  if (s->color) {
    line_append_str(g, "\"color\":\"");
    line_append_str(g, s->color);
    line_append_str(g, "\",");
  }

  line_append_str(g, "\"full_text\":\"");
  assert(s->text);
  line_append_str(g, s->text);
  line_append_str(g, "\"");

  line_append_str(g, ",\"separator\":false");
  line_append_str(g, ",\"separator_block_width\":14");

  line_append_str(g, "}");
}


void line_print(GlobalData *g)
{
  line_append_str(g, "],");
  puts(g->line);
}


void statusitem_init(StatusItem *s)
{
  s->color = NULL;
  s->text = NULL;
}