~fancycade/szc

3706cef79d377fdbee215187dcdee84b3f089a84 — Harley Swick 2 years ago 62118be
Add shortcut arguments
1 files changed, 213 insertions(+), 162 deletions(-)

M src/main.c
M src/main.c => src/main.c +213 -162
@@ 14,10 14,15 @@ int read_hub(char* hubpath);
void new_zettel(char* hubpath, char* title);
void new_hub(char* hubpath);
void list_hubs(char* zdir);
void user_interface(void);
void user_interface(char *zdir);
int scan_zettel(char* path, char* zettel);
void link_zettel(char* path, char* path2);
int match_id_to_path(char* path, char* id, char* hub);
void init_zdir(char* zdir);
void query_choice(char* zdir);
void hub_choice(char* zdir);
void zettel_choice(char* zdir);
void link_choice(char* zdir);

void
new_zettel(char* hubpath, char* title)


@@ 209,9 214,8 @@ link_zettel(char* path, char* path2)
}

void
user_interface(void)
init_zdir(char* zdir)
{
        char zdir[200];
        char* homedir = getenv("HOME");
        if (homedir == NULL)
                homedir = getpwuid(getuid())->pw_dir;


@@ 229,7 233,183 @@ user_interface(void)
                        printf("Initialized zettelkasten directory in $HOME\n");
                }
        }
}

void
query_choice(char* zdir)
{
	printf("Listing hubs:\n");
	list_hubs(zdir);
	printf("---\n");
	printf("Enter a hub:\n");

	char hub[50];
	fgets(hub, 50, stdin);
	hub[strcspn(hub, "\n")] = '\0';
	printf("\n");

	char hubpath[300];
	strcpy(hubpath, zdir);
	strcat(hubpath, hub);

	printf("Listing zettels in hub: %s\n", hub);
	if (read_hub(hubpath) != 0) {
		printf("Unable to find hub: %s\n", hub);
		return;
	}
	printf("----\n");
	printf("Enter the zettel id:\n");

	char zettel[10];
	fgets(zettel, 10, stdin);
	zettel[strcspn(zettel, "\n")] = '\0';
	printf("\n");

	char path[50];
	if (match_id_to_path(path, zettel, hubpath) != 0) {
		printf("Unable to find zettel: %s\n", zettel);
		return;
	}

	if (getenv("EDITOR") == NULL) {
		printf("EDITOR variable is not set\n");
		return;
	}

	char command[100];
	sprintf(command, "$EDITOR %s", path);

	system(command);	
}

void
hub_choice(char* zdir)
{
	printf("Enter new hub name:\n");
	char hubname[50];
	fgets(hubname, 50, stdin);
	hubname[strcspn(hubname, "\n")] = '\0';
	printf("\n");

	char hubpath[150];
	strcpy(hubpath, zdir);
	strcat(hubpath, hubname);

	new_hub(hubpath);	
}

void
zettel_choice(char* zdir)
{
	printf("Listing hubs:\n");
	list_hubs(zdir);
	printf("------\n");
	printf("Enter hub:\n");

	char hubname[50];
	fgets(hubname, 50, stdin);
	hubname[strcspn(hubname, "\n")] = '\0';
	printf("\n");

	char hubpath[200];
	strcpy(hubpath, zdir);
	strcat(hubpath, hubname);

	DIR* dr = opendir(hubpath);
	if (dr == NULL) {
		printf("Unable to open directory at: %s\n", hubpath);
		closedir(dr);
		return;
	}
	closedir(dr);

	printf("Enter the title of your new zettel:\n");

	char zettel[50];
	fgets(zettel, 50, stdin);
	zettel[strcspn(zettel, "\n")] = '\0';

	new_zettel(hubpath, zettel);	
}

