Use a bit of clang-format on git-prompt.c

This commit is contained in:
Kenneth Benzie 2024-04-08 20:33:26 +01:00
parent 7aa78e94d1
commit 1ca9e4f6ae

View File

@ -9,24 +9,24 @@
#endif
#ifdef DEBUG
#define check(CONDITION) \
if (CONDITION) { \
fprintf(stderr, "error: %s: %d: %s\n", __FILE__, __LINE__, #CONDITION); \
exit(0); \
#define check(CONDITION) \
if (CONDITION) { \
fprintf(stderr, "error: %s: %d: %s\n", __FILE__, __LINE__, #CONDITION); \
exit(0); \
}
#else
#define check(CONDITION) \
if (CONDITION) { \
exit(0); \
#define check(CONDITION) \
if (CONDITION) { \
exit(0); \
}
#endif
typedef struct process {
pid_t pid;
FILE* out;
FILE *out;
} process_t;
process_t process_open(char* command) {
process_t process_open(char *command) {
int fds[2];
check(pipe(fds));
int pid = fork();
@ -35,7 +35,7 @@ process_t process_open(char* command) {
close(fds[0]);
dup2(fds[1], STDOUT_FILENO);
dup2(STDOUT_FILENO, STDERR_FILENO);
char* argv[] = {"sh", "-c", command, NULL};
char *argv[] = {"sh", "-c", command, NULL};
exit(execvp(argv[0], argv));
} else { // parent process
close(fds[1]);
@ -54,9 +54,11 @@ int process_close(process_t process) {
return 0;
}
char* trim(char* str) {
char* end;
while (isspace((unsigned char)*str)) str++;
char *trim(char *str) {
char *end;
while (isspace((unsigned char)*str)) {
str++;
}
if (*str == 0) {
return str;
}
@ -68,17 +70,17 @@ char* trim(char* str) {
return str;
}
char* append(char* buffer, int count, ...) {
char *append(char *buffer, int count, ...) {
va_list list;
va_start(list, count);
for (int i = 0; i < count; i++) {
strcat(buffer, va_arg(list, char*));
strcat(buffer, va_arg(list, char *));
}
va_end(list);
return buffer;
}
char* inttostr(char* buffer, int value) {
char *inttostr(char *buffer, int value) {
sprintf(buffer, "%d", value);
return buffer;
}
@ -102,7 +104,7 @@ int main() {
check(process_close(process));
}
}
char* branch = trim(branch_buf);
char *branch = trim(branch_buf);
char prompt[1024] = {};
append(prompt, 3, " %{%F{66}%}", branch, "%{%f%}");
@ -113,7 +115,7 @@ int main() {
char remote_buf[256] = {};
fread(remote_buf, 1, sizeof(remote_buf), process.out);
if (process_close(process) == 0) {
char* remote = trim(remote_buf);
char *remote = trim(remote_buf);
// get the number of commits ahead of the remote
memset(command, 0, sizeof(command));
process = process_open(append(command, 5,
@ -121,7 +123,7 @@ int main() {
remote, "/", branch, "...HEAD --count"));
char count[32] = {};
fread(count, 1, sizeof(count), process.out);
if(process_close(process) == 0 && strcmp("0", trim(count))) {
if (process_close(process) == 0 && strcmp("0", trim(count))) {
append(prompt, 2, "", trim(count));
}
@ -132,7 +134,7 @@ int main() {
remote, "/", branch, "...HEAD --count"));
memset(count, 0, sizeof(count));
fread(count, 1, sizeof(count), process.out);
if(process_close(process) == 0 && strcmp("0", trim(count))) {
if (process_close(process) == 0 && strcmp("0", trim(count))) {
append(prompt, 2, "", trim(count));
}
}
@ -157,7 +159,7 @@ int main() {
case ' ':
switch (Y) {
case 'M': ++modified; break;
case 'D': ++deleted; break;
case 'D': ++deleted; break;
} break;
case 'D': ++indexed;
switch (Y) {
@ -168,14 +170,14 @@ int main() {
switch (Y) {
case ' ': break;
case 'M': ++modified; break;
case 'D': ++deleted; break;
case 'D': ++deleted; break;
} break;
}
}
}
check(process_close(process));
if (indexed || modified || deleted || unmerged || untracked) { // modified
if (indexed || modified || deleted || unmerged || untracked) { // modified
char int_buf[32];
if (indexed) {
append(prompt, 3, "%{%F{2}%}*", inttostr(int_buf, indexed), "%{%f%}");
@ -192,7 +194,7 @@ int main() {
if (untracked) {
append(prompt, 1, "%{%F{1}%}…%{%f%}");
}
} else { // clean
} else { // clean
append(prompt, 1, "%{%B%F{2}%}✓%{%f%b%}");
}