regexghost-dotfiles

My dotfiles and scripts
git clone git@git.regexghost.com/regexghost-dotfiles.git
Log | Files | Refs | README

directory_bookmarks.c (9633B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <stdbool.h>
      4 #include <string.h>
      5 #include <unistd.h>
      6 #include <linux/limits.h>
      7 
      8 #define BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/directory_bookmarks.txt"
      9 #define FILE_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/file_bookmarks.txt"
     10 #define RANGER_BOOKMARK_FILE_LOCATION "/.local/share/regexghost/script_data/ranger_directory_bookmarks.conf"
     11 
     12 // This script is based on apparix: https://github.com/micans/apparix
     13 
     14 static char file_loc[256];
     15 static char file_bookmarks_file_loc[256];
     16 static char ranger_file_loc[256];
     17 
     18 typedef struct {
     19 	char bookmark_name[11];
     20 	char directory_name[251];
     21 } Bookmark;
     22 
     23 typedef struct {
     24 	char bookmark_name[11];
     25 	char file_path[251];
     26 } FileBookmark;
     27 
     28 void print_help() {
     29 	printf("Usage:\n");
     30 	printf("  Add New Bookmark: bookmarks add *name*\n");
     31 	printf("  Remove Bookmark: bookmarks remove *name*\n");
     32 	printf("  Get Bookmark: bookmarks get *name*\n");
     33 	printf("  List Bookmarks: bookmarks list\n");
     34 	printf("  Get Bookmark Name of Current Directory: bookmarks current\n");
     35 	printf("Usage (File Bookmarks):\n");
     36 	printf("  Add New File Bookmark: bookmarks file add *name* *filename*\n");
     37 	printf("  Remove Bookmark: bookmarks file remove *name*\n");
     38 	printf("  Get Bookmark: bookmarks file get *name*\n");
     39 	printf("  List Bookmarks: bookmarks file list\n");
     40 }
     41 
     42 int get_bookmarks(Bookmark* bookmarks) {
     43 	FILE* f;
     44 	f = fopen(file_loc, "r");
     45 	if (f == NULL) {
     46 		return 0;
     47 	}
     48 
     49 	int i = 0;
     50 	char buffer[400];
     51 	
     52 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
     53 		if (i > 99) {
     54 			printf("Error, bookmarks file too big\n");
     55 			break;
     56 		}
     57 
     58 		char bookmark_name[11] = {0};
     59 		char directory_name[251] = {0};
     60 		sscanf(buffer, "%s %[^\n]", bookmark_name, directory_name);
     61 		strcpy(bookmarks[i].bookmark_name, bookmark_name);
     62 		strcpy(bookmarks[i].directory_name, directory_name);
     63 
     64 		memset(buffer, 0, sizeof(buffer));
     65 
     66 		i++;
     67 	}
     68 	return i;
     69 }
     70 
     71 int get_file_bookmarks(FileBookmark* bookmarks) {
     72 	FILE* f;
     73 	f = fopen(file_bookmarks_file_loc, "r");
     74 	if (f == NULL) {
     75 		return 0;
     76 	}
     77 
     78 	int i = 0;
     79 	char buffer[400];
     80 	
     81 	while (fgets(buffer, sizeof(buffer), f) != NULL) {
     82 		if (i > 99) {
     83 			printf("Error, file bookmarks file too big\n");
     84 			break;
     85 		}
     86 
     87 		char bookmark_name[11] = {0};
     88 		char file_path[251] = {0};
     89 		sscanf(buffer, "%s %[^\n]", bookmark_name, file_path);
     90 		strcpy(bookmarks[i].bookmark_name, bookmark_name);
     91 		strcpy(bookmarks[i].file_path, file_path);
     92 
     93 		memset(buffer, 0, sizeof(buffer));
     94 
     95 		i++;
     96 	}
     97 	return i;
     98 }
     99 
    100 void write_bookmarks(Bookmark* bookmarks, int num_bookmarks) {
    101 	FILE* f;
    102 	f = fopen(file_loc, "w");
    103 	for (int i=0; i<num_bookmarks; i++) {
    104 		fprintf(f, "%s %s\n", bookmarks[i].bookmark_name, bookmarks[i].directory_name);
    105 	}
    106 	fclose(f);
    107 
    108 	FILE* g;
    109 	g = fopen(ranger_file_loc, "w");
    110 	for (int i=0; i<num_bookmarks; i++) {
    111 		fprintf(g, "map go%s cd %s\n", bookmarks[i].bookmark_name, bookmarks[i].directory_name);
    112 	}
    113 	fclose(g);
    114 }
    115 
    116 void write_file_bookmarks(FileBookmark* bookmarks, int num_bookmarks) {
    117 	FILE* f;
    118 	f = fopen(file_bookmarks_file_loc, "w");
    119 	for (int i=0; i<num_bookmarks; i++) {
    120 		fprintf(f, "%s %s\n", bookmarks[i].bookmark_name, bookmarks[i].file_path);
    121 	}
    122 	fclose(f);
    123 }
    124 
    125 void add_file_bookmark(char* bookmark_name, char* filename) {
    126 	FileBookmark bookmarks[100];
    127 	memset(bookmarks, 0, sizeof(bookmarks));
    128 	int num_bookmarks = get_file_bookmarks(bookmarks);
    129 
    130 	if (num_bookmarks > 99) {
    131 		printf("Error, file bookmarks full\n");
    132 	}
    133 
    134 	char cwd[PATH_MAX] = {0};
    135 	if (getcwd(cwd, sizeof(cwd)) == NULL) {
    136 		printf("Error, unable to get current directory\n");
    137 		return;
    138 	}
    139 
    140 	strcpy(bookmarks[num_bookmarks].bookmark_name, bookmark_name);
    141 	strcpy(bookmarks[num_bookmarks].file_path, cwd);
    142 	strcpy(bookmarks[num_bookmarks].file_path, filename);
    143 	strcat(strcat(strcpy(bookmarks[num_bookmarks].file_path, cwd), "/"), filename);
    144 
    145 
    146 	num_bookmarks++;
    147 	write_file_bookmarks(bookmarks, num_bookmarks);
    148 }
    149 
    150 void add_bookmark(char* to_add) {
    151 	Bookmark bookmarks[100];
    152 	memset(bookmarks, 0, sizeof(bookmarks));
    153 	int num_bookmarks = get_bookmarks(bookmarks);
    154 
    155 	if (num_bookmarks > 99) {
    156 		printf("Error, bookmarks full\n");
    157 	}
    158 
    159 	char cwd[PATH_MAX] = {0};
    160 	if (getcwd(cwd, sizeof(cwd)) == NULL) {
    161 		printf("Error, unable to get current directory\n");
    162 		return;
    163 	}
    164 
    165 	strcpy(bookmarks[num_bookmarks].bookmark_name, to_add);
    166 	strcpy(bookmarks[num_bookmarks].directory_name, cwd);
    167 	
    168 	num_bookmarks++;
    169 	write_bookmarks(bookmarks, num_bookmarks);
    170 }
    171 
    172 void resolve_bookmark(char* to_resolve) {
    173 	Bookmark bookmarks[100];
    174 	memset(bookmarks, 0, sizeof(bookmarks));
    175 	int num_bookmarks = get_bookmarks(bookmarks);
    176 
    177 	for (int i=0; i<num_bookmarks; i++) {
    178 		if (!strcmp(bookmarks[i].bookmark_name, to_resolve)) {
    179 			printf("%s\n", bookmarks[i].directory_name);
    180 			return;
    181 		}
    182 	}
    183 }
    184 
    185 void resolve_file_bookmark(char* to_resolve) {
    186 	FileBookmark bookmarks[100];
    187 	memset(bookmarks, 0, sizeof(bookmarks));
    188 	int num_bookmarks = get_file_bookmarks(bookmarks);
    189 
    190 	for (int i=0; i<num_bookmarks; i++) {
    191 		if (!strcmp(bookmarks[i].bookmark_name, to_resolve)) {
    192 			printf("%s\n", bookmarks[i].file_path);
    193 			return;
    194 		}
    195 	}
    196 }
    197 
    198 void bookmark_for_current_dir() {
    199 	Bookmark bookmarks[100];
    200 	memset(bookmarks, 0, sizeof(bookmarks));
    201 	int num_bookmarks = get_bookmarks(bookmarks);
    202 
    203 	char cwd[PATH_MAX] = {0};
    204 	if (getcwd(cwd, sizeof(cwd)) == NULL) {
    205 		printf("Error, unable to get current directory\n");
    206 		return;
    207 	}
    208 
    209 	for (int i=0; i<num_bookmarks; i++) {
    210 		if (!strcmp(bookmarks[i].directory_name, cwd)) {
    211 			printf("-(%s)", bookmarks[i].bookmark_name);
    212 			return;
    213 		}
    214 	}
    215 }
    216 
    217 void list_bookmarks() {
    218 	Bookmark bookmarks[100];
    219 	memset(bookmarks, 0, sizeof(bookmarks));
    220 	int num_bookmarks = get_bookmarks(bookmarks);
    221 
    222 	for (int i=0; i<num_bookmarks; i++) {
    223 		printf("%s: %s\n", bookmarks[i].bookmark_name, bookmarks[i].directory_name);
    224 	}
    225 }
    226 
    227 void list_file_bookmarks() {
    228 	FileBookmark bookmarks[100];
    229 	memset(bookmarks, 0, sizeof(bookmarks));
    230 	int num_bookmarks = get_file_bookmarks(bookmarks);
    231 
    232 	for (int i=0; i<num_bookmarks; i++) {
    233 		printf("%s: %s\n", bookmarks[i].bookmark_name, bookmarks[i].file_path);
    234 	}
    235 }
    236 
    237 void remove_bookmark(char* to_remove) {
    238 	Bookmark bookmarks[100];
    239 	memset(bookmarks, 0, sizeof(bookmarks));
    240 	int num_bookmarks = get_bookmarks(bookmarks);
    241 
    242 	bool move_backwards = false;
    243 
    244 	for (int i=0; i<num_bookmarks; i++) {
    245 		if (move_backwards) {
    246 			memset(bookmarks[i-1].bookmark_name, 0, sizeof(bookmarks[i-1].bookmark_name));
    247 			memset(bookmarks[i-1].directory_name, 0, sizeof(bookmarks[i-1].directory_name));
    248 			strcpy(bookmarks[i-1].bookmark_name, bookmarks[i].bookmark_name);
    249 			strcpy(bookmarks[i-1].directory_name, bookmarks[i].directory_name);
    250 		}
    251 		else {
    252 			if (!strcmp(bookmarks[i].bookmark_name, to_remove)) {
    253 				move_backwards = true;
    254 			}
    255 		}
    256 	}
    257 	if (move_backwards) {
    258 		num_bookmarks--;
    259 	}
    260 	write_bookmarks(bookmarks, num_bookmarks);
    261 }
    262 
    263 void remove_file_bookmark(char* to_remove) {
    264 	FileBookmark bookmarks[100];
    265 	memset(bookmarks, 0, sizeof(bookmarks));
    266 	int num_bookmarks = get_file_bookmarks(bookmarks);
    267 
    268 	bool move_backwards = false;
    269 
    270 	for (int i=0; i<num_bookmarks; i++) {
    271 		if (move_backwards) {
    272 			memset(bookmarks[i-1].bookmark_name, 0, sizeof(bookmarks[i-1].bookmark_name));
    273 			memset(bookmarks[i-1].file_path, 0, sizeof(bookmarks[i-1].file_path));
    274 			strcpy(bookmarks[i-1].bookmark_name, bookmarks[i].bookmark_name);
    275 			strcpy(bookmarks[i-1].file_path, bookmarks[i].file_path);
    276 		}
    277 		else {
    278 			if (!strcmp(bookmarks[i].bookmark_name, to_remove)) {
    279 				move_backwards = true;
    280 			}
    281 		}
    282 	}
    283 	if (move_backwards) {
    284 		num_bookmarks--;
    285 	}
    286 	write_file_bookmarks(bookmarks, num_bookmarks);
    287 }
    288 
    289 int main(int argc, char* argv[]) {
    290 	if (argc == 1) {
    291 		print_help();
    292 		return 1;
    293 	}
    294 
    295 	memset(file_loc, 0, 256);
    296 	memset(ranger_file_loc, 0, 256);
    297 	strcat(strcpy(file_loc, getenv("HOME")), BOOKMARK_FILE_LOCATION);
    298 	strcat(strcpy(file_bookmarks_file_loc, getenv("HOME")), FILE_BOOKMARK_FILE_LOCATION);
    299 	strcat(strcpy(ranger_file_loc, getenv("HOME")), RANGER_BOOKMARK_FILE_LOCATION);
    300 
    301 	// Add a new bookmark for current directory
    302 	if (!strcmp(argv[1], "file")) {
    303 		if (!strcmp(argv[2], "add")) {
    304 				if (argc != 5) {
    305 					print_help();
    306 					return 1;
    307 				}
    308 				char* to_add = argv[3];
    309 				char* file_name = argv[4];
    310 				add_file_bookmark(to_add, file_name);
    311 			}
    312 		// Get a bookmark
    313 			else if (!strcmp(argv[2], "get")) {
    314 				if (argc != 4) {
    315 					print_help();
    316 					return 1;
    317 				}
    318 				char* to_resolve = argv[3];
    319 				resolve_file_bookmark(to_resolve);
    320 			}
    321 		// List all file bookmarks
    322 			else if (!strcmp(argv[2], "list")) {
    323 				list_file_bookmarks();
    324 			}
    325 		// Remove a bookmark by name
    326 			else if (!strcmp(argv[2], "remove")) {
    327 				if (argc != 4) {
    328 					print_help();
    329 					return 1;
    330 				}
    331 				char* to_remove = argv[3];
    332 				remove_file_bookmark(to_remove);
    333 			}
    334 			else {
    335 				print_help();
    336 				return 1;
    337 			}
    338 	} else {
    339 		if (!strcmp(argv[1], "add")) {
    340 			if (argc != 3) {
    341 				print_help();
    342 				return 1;
    343 			}
    344 			char* to_add = argv[2];
    345 			add_bookmark(to_add);
    346 		}
    347 		// Get a bookmark
    348 		else if (!strcmp(argv[1], "get")) {
    349 			if (argc != 3) {
    350 				print_help();
    351 				return 1;
    352 			}
    353 			char* to_resolve = argv[2];
    354 			resolve_bookmark(to_resolve);
    355 		}
    356 		// Print the current directory's bookmark, if it exists
    357 		else if (!strcmp(argv[1], "current")) {
    358 			bookmark_for_current_dir();
    359 		}
    360 		// List all bookmarks
    361 		else if (!strcmp(argv[1], "list")) {
    362 			list_bookmarks();
    363 		}
    364 		// Remove a bookmark by name
    365 		else if (!strcmp(argv[1], "remove")) {
    366 			if (argc != 3) {
    367 				print_help();
    368 				return 1;
    369 			}
    370 			char* to_remove = argv[2];
    371 			remove_bookmark(to_remove);
    372 		}
    373 		else {
    374 			print_help();
    375 			return 1;
    376 		}
    377 	}
    378 }