-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsg2.c
229 lines (189 loc) · 4.83 KB
/
psg2.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright Ted Meyer 2015
*
* see LICENSE for details
*
*/
#define _DEFAULT_SOURCE
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "json.h"
#include "charstream.h"
#define SOLID_RIGHT ""
#define LINE_RIGHT ""
#define SOLID_LEFT ""
#define LINE_LEFT ""
#define bool unsigned char
#define true 1
#define false 0
typedef
struct psg {
char *parsed;
json *conf;
struct psg *next;
} psg;
static dmap *map = NULL;
static bool colors = true;
char *matchreplace(char *str, json *tofrom) {
if (!tofrom || !(tofrom->obj)) {
return str;
}
json *j_to = get(tofrom->obj, "to");
json *j_from = get(tofrom->obj, "from");
if (!j_to || !j_from) {
return str;
}
char *to = j_to->string;
char *from = j_from->string;
if (!to || !from) {
return str;
}
int maxsize = strlen(str) * strlen(to) / strlen(from);
char *res = calloc(sizeof(char), maxsize);
char *tmp = res, *del = NULL;
while((del=strstr(str, from))) {
del[0] = 0;
tmp += sprintf(tmp, "%s%s", str, to);
str = del+1;
}
sprintf(tmp, "%s", str);
return res;
}
void print_powerline_full(char *str, json *conf, json *next) {
if (!str) {
return;
}
json *forground = get(conf->obj, "forground");
json *background = get(conf->obj, "background");
if (colors) {
printf("\\[\\033[38;5;%im\\]", forground->number);
printf("\\[\\033[48;5;%im\\]", background->number);
}
printf(" %s ", str);
if (colors) {
printf("\\[\\033[38;5;%im\\]", background->number);
if (next) {
json *nb = get(next->obj, "background");
printf("\\[\\e[48;5;%im\\]", nb->number);
} else {
printf("\\[\\e[0m\\]");
printf("\\[\\e[38;5;%im\\]", background->number);
}
}
printf(SOLID_RIGHT);
}
void print_powerline_light(char *str, json *conf) {
if (!str) {
return;
}
json *forground = get(conf->obj, "background");
if (colors) {
printf("\\[\\033[38;5;%im\\]", forground->number);
}
printf(" %s ", str);
printf(LINE_RIGHT);
}
char *optsprocess(char *src, json *opts) {
if (!opts) {
return src;
}
char *name;
json *nxt;
map_each(opts->obj, name, nxt) {
if (!strcmp("match", name)) {
src = matchreplace(src, nxt);
}
}
return src;
}
void parse_psg(psg *ps) {
while(ps) {
json *src = get(ps->conf->obj, "name");
json *val = get(ps->conf->obj, "default");
char *srcval = src?src->string:NULL;
char *defval = val?val->string:NULL;
ps->parsed = defval;
char *override = get(map, srcval);
if (override) {
ps->parsed = override;
}
ps->parsed = optsprocess(ps->parsed, get(ps->conf->obj, "options"));
if (ps->parsed && strlen(ps->parsed) == 0) {
ps->parsed = NULL;
}
ps = ps->next;
}
}
void print_psg(psg *ps, json *style) {
json *powerline = get(style->obj, "powerline");
char *pltype = powerline->string;
if (!strncmp(pltype, "full", 4)) {
while(ps) {
print_powerline_full(ps->parsed
,ps->conf
,ps->next&&ps->next->parsed?ps->next->conf:NULL);
ps = ps->next;
}
} else
if (!strncmp(pltype, "minimal", 7)) {
while(ps) {
print_powerline_light(ps->parsed, ps->conf);
ps = ps->next;
}
printf(" ");
}
}
void parse_arg(dmap *map, char *arg) {
char *eq = strstr(arg, "=");
if (!eq) {
fprintf(stderr, "error on [%s]; args are in form [name]=[value]", arg);
exit(1);
}
eq[0] = 0;
put(map, arg, eq+1);
}
void parse_args(dmap *map, int pl, char **argv) {
if (!strcmp(argv[pl], "--no-colors")) {
colors = false;
} else {
parse_arg(map, argv[pl]);
}
}
int main(int argc, char **argv) {
json *each, *j = read_json(streamfile(stdin));
json *elements = get(j->obj, "elements");
json *style = get(j->obj, "style");
if (!elements || !style) {
puts("cannot find a style or elements block in json");
return 1;
}
psg *head = NULL;
psg *tail = NULL;
jarray_each(elements, each) {
psg *p = calloc(sizeof(psg), 1);
p->next = NULL;
p->conf = each;
if (tail) {
tail->next = p;
tail = p;
}
if (!head) {
head = p;
tail = p;
}
}
map = map_new();
for(int i=1; i<argc; i++) {
parse_args(map, i, argv);
}
parse_psg(head);
print_psg(head, style);
if (colors) {
printf("\\[\\e[0m\\]");
} else {
putchar('\n');
}
}