From 05a24720b41ae873037c4b7ac4c344b5b8d96ed7 Mon Sep 17 00:00:00 2001 From: SerhiiStets Date: Wed, 7 Dec 2022 01:01:02 +0200 Subject: [PATCH] added command to open folders in explorer --- Makefile | 4 +- README.md | 12 ++++++ commands/help.c | 5 ++- commands/open.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ commands/open.h | 6 +++ definitions.h | 2 +- fed.c | 2 + 7 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 commands/open.c create mode 100644 commands/open.h diff --git a/Makefile b/Makefile index 75dbd39..d7d98ad 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ endif CFLAGS = -Wall -g # define the C source files -SRCS = fed.c utils.c commands/add.c commands/help.c commands/ls.c commands/clean.c commands/cd.c +SRCS = fed.c utils.c commands/add.c commands/help.c commands/ls.c commands/clean.c commands/cd.c commands/open.c # define the C object files # @@ -54,4 +54,4 @@ clean: $(RM) *.o commands\*.o $(MAIN).exe $(MAIN) -# DO NOT DELETE THIS LINE -- make depend needs it \ No newline at end of file +# DO NOT DELETE THIS LINE -- make depend needs it diff --git a/README.md b/README.md index c0bf9f0..fdbb209 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,18 @@ $ fed clean python_projects fed: The directory [python_projects] E:\Desktop is deleted from favourites. ``` +### **open** + +Opens given folder in explorer. Can take either alias or number in favourite list. + +```shell +$ fed open nvim + fed: openning C:\Users\admin\AppData\Local\nvim directory. + +$ fed open 1 + fed: openning C: directory. +``` + ### **help** Receive info for all available commands. diff --git a/commands/help.c b/commands/help.c index edd61a4..a560f7c 100644 --- a/commands/help.c +++ b/commands/help.c @@ -18,6 +18,9 @@ int cmd_help(int argc, const char **argv) \t\t\t\tremoves folder from favourite by given alias.\n\ \t\t\tremoves fodler given by number in ls list.\n\ \t\t\tremoves folder from favourites.\n\n\ + open\n\ + \t\t\t\topens folder in explorer from favourite by given alias.\n\ + \t\t\topens fodler in explorer given by number in ls list.\n\n\ help\t\t\tdisplay help.\n\n\ version\t\t\tdisplay cli version.\n", CLI_NAME); @@ -28,4 +31,4 @@ int cmd_version(int argc, const char **argv) { fprintf(stdout, "%s version %s\n", CLI_NAME, VERSION); return 0; -} \ No newline at end of file +} diff --git a/commands/open.c b/commands/open.c new file mode 100644 index 0000000..9dfb6d4 --- /dev/null +++ b/commands/open.c @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include "../utils.h" +#include "../definitions.h" + +/** + * @brief Opens the folder in explorer + * + * @param argc Number of arguments passed + * @param argv Arguments + * @return int Status of function success + */ +int cmd_open(int argc, const char **argv) +{ + FILE *pFile; + Named_dir alias_dir; + int i = 1; + int is_in_favourites = 0; + char buffer[MAX]; /* not ISO 90 compatible */ + char temp[MAX]; + char delim[3] = "[]"; + char *token; + + if (!argc) + { + fprintf(stdout, "%s: no paramters passed. Please use \n\n%s open / \n\n", CLI_NAME, CLI_NAME); + return 0; + } + + char *conf_path = get_cfg_path(); + + pFile = fopen(conf_path, "r"); + while (fgets(buffer, MAX, pFile)) + { + buffer[strlen(buffer) - 1] = '\0'; + /* Storing file line to temp to not mess with buffer that's gonna be used later */ + strcpy(temp, buffer); + + /* if argument is a number then check which dir it is in the list and break */ + if (i == atoi(argv[0])) + { + /* Trying to fecth a directory from temp if the line has alias */ + /* if alias_dir.directory is NULL then there was no alias */ + /* and there's no need to change the value of the buffer */ + token = strtok(temp, delim); + alias_dir.alias = token; + token = strtok(NULL, delim); + alias_dir.directory = token; + if (alias_dir.directory) + strcpy(buffer, alias_dir.directory + 1); + is_in_favourites = 1; + break; + } + + + /* if the code goes here then the user did not pass a number */ + /* so code tries to see if the alias was passed and if it exists */ + token = strtok(temp, delim); + alias_dir.alias = token; + token = strtok(NULL, delim); + alias_dir.directory = token; + + if (alias_dir.directory) + { + /* if alias == argument passed, copy parsed directory to buffer for future wokr */ + if (!strcmp(alias_dir.alias, argv[0])) + { + is_in_favourites = 1; + strcpy(buffer, alias_dir.directory + 1); + break; + } + } + i++; + } + fclose(pFile); + free(conf_path); + + if (is_in_favourites) + { + char open_explr_cmd[MAX]; + strncpy(open_explr_cmd, "start \"\" \"", 11); + strncat(open_explr_cmd, buffer, strlen(buffer) + 1); + strncat(open_explr_cmd, "\"", 2); + fprintf(stdout, "%s: openning %s directory.\n\n", CLI_NAME, buffer); + system(open_explr_cmd); + return 1; + } + + /* Inform user if argument passed can not be traced to favourte folders */ + fprintf(stderr, "%s: %s is not in favourites.\ + \nUse %s to see what directoris are in favourites.\n\n", + CLI_NAME, argv[0], CLI_NAME); + + return 1; +} diff --git a/commands/open.h b/commands/open.h new file mode 100644 index 0000000..f8eb04d --- /dev/null +++ b/commands/open.h @@ -0,0 +1,6 @@ +#ifndef OPEN_H +#define OPEN_H + +int cmd_open(int argc, const char **argv); + +#endif diff --git a/definitions.h b/definitions.h index 860b094..aced9da 100644 --- a/definitions.h +++ b/definitions.h @@ -9,7 +9,7 @@ #endif #define CLI_NAME "fed" -#define VERSION "0.0.1" +#define VERSION "0.2.0" #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define MAX 255 #endif diff --git a/fed.c b/fed.c index 5c43f43..d080e82 100644 --- a/fed.c +++ b/fed.c @@ -10,6 +10,7 @@ #include "commands/ls.h" #include "commands/clean.h" #include "commands/cd.h" +#include "commands/open.h" #include "definitions.h" #include "utils.h" @@ -25,6 +26,7 @@ static struct cmd_struct commands[] = { {"ls", cmd_ls}, {"cd", cmd_cd}, {"clean", cmd_clean}, + {"open", cmd_open}, {"version", cmd_version}, {"help", cmd_help}, };