-
-
Save halferty/c315f11f73c9c5d83bb078eeead83914 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ncurses.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#define MAX_BRANCH_NAME_LEN 4096 | |
#define MAX_BRANCHES 100 | |
#define COLOR_PAIR_HEADER 1 | |
#define COLOR_PAIR_LIST_NORMAL 2 | |
#define COLOR_PAIR_LIST_HIGHLIGHTED 3 | |
int main() { | |
int ch, done = 0; | |
int i = 0, numBranches, highlighted = 0; | |
char **gitBranchList = malloc(MAX_BRANCHES * sizeof(char *)); | |
int *branchesToDelete = malloc(MAX_BRANCHES * sizeof(int)); | |
char *line, *lineCpy = malloc(MAX_BRANCH_NAME_LEN), *tmpStr = malloc(4096); | |
size_t len = 0; | |
ssize_t lineSize = 0; | |
FILE *f; | |
f = fopen("/tmp/git_branches.tmp", "r"); | |
for (i = 0; i < MAX_BRANCHES; i++) { gitBranchList[i] = NULL; branchesToDelete[i] = 0; } | |
for (i = 0; i < MAX_BRANCHES; i++) { | |
line = fgetln(f, &len); | |
if (len == 0) { break; } | |
memcpy(lineCpy, line, len); | |
lineCpy[len] = NULL; | |
gitBranchList[i] = malloc(MAX_BRANCH_NAME_LEN); | |
sprintf(gitBranchList[i], "%s", lineCpy); | |
} | |
fclose(f); | |
initscr(); | |
cbreak(); | |
keypad(stdscr, TRUE); | |
noecho(); | |
start_color(); | |
init_pair(COLOR_PAIR_HEADER, COLOR_BLACK, COLOR_YELLOW); | |
init_pair(COLOR_PAIR_LIST_NORMAL, COLOR_BLUE, COLOR_WHITE); | |
init_pair(COLOR_PAIR_LIST_HIGHLIGHTED, COLOR_CYAN, COLOR_BLACK); | |
while (!done) { | |
clear(); | |
attron(COLOR_PAIR(COLOR_PAIR_HEADER)); | |
printw("Remove branches:\n"); | |
attroff(COLOR_PAIR(COLOR_PAIR_HEADER)); | |
for (i = 0; i < MAX_BRANCHES; i++) { | |
if (!gitBranchList[i]) { numBranches = i; break; } | |
attron(COLOR_PAIR(branchesToDelete[i] ? COLOR_PAIR_LIST_NORMAL : COLOR_PAIR_LIST_HIGHLIGHTED)); | |
printw("%c%c", gitBranchList[i][0], gitBranchList[i][1]); | |
attron(COLOR_PAIR(highlighted == i ? COLOR_PAIR_LIST_NORMAL : COLOR_PAIR_LIST_HIGHLIGHTED)); | |
printw(&(gitBranchList[i][2])); | |
} | |
ch = getch(); | |
switch (ch) { | |
case 'q': { | |
done = 1; | |
refresh(); | |
endwin(); | |
break; | |
} | |
case 'k': { | |
if (highlighted > 0) { highlighted--; } | |
break; | |
} | |
case KEY_UP: { | |
if (highlighted > 0) { highlighted--; } | |
break; | |
} | |
case 'j': { | |
if (highlighted < numBranches - 1) { highlighted++; } | |
break; | |
} | |
case KEY_DOWN: { | |
if (highlighted < numBranches - 1) { highlighted++; } | |
break; | |
} | |
case '\n': { | |
for (i = 0; i < MAX_BRANCHES; i++) { | |
if (!gitBranchList[i]) { break; } | |
if (branchesToDelete[i]) { | |
sprintf(tmpStr, "git branch -D %s\n", gitBranchList[i]); | |
system(tmpStr); | |
} | |
} | |
refresh(); | |
endwin(); | |
return 0; | |
break; | |
} | |
case ' ': { | |
branchesToDelete[highlighted] = !branchesToDelete[highlighted]; | |
break; | |
} | |
} | |
usleep(3000); | |
refresh(); | |
} | |
refresh(); | |
endwin(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
rm -f ~/c/bin/_removebranches.bin | |
gcc ~/c/bin/_removebranches.c -lncurses -o ~/c/bin/_removebranches.bin | |
git branch -v > /tmp/git_branches.tmp | |
~/c/bin/_removebranches.bin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment