-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkfc.c
236 lines (198 loc) · 5.36 KB
/
kfc.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
229
230
231
232
233
234
235
236
/*
* SPDX-License-Identifier: MIT
* Copyright (C) 2020-2023 Michael Czigler
*/
#define _POSIX_C_SOURCE 200809L
#include <dirent.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define SEQUENCE "printf \"\
\\033]4;0;#$(echo $color00)\\033\\ \
\\033]4;1;#$(echo $color01)\\033\\ \
\\033]4;2;#$(echo $color02)\\033\\ \
\\033]4;3;#$(echo $color03)\\033\\ \
\\033]4;4;#$(echo $color04)\\033\\ \
\\033]4;5;#$(echo $color05)\\033\\ \
\\033]4;6;#$(echo $color06)\\033\\ \
\\033]4;7;#$(echo $color07)\\033\\ \
\\033]4;8;#$(echo $color08)\\033\\ \
\\033]4;9;#$(echo $color09)\\033\\ \
\\033]4;10;#$(echo $color10)\\033\\ \
\\033]4;11;#$(echo $color11)\\033\\ \
\\033]4;12;#$(echo $color12)\\033\\ \
\\033]4;13;#$(echo $color13)\\033\\ \
\\033]4;14;#$(echo $color14)\\033\\ \
\\033]4;15;#$(echo $color15)\\033\\ \
\\033]10;#$(echo $foreground)\\033\\ \
\\033]11;#$(echo $background)\\033\\ \
\\033]12;#$(echo $cursor)\\033\\ \
\\033[21D\""
#define PALETTE "\
\033[48;5;0m \033[0m\
\033[48;5;1m \033[0m\
\033[48;5;2m \033[0m\
\033[48;5;3m \033[0m\
\033[48;5;4m \033[0m\
\033[48;5;5m \033[0m\
\033[48;5;6m \033[0m\
\033[48;5;7m \033[0m\n\
\033[48;5;8m \033[0m\
\033[48;5;9m \033[0m\
\033[48;5;10m \033[0m\
\033[48;5;11m \033[0m\
\033[48;5;12m \033[0m\
\033[48;5;13m \033[0m\
\033[48;5;14m \033[0m\
\033[48;5;15m \033[0m\n"
static char *seq; /* path to palette source directory */
static char *conf; /* path to configuration file */
static char *mode = "dark"; /* selected mode string */
static char *sval = NULL; /* selected palette string */
static void
find_palettes(void) {
if (access("/usr/share/kfc/palettes", F_OK) == 0) {
seq = "/usr/share/kfc/palettes";
}
else if (access("palettes", F_OK) == 0) {
seq = "palettes";
}
else {
fprintf(stderr, "Palette source directory not found\n");
exit(1);
}
}
static void
select_palette(char *sel) {
char *evar, *eval, *line;
FILE *fp = fopen(sel, "r");
size_t len = 255;
line = malloc(sizeof(char) * len);
if (fp == NULL) {
fprintf(stderr, "Selected palette does not exist\n");
exit(1);
}
while (fgets(line, len, fp) != NULL) {
evar = strtok(line, "=");
eval = strtok(NULL, "=");
setenv(evar, eval, 1);
}
fclose(fp);
free(line);
fp = fopen(conf, "w");
fprintf(fp, "%s", sel);
fclose(fp);
fp = popen(SEQUENCE, "w");
pclose(fp);
}
static void
list_palette(char *sel) {
struct dirent **de;
int n = scandir(sel, &de, NULL, alphasort);
while (n--) {
if (de[n]->d_type == 8) printf("%s/%s\n", mode, de[n]->d_name);
}
free(de);
}
static void
random_palette(char *sel) {
struct dirent **de;
int n = scandir(sel, &de, NULL, alphasort);
srand(time(0));
int i = (rand() % (n - 0 + 1)) + 0;
while (n--) {
if (i == n) sval = de[n]->d_name;
}
free(de);
}
static void
print_palette(void) {
FILE *fp = fopen(conf, "r");
size_t len = 255;
char *line;
line = malloc(sizeof(char) * len);
if (fp == NULL) {
fprintf(stderr, "No palette set or config file missing\n");
exit(1);
}
fgets(line, len, fp);
puts(line);
fclose(fp);
free(line);
printf(PALETTE);
}
int
main(int argc, char **argv) {
int cval;
int rflag = 0, lflag = 0, pflag = 0;
size_t len;
char *sel, *tmp;
if (argc == 1) {
fprintf(stderr, "No argument(s) provided\n");
return 1;
}
tmp = getenv("XDG_CONFIG_HOME");
if (tmp != NULL) {
len = strlen(tmp) + sizeof("/kfc/current");
conf = malloc(sizeof(char) * len);
snprintf(conf, len, "%s", tmp);
}
else {
puts("XDG_CONFIG_HOME is not defined. Defaulting to HOME/.config");
tmp = getenv("HOME");
len = strlen(tmp) + sizeof("/.config/kfc/current");
conf = malloc(sizeof(char) * len);
snprintf(conf, len, "%s/.config", tmp);
}
strcat(conf, "/kfc");
if (mkdir(conf, 0777) == 0) {
puts("Created 'kfc' directory in XDG_CONFIG_HOME");
}
strcat(conf, "/current");
find_palettes();
while ((cval = getopt(argc, argv, "rlLpvs:" )) != -1) {
switch (cval) {
case 'v': puts("kfc 0.1.4"); break;
case 'L': mode = "light"; break;
case 'l': lflag++; break;
case 'r': rflag++; break;
case 'p': pflag++; break;
case 's':
if (rflag) {
fprintf(stderr, "Cannot specify -r with -s\n");
return 1;
}
sval = optarg;
break;
case '?': return 1;
}
}
len = strlen(seq)
+ sizeof("/")
+ strlen(mode)
+ (sval ? strlen(sval) + 1 : 0);
sel = malloc(sizeof(char) * len);
if (lflag) {
snprintf(sel, len, "%s/%s", seq, mode);
list_palette(sel);
}
if (rflag) {
snprintf(sel, len, "%s/%s", seq, mode);
random_palette(sel);
len = strlen(sel)
+ sizeof("/")
+ strlen(sval);
sel = (char *)realloc(sel, sizeof(char) * len);
}
if (sval) {
snprintf(sel, len, "%s/%s/%s", seq, mode, sval);
select_palette(sel);
}
if (pflag) print_palette();
free(sel);
return 0;
}