-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.c
184 lines (169 loc) · 3.57 KB
/
segment.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
/**
* Copyright (c) 2019-2020 Maksymilian Mruszczak
* pwrln generic segment functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pwrln.h"
#include "config.h"
enum Shell {
SH_GENERIC,
SH_BASH,
SH_KSH,
SH_ZSH,
SH_RC
};
static enum Shell target_shell;
static char esc_delim[2][3];
static void print_version(void);
void
set_target_shell(const char *name)
{
char buf[TMPSIZ], *word, *sh_name;
strlcpy(buf, name, TMPSIZ);
for (word = strtok(buf, "-"); word; word = strtok(NULL, "-"))
sh_name = word;
if (!strncmp(sh_name, "bash", TMPSIZ)) {
target_shell = SH_BASH;
esc_delim[0][0] = 0x01;
esc_delim[1][0] = 0x02;
esc_delim[0][1] = 0x00;
esc_delim[1][1] = 0x00;
} else if (!strncmp(sh_name, "ksh", TMPSIZ)) {
target_shell = SH_KSH;
esc_delim[0][0] = 0x01;
esc_delim[1][0] = 0x01;
esc_delim[0][1] = 0x00;
esc_delim[1][1] = 0x00;
} else if (!strncmp(sh_name, "zsh", TMPSIZ)) {
target_shell = SH_ZSH;
esc_delim[0][0] = '%';
esc_delim[1][0] = '%';
esc_delim[0][1] = '{';
esc_delim[1][1] = '}';
esc_delim[0][2] = 0x00;
esc_delim[1][2] = 0x00;
} else if (!strncmp(sh_name, "rc", TMPSIZ)) {
target_shell = SH_RC;
esc_delim[0][0] = 0x01;
esc_delim[1][0] = 0x02;
esc_delim[0][1] = 0x00;
esc_delim[1][1] = 0x00;
} else {
target_shell = SH_GENERIC;
esc_delim[0][0] = 0x00;
esc_delim[1][0] = 0x00;
}
}
int /* only version param for now */
parse_arg(const char *arg)
{
if (arg[0] != '-')
return 0;
switch (arg[1]) {
case '-':
if (!strcmp(arg, "--version")) {
print_version();
return 1;
}
break;
case 'v':
if (arg[2] == '\0') {
print_version();
return 1;
}
break;
}
return 0;
}
static void
print_version(void)
{
Segment *v = new("PWR", 226, 233);
v->next = new("LN \xe2\x9a\xa1", 233, 226);
v->next->next = new(VERSION, 7, 236);
print(v);
putchar('\n');
prune(v);
}
Segment *
new(const char *content, unsigned char bg, unsigned char fg)
{
Segment *s = (Segment *)malloc(sizeof(Segment));
if (!s)
fail("segment init error");
s->bg = bg;
s->fg = fg;
s->bold = 0;
s->next = NULL;
s->content = (char *)malloc(strlen(content)+sizeof(char));
if (!s->content)
fail("segment content buffer init error");
strlcpy(s->content, content, TMPSIZ);
return s;
}
Segment *
tail(Segment *s)
{
while (s->next != NULL)
s = s->next;
return s;
}
void
prune(Segment *s)
{
Segment *n;
while (s != NULL) {
n = s->next;
free(s->content);
free(s);
s = n;
}
}
char *
render(Segment *s)
{
char tmp[TMPSIZ], *buf = (char *)malloc(sizeof(char)*512);
if (!buf)
fail("segment render buffer allocation error");
if (s->bold)
snprintf(buf, TMPSIZ, "%s%c[1m%s", esc_delim[0], esc, esc_delim[1]);
else
buf[0] = '\0';
snprintf(tmp, TMPSIZ, "%s%c[48;5;%d;38;5;%dm%s %s ",
esc_delim[0], esc, s->bg, s->fg, esc_delim[1], s->content);
strlcat(buf, tmp, TMPSIZ);
if (s->next != NULL)
snprintf(tmp, TMPSIZ, "%s%c[0;48;5;%d;38;5;%dm%s%s",
esc_delim[0], esc, s->next->bg, s->bg, esc_delim[1], glyph_delimiter);
else
snprintf(tmp, TMPSIZ, "%s%c[0;38;5;%dm%s%s%s%c[0m%s ",
esc_delim[0], esc, s->bg, esc_delim[1],
glyph_delimiter, esc_delim[0], esc, esc_delim[1]);
strlcat(buf, tmp, TMPSIZ);
return buf;
}
void
print(Segment *s)
{
char *tmp, buf[512];
buf[0] = '\0';
if (target_shell == SH_KSH)
printf("%s\r", esc_delim[0]);
while (s != NULL) {
tmp = render(s);
strlcat(buf, tmp, TMPSIZ);
s = s->next;
free(tmp);
}
printf("%s", buf);
}
void
fail(const char *msg)
{
fprintf(stderr, "pwrln failure: %s: ", msg);
perror(NULL);
printf("pwrln prompt failed> ");
exit(1);
}