diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index ca29ee0d..5848e590 100644 --- a/42sh/includes/completion.h +++ b/42sh/includes/completion.h @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/02/18 11:13:04 by alao #+# #+# */ -/* Updated: 2017/02/15 20:00:32 by alao ### ########.fr */ +/* Updated: 2017/02/16 01:19:17 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,6 +103,7 @@ int c_rematch(t_comp *c, long int keypress); void c_term_mv_down(t_comp *c); void c_term_mv_back(t_comp *c); +void c_term_clear(t_comp *c); void c_printer(t_comp *c); /* @@ -110,6 +111,7 @@ void c_printer(t_comp *c); */ int c_clear(t_data *s); +int c_clear_lst(t_comp *c); char *path_solver(t_comp *c, char *cmd, char *cwd); int test(t_comp *c); diff --git a/42sh/src/completion/c_binary.c b/42sh/src/completion/c_binary.c index 555488a7..4d9b05b3 100644 --- a/42sh/src/completion/c_binary.c +++ b/42sh/src/completion/c_binary.c @@ -6,15 +6,16 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:30:57 by alao #+# #+# */ -/* Updated: 2017/02/03 17:31:46 by jhalford ### ########.fr */ +/* Updated: 2017/02/16 12:02:41 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* -** Retrieve the path from the env and cycle through it to find the matching -** element with the c_parser() function +** Retrieve the path from the env and create a char ** from the PATH pattern. +** The function will cycle through it to find the matching element using the +** c_parser() function */ int c_seek_binary(t_data *s, t_comp *c) diff --git a/42sh/src/completion/c_clear.c b/42sh/src/completion/c_clear.c index 57141311..31c25201 100644 --- a/42sh/src/completion/c_clear.c +++ b/42sh/src/completion/c_clear.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:37:43 by alao #+# #+# */ -/* Updated: 2017/02/03 17:32:05 by jhalford ### ########.fr */ +/* Updated: 2017/02/16 11:37:43 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ ** Clear the list from the memory */ -static void c_clear_lst(t_comp *c) +int c_clear_lst(t_comp *c) { t_clst *c_lst; t_clst *p_lst; @@ -35,6 +35,7 @@ static void c_clear_lst(t_comp *c) } c_lst->name ? ft_memdel((void *)&c_lst->name) : (0); c_lst ? ft_memdel((void *)&c_lst) : (0); + return (0); } /* @@ -55,6 +56,7 @@ int c_clear(t_data *s) ptr->trail ? ft_memdel((void *)&ptr->trail) : (0); if (ptr->lst && ptr->lst->name) c_clear_lst(ptr); + s->comp ? ft_memdel((void *)&s->comp) : (0); s->comp = NULL; return (0); } diff --git a/42sh/src/completion/c_files.c b/42sh/src/completion/c_files.c index 7fbbd5c4..bf548265 100644 --- a/42sh/src/completion/c_files.c +++ b/42sh/src/completion/c_files.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:31:21 by alao #+# #+# */ -/* Updated: 2017/02/15 19:33:11 by alao ### ########.fr */ +/* Updated: 2017/02/16 12:04:10 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ ** If the parsing for local file fail. The function is called to check if the ** match is actually a folder. If so, the command is updated with a trailing ** slash (/). +** Exemple: cd folder[tab] to cd folder/ */ static int c_exclusion_folder(t_comp *c) @@ -62,7 +63,7 @@ static char *c_slicer(t_comp *c) while (i > 0 && tmp[i] && tmp[i] != '/') i--; tmp[i] == '/' ? i++ : (0); - rt = ft_strsub(tmp, 0, i); + rt = (i == (int)ft_strlen(tmp) - 1) ? NULL : ft_strsub(tmp, 0, i); if (i <= (int)ft_strlen(tmp) - i + 1) c->match = ft_strsub(tmp, i, ft_strlen(tmp) - i); tmp ? ft_memdel((void *)&tmp) : (0); @@ -78,9 +79,12 @@ int c_seek_files(t_data *s, t_comp *c) char *path; (void)s; - path = c_slicer(c); - c->cpath = path_solver(c, path, NULL); - path ? ft_memdel((void *)&path) : (0); + if (c->cpath == NULL) + { + path = c_slicer(c); + c->cpath = path_solver(c, path, NULL); + path ? ft_memdel((void *)&path) : (0); + } c_parser(c, c->cpath, c->match); if (c->lst == NULL) c_exclusion_folder(c); diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index fe04ae4c..b4df647a 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/02/15 21:00:22 by alao ### ########.fr */ +/* Updated: 2017/02/16 12:10:24 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,48 +17,76 @@ ** position of the cursor in data->line.pos. If the autocompletion occur in ** the middle of the command, it will join the trailing part of it. Therefor ** recreating the commands completed. -** Once that done, it will clear all the memory related and return zero. +** Once that done, it will clear all the memory related and return one for the +** line edition module which will then trigger an update on the command line. */ -int c_updater(t_comp *c, char *select) +int c_updater(t_comp *c, char *select) { char *tmp; char *rt; - int new_pos; tmp = NULL; rt = NULL; - new_pos = c->ircmd + (ft_strlen(select) - ft_strlen(c->match)) + 1; - tmp = ft_strsub(c->rcmd, 0, ft_strlen(c->rcmd) - ft_strlen(c->match)); + if (c->match) + tmp = ft_strsub(c->rcmd, 0, ft_strlen(c->rcmd) - ft_strlen(c->match)); + else + tmp = ft_strdup(c->rcmd); rt = ft_strjoin(tmp, select); tmp ? ft_memdel((void *)&tmp) : (0); if (c->trail) data_singleton()->line.input = ft_strjoin(rt, c->trail); else data_singleton()->line.input = ft_strdup(rt); - data_singleton()->line.pos = new_pos; + data_singleton()->line.pos = ft_strlen(c->rcmd) + ft_strlen(select); rt ? ft_memdel((void *)&rt) : (0); c_clear(data_singleton()); return (1); } /* -** Placeholder to clear the memory if an other key than tab is pressed to know -** if the command should be updated or not before clearing the memory. +** Keypress handling function. +** +** The function will determine the right behavior depending on the key pressed +** If a delete key is called, the function will clear all the line and delete +** the module memory. +** if a validation key is called, the function will search for the selected +** node and call c_updater(). +** If none of the above behavior is right, the function will call for +** c_rematch() which will recreate the list by adding the keypressed to the +** c->match variable. +** +** RETURN VALUE: +** If the function doesn't require an update of the command line, it will +** return 0. Else it will return 1. +** +** Keypress values that cancel the module: +** 27: Escape +** 127: Backspace +** 2117294875: Delete +** +** Keypress values that validate the choice: +** 10: Enter +** 32: Space */ -int c_gtfo(t_comp *c, long int keypress) +int c_gtfo(t_comp *c, long int keypress) { t_clst *ptr; - if (keypress != 10) + if (keypress == 27 || keypress == 127 || keypress == 2117294875) { -// c_rematch(c, keypress); + c_term_clear(c); c_clear(data_singleton()); + return (0); + } + if (keypress == 10 || keypress == 32) + { + ptr = c->lst; + while (!ptr->cursor) + ptr = ptr->next; + c_updater(c, ptr->name); return (1); } - ptr = c->lst; - while (!ptr->cursor) - ptr = ptr->next; - return (c_updater(c, ptr->name)); + return ((c_rematch(c, keypress)) ? (0) : (1)); } diff --git a/42sh/src/completion/c_pathsolver.c b/42sh/src/completion/c_pathsolver.c index 140dae2d..22c5573b 100644 --- a/42sh/src/completion/c_pathsolver.c +++ b/42sh/src/completion/c_pathsolver.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/25 18:21:54 by alao #+# #+# */ -/* Updated: 2017/02/03 17:32:50 by jhalford ### ########.fr */ +/* Updated: 2017/02/16 12:18:45 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,10 @@ char *ft_sstrtostr(char **s, char *sep) return (tmp); } +/* +** Solve the tilde pattern in the path +*/ + static char *tilde(t_comp *c, char *cmd) { char *t_home; @@ -75,8 +79,8 @@ static char *tilde(t_comp *c, char *cmd) } /* -** BUILTINS: Advanced dots purge if the first is not enough. Remove a /path/ -** from the raw command if .. is found. Delete the . . +** Advanced dots purge if the first is not enough. Remove a /path/ from +** the raw command if .. is found. Delete the . . */ static char *dots_purge(char **scwd, char **scmd, int i) @@ -109,7 +113,7 @@ static char *dots_purge(char **scwd, char **scmd, int i) } /* -** BUILTINS: Solve the dots pattern in the path +** Solve the dots pattern in the path */ static char *dots(char *cmd, char *cwd, int i) @@ -141,7 +145,7 @@ static char *dots(char *cmd, char *cwd, int i) } /* -** BUILTINS: Determine the type of commands and redistribute it +** Path solving function. Turn any abstract path to the full version. */ char *path_solver(t_comp *c, char *cmd, char *cwd) @@ -152,7 +156,7 @@ char *path_solver(t_comp *c, char *cmd, char *cwd) dir = NULL; tmp = NULL; - ncmd = cmd ? ft_strtrim(cmd) : NULL; + ncmd = (cmd != NULL) ? ft_strtrim(cmd) : NULL; cwd = getcwd(cwd, 512); !cwd ? cwd = ft_strdup(c->pwd) : (0); if (!ncmd || (*ncmd != '.' && *ncmd != '~' && *ncmd != '/')) diff --git a/42sh/src/completion/c_printer.c b/42sh/src/completion/c_printer.c index 694fca7b..6dc92063 100644 --- a/42sh/src/completion/c_printer.c +++ b/42sh/src/completion/c_printer.c @@ -6,17 +6,18 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 12:55:39 by alao #+# #+# */ -/* Updated: 2017/02/15 19:28:43 by alao ### ########.fr */ +/* Updated: 2017/02/16 12:20:20 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* -** AUTOCOMPLETION: Print the name with or without an underline and colored upon -** file type as follow: -** - (4) Folder: Red "\e[1;31m" -** - (10) Symlink: Cyan "\e[96m" +** Print the name with or without an underline and colored upon file type +** as follow: +** - (4) Folder: Red "\e[1;31m" +** - (10) Symlink: Cyan "\e[96m" +** If the element is under selection, the video mode is inverted instead. */ static void c_printer_node(t_clst *lst, int c_sx) @@ -45,9 +46,9 @@ static void c_printer_node(t_clst *lst, int c_sx) } /* -** AUTOCOMPLETION: Cycle through the line to print on the same line the -** number of time to fill it PER LINE and add 2 space after the print to -** accomodate the trailing / for folder and a space in between. +** Cycle through the line to print on the same line the number of time to +** fill it PER LINE and add 2 space after the print to accomodate the +** trailing / for folder and a space in between. */ static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) @@ -78,7 +79,7 @@ static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) } /* -** AUTOCOMPLETION: Control the number of time it cycle for LINE +** Control the number of time it cycle for LINE */ void c_printer(t_comp *c) diff --git a/42sh/src/completion/c_rematch.c b/42sh/src/completion/c_rematch.c index 222e0e62..1b27819f 100644 --- a/42sh/src/completion/c_rematch.c +++ b/42sh/src/completion/c_rematch.c @@ -6,15 +6,18 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */ -/* Updated: 2017/02/15 20:17:55 by alao ### ########.fr */ +/* Updated: 2017/02/16 12:15:28 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" +/* +** Recreate a c->match value by adding the new key pressed to it. +*/ + static int c_refresh_match(t_comp *c, long int keypress) { - DG("Refresh match"); char *tmp; char kpconv[2]; @@ -28,44 +31,31 @@ static int c_refresh_match(t_comp *c, long int keypress) return (0); } -static int c_refresh_list(t_comp *c) -{ - DG("Refresh list"); - t_clst *ptr; - t_clst *ptr_bk; - - ptr = c->lst; - c->lst->prev->next = NULL; - c->lst->prev = NULL; - while (ptr->next) - { - DG("Refresh list loop on [%s]", ptr->name); - if (ft_strncmp(c->match, ptr->name, ft_strlen(c->match)) != 0) - { - if (ptr == c->lst) - { - c->lst = ptr->next; - c->lst->prev = NULL; - } - ptr->prev->next = ptr->next; - ptr->next->prev = ptr->prev; - ptr_bk = ptr->next; - ptr->name ? ft_memdel((void *)&ptr->name) : (0); - ptr ? ft_memdel((void *)&ptr) : (0); - ptr = ptr_bk; - } - ptr = ptr->next; - } - DG("Refresh list end"); - return (0); -} +/* +** The function is called when the module already exist and a foreign key is +** pressed (like a new letter). If the input key is on the ascii table, the +** previous display of the list is cleared and a new c->match values is +** created. The previous list is then cleared followed by a call to +** c_seek_files() function which will try to recreate a list. If so, the sizing +** is updated and the function return 1. If the research for a new listing fail +** the function return 0 which will tell the next function to cancel the update +** and clear the module memory. +*/ int c_rematch(t_comp *c, long int keypress) { if (ft_isascii(keypress)) { + c_term_clear(c); c_refresh_match(c, keypress); - c_refresh_list(c); + c_clear_lst(c); + c->lst = NULL; + c_seek_files(data_singleton(), c); + if (c->lst) + { + c_sizing(c); + return (1); + } } return (0); } diff --git a/42sh/src/completion/c_terminal.c b/42sh/src/completion/c_terminal.c index 42245c97..c98efe70 100644 --- a/42sh/src/completion/c_terminal.c +++ b/42sh/src/completion/c_terminal.c @@ -6,15 +6,37 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/11 10:44:40 by alao #+# #+# */ -/* Updated: 2017/02/03 17:33:08 by jhalford ### ########.fr */ +/* Updated: 2017/02/16 12:16:30 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" /* -** AUTOCOMPLETION: Move the terminal up by the number of line needed and -** move it back up to the original position +** Clear the previous list from the screen and restore the same position. +*/ + +void c_term_clear(t_comp *c) +{ + int i; + int lcmd; + + ft_putstr(tgetstr("do", NULL)); + ft_putstr(tgetstr("cd", NULL)); + ft_putstr(tgetstr("up", NULL)); + i = 0; + lcmd = 0; + c->rcmd ? lcmd += (int)ft_strlen(c->rcmd) + c->prompt + 1 : 0; + while (i < lcmd) + { + ft_putstr(tgetstr("nd", NULL)); + i++; + } +} + +/* +** Move the terminal up by the number of line needed and move it back up to +** the original position. */ void c_term_mv_back(t_comp *c) @@ -40,8 +62,8 @@ void c_term_mv_back(t_comp *c) } /* -** AUTOCOMPLETION: Move the terminal down by the number of line needed and -** move it back up to the first line under the prompt +** Move the terminal down by the number of line needed and move it back up to +** the first line under the prompt */ void c_term_mv_down(t_comp *c) @@ -52,6 +74,7 @@ void c_term_mv_down(t_comp *c) while (i < c->c_line) { ft_putstr(tgetstr("do", NULL)); + ft_putstr(tgetstr("cd", NULL)); i++; } i = 0; diff --git a/42sh/src/completion/completion.c b/42sh/src/completion/completion.c index 2768f615..595ee337 100644 --- a/42sh/src/completion/completion.c +++ b/42sh/src/completion/completion.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/09/20 14:50:33 by alao #+# #+# */ -/* Updated: 2017/02/15 19:30:18 by alao ### ########.fr */ +/* Updated: 2017/02/16 11:47:59 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,11 +41,12 @@ static int c_dispatcher(t_data *s) if (s->comp && s->comp->lst == NULL && s->comp->isfolder) { c_clear(s); - return(1); + return (1); } if (s->comp && s->comp->lst == NULL) { c_clear(s); + return (1); } else if (s->comp && s->comp->lst == s->comp->lst->next) return (c_updater(s->comp, s->comp->lst->name)); @@ -96,7 +97,10 @@ int completion(long int keypress) if (keypress == TOUCHE_TAB) c_next_item(s->comp); else - return (c_gtfo(s->comp, keypress)); + { + if (c_gtfo(s->comp, keypress)) + return (1); + } } return (c_dispatcher(s)); }