void
link_choice(char* zdir)
{
	printf("Choose first zettel. Listing hubs:\n");
	list_hubs(zdir);
	printf("---\n");
	printf("Enter a hub:\n");

	char hub[50];
	fgets(hub, 50, stdin);
	hub[strcspn(hub, "\n")] = '\0';
	printf("\n");

	char hubpath[300];
	strcpy(hubpath, zdir);
	strcat(hubpath, hub);

	printf("Listing zettels in hub: %s\n", hub);
	if (read_hub(hubpath) != 0) {
		printf("Unable to find hub: %s\n", hub);
		return;
	}
	printf("----\n");
	printf("Enter the zettel id:\n");

	char zettel[10];
	fgets(zettel, 10, stdin);
	zettel[strcspn(zettel, "\n")] = '\0';
	printf("\n");

	char path[50];
	if (match_id_to_path(path, zettel, hubpath) != 0) {
		printf("Unable to find zettel: %s at %s\n", zettel, path);
		return;
	}

	printf("Choose second zettel. Listing hubs:\n");
	list_hubs(zdir);
	printf("---\n");
	printf("Enter a hub:\n");

	char hub2[50];
	fgets(hub2, 50, stdin);
	hub2[strcspn(hub2, "\n")] = '\0';
	printf("\n");

	char hubpath2[300];
	strcpy(hubpath2, zdir);
	strcat(hubpath2, hub2);

	printf("Listing zettels in hub: %s\n", hub2);
	if (read_hub(hubpath2) != 0) {
		printf("Unable to find hub: %s\n", hub2);
		return;
	}
	printf("----\n");
	printf("Enter a zettel by title:\n");

	char zettel2[10];
	fgets(zettel2, 10, stdin);
	zettel2[strcspn(zettel2, "\n")] = '\0';
	printf("\n");

	char path2[50];
	if (match_id_to_path(path2, zettel2, hubpath2) != 0) {
		printf("Unable to find zettel: %s\n", zettel2);
		return;
	}

	link_zettel(path, path2);
	link_zettel(path2, path);

	printf("%s and %s have been linked.\n", zettel, zettel2);	
}

void
user_interface(char* zdir)
{
        printf("Hi there! Welcome to Simple Zettelkasten in C.\n");
        printf("Do you want to create a new zettel, query for a zettel, or link two zettels?\n");



@@ 239,50 419,7 @@ user_interface(void)
        printf("\n");

        if (strcmp(choice, "query") == 0) {

                printf("Listing hubs:\n");
                list_hubs(zdir);
                printf("---\n");
                printf("Enter a hub:\n");

                char hub[50];
                fgets(hub, 50, stdin);
                hub[strcspn(hub, "\n")] = '\0';
                printf("\n");

                char hubpath[300];
                strcpy(hubpath, zdir);
                strcat(hubpath, hub);

                printf("Listing zettels in hub: %s\n", hub);
                if (read_hub(hubpath) != 0) {
                        printf("Unable to find hub: %s\n", hub);
                        return;
                }
                printf("----\n");
                printf("Enter the zettel id:\n");

                char zettel[10];
                fgets(zettel, 10, stdin);
                zettel[strcspn(zettel, "\n")] = '\0';
                printf("\n");

                char path[50];
                if (match_id_to_path(path, zettel, hubpath) != 0) {
                        printf("Unable to find zettel: %s\n", zettel);
                        return;
                }

                if (getenv("EDITOR") == NULL) {
                        printf("EDITOR variable is not set\n");
                        return;
                }

                char command[100];
                sprintf(command, "$EDITOR %s", path);

                system(command);

		query_choice(zdir);
        } else if (strcmp(choice, "new") == 0) {
                printf("Do you want to create a new hub or zettel?\n");
                char choice2[10];


@@ 291,133 428,47 @@ user_interface(void)
                printf("\n");

                if (strcmp(choice2, "hub") == 0) {
                        printf("Enter new hub name:\n");
                        char hubname[50];
                        fgets(hubname, 50, stdin);
                        hubname[strcspn(hubname, "\n")] = '\0';
                        printf("\n");

                        char hubpath[150];
                        strcpy(hubpath, zdir);
                        strcat(hubpath, hubname);

                        new_hub(hubpath);
			hub_choice(zdir);
                } else if (strcmp(choice2, "zettel") == 0) {
                        printf("Listing hubs:\n");
                        list_hubs(zdir);
                        printf("------\n");
                        printf("Enter hub:\n");

                        char hubname[50];
                        fgets(hubname, 50, stdin);
                        hubname[strcspn(hubname, "\n")] = '\0';
                        printf("\n");

                        char hubpath[200];
                        strcpy(hubpath, zdir);
                        strcat(hubpath, hubname);

                        DIR* dr = opendir(hubpath);
                        if (dr == NULL) {
                                printf("Unable to open directory at: %s\n", hubpath);
                                closedir(dr);
                                return;
                        }
                        closedir(dr);

                        printf("Enter the title of your new zettel:\n");

                        char zettel[50];
                        fgets(zettel, 50, stdin);
                        zettel[strcspn(zettel, "\n")] = '\0';

                        new_zettel(hubpath, zettel);

			zettel_choice(zdir);
                } else {
                        printf("Invalid command: %s. Try hub or zettel\n", choice2);
                }
        } else if (strcmp(choice, "link") == 0) {

                printf("Choose first zettel. Listing hubs:\n");
                list_hubs(zdir);
                printf("---\n");
                printf("Enter a hub:\n");

                char hub[50];
                fgets(hub, 50, stdin);
                hub[strcspn(hub, "\n")] = '\0';
                printf("\n");

                char hubpath[300];
                strcpy(hubpath, zdir);
                strcat(hubpath, hub);

                printf("Listing zettels in hub: %s\n", hub);
                if (read_hub(hubpath) != 0) {
                        printf("Unable to find hub: %s\n", hub);
                        return;
                }
                printf("----\n");
                printf("Enter the zettel id:\n");

                char zettel[10];
                fgets(zettel, 10, stdin);
                zettel[strcspn(zettel, "\n")] = '\0';
                printf("\n");

                char path[50];
                if (match_id_to_path(path, zettel, hubpath) != 0) {
                        printf("Unable to find zettel: %s at %s\n", zettel, path);
                        return;
                }

                printf("Choose second zettel. Listing hubs:\n");
                list_hubs(zdir);
                printf("---\n");
                printf("Enter a hub:\n");

                char hub2[50];
                fgets(hub2, 50, stdin);
                hub2[strcspn(hub2, "\n")] = '\0';
                printf("\n");

                char hubpath2[300];
                strcpy(hubpath2, zdir);
                strcat(hubpath2, hub2);

                printf("Listing zettels in hub: %s\n", hub2);
                if (read_hub(hubpath2) != 0) {
                        printf("Unable to find hub: %s\n", hub2);
                        return;
                }
                printf("----\n");
                printf("Enter a zettel by title:\n");

                char zettel2[10];
                fgets(zettel2, 10, stdin);
                zettel2[strcspn(zettel2, "\n")] = '\0';
                printf("\n");

                char path2[50];
                if (match_id_to_path(path2, zettel2, hubpath2) != 0) {
                        printf("Unable to find zettel: %s\n", zettel2);
                        return;
                }

                link_zettel(path, path2);
                link_zettel(path2, path);

                printf("%s and %s have been linked.\n", zettel, zettel2);

		link_choice(zdir);
        } else {
                printf("Invalid command: %s. Try query or new\n", choice);
        }

}

int
main(int argc, char* argv[])
{
        user_interface();
        char zdir[200];
	init_zdir(zdir);
	
	if (argc == 2) {
		if (strcmp(argv[1], "query") == 0) {
			query_choice(zdir);
		} else if (strcmp(argv[1], "link") == 0) {
			link_choice(zdir);
		} else {
			printf("Not a valid command: %s", argv[1]);
		}
	} else if(argc == 3) {
		if (strcmp(argv[1], "new") == 0) {
			if (strcmp(argv[2], "zettel") == 0) {
				zettel_choice(zdir);
			} else if (strcmp(argv[2], "hub") == 0) {
				hub_choice(zdir);
			} else {
				printf("Not a valid option for new: %s", argv[2]);
			}
		} else {
			printf("Not a valid command: %s", argv[1]);
		}
	} else {
		user_interface(zdir);
	}
        return 0;
}