-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
131 lines (102 loc) · 2.67 KB
/
main.c
File metadata and controls
131 lines (102 loc) · 2.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VERSION "0.3"
#define DEFINE_GLOBALS
#include "powerlinec.h"
#include "segments.h"
const size_t color_names_size = sizeof(color_names);
SEGMENT create_segment(char* description);
void delete_segment(SEGMENT* segment);
void draw_segment(SEGMENT* segment);
int get_color_index(char* color);
int get_segment_index(char* segment);
void draw_separator(int background, int foreground);
int main(int argc, char* argv[]) {
if (1 >= argc) {
printf("ERROR: no arguments provided.\n");
exit(1);
}
if (0 == strcmp("--version", argv[1])) {
printf("powerlinec %s\n", VERSION);
exit(0);
}
SEGMENT old;
int drawn = 0;
int flag = 0;
for (int i = 1; i < argc; i++) {
SEGMENT current = create_segment(argv[i]);
if (NULL != current.text) {
if (flag >= 1) {
draw_separator(current.background, old.background - 10);
}
draw_segment(¤t);
drawn++;
old.foreground = current.foreground;
old.background = current.background;
flag = 1;
}
else {
flag = 0;
}
delete_segment(¤t);
}
if (0 < drawn) {
draw_separator(0, old.background - 10);
printf("\\[\x1b[0m\\] ");
}
return 0;
}
SEGMENT create_segment(char* description) {
char* copy = strdup(description);
int segment_index = -1, background, foreground;
char* token;
token = strtok(copy, ":");
if (NULL != token)
segment_index = get_segment_index(token);
token = strtok(NULL, ":");
if (NULL != token)
background = get_color_index(token) + 40;
token = strtok(NULL, ":");
if (NULL != token)
foreground = get_color_index(token) + 30;
SEGMENT segment = {.text = NULL, .background = background, .foreground = foreground};
if (segment_index >= 0) {
int res = (*available_segments[segment_index])(&segment);
}
return segment;
}
void delete_segment(SEGMENT* segment) {
if (NULL != segment->text)
free(segment->text);
segment->foreground = 0;
segment->background = 0;
}
void draw_segment(SEGMENT* segment) {
printf("\\[\x1b[%dm\\]", segment->background);
printf("\\[\x1b[%dm\\]", segment->foreground);
printf(" %s ", segment->text);
}
int get_color_index(char* color) {
size_t size = sizeof(color_names) / sizeof(*color_names);
for (int i = 0; i < size; i++) {
if (strcmp(color, color_names[i]) == 0) {
return i;
}
}
return 0;
}
int get_segment_index(char* segment) {
size_t size = sizeof(segment_names) / sizeof(*segment_names);
for (int i = 0; i < size; i++) {
if (strcmp(segment, segment_names[i]) == 0) {
return i;
}
}
return -1;
}
void draw_separator(int background, int foreground) {
printf("\\[\x1b[%dm\\]", background);
printf("\\[\x1b[%dm\\]", foreground);
printf("%s", icons[SEPARATOR]);
}