Skip to content

Commit

Permalink
added <open> command to open folders in explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiStets committed Dec 6, 2022
1 parent 6146d1e commit 05a2472
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -54,4 +54,4 @@ clean:
$(RM) *.o commands\*.o $(MAIN).exe $(MAIN)


# DO NOT DELETE THIS LINE -- make depend needs it
# DO NOT DELETE THIS LINE -- make depend needs it
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion commands/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ int cmd_help(int argc, const char **argv)
\t\t<alias>\t\tremoves folder from favourite by given alias.\n\
\t\t<number>\tremoves fodler given by number in ls list.\n\
\t\t<directory>\tremoves folder from favourites.\n\n\
open\n\
\t\t<alias>\t\topens folder in explorer from favourite by given alias.\n\
\t\t<number>\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);
Expand All @@ -28,4 +31,4 @@ int cmd_version(int argc, const char **argv)
{
fprintf(stdout, "%s version %s\n", CLI_NAME, VERSION);
return 0;
}
}
97 changes: 97 additions & 0 deletions commands/open.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#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 <alias>/<number> \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 <ls> to see what directoris are in favourites.\n\n",
CLI_NAME, argv[0], CLI_NAME);

return 1;
}
6 changes: 6 additions & 0 deletions commands/open.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef OPEN_H
#define OPEN_H

int cmd_open(int argc, const char **argv);

#endif
2 changes: 1 addition & 1 deletion definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions fed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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},
};
Expand Down

0 comments on commit 05a2472

Please sign in to comment.