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