From fbecb59a142ba1a46ed30563580fe1f1e7852f1c Mon Sep 17 00:00:00 2001 From: M600 Date: Wed, 15 Feb 2017 21:01:23 +0100 Subject: [PATCH] Reworked colors and fix a few bugs. Still missing advanced research --- 42sh/Makefile | 7 ++-- 42sh/includes/completion.h | 6 ++- 42sh/src/completion/c_files.c | 30 +++++++++++++- 42sh/src/completion/c_init.c | 3 +- 42sh/src/completion/c_matching.c | 10 ++++- 42sh/src/completion/c_output.c | 3 +- 42sh/src/completion/c_printer.c | 30 ++++++++------ 42sh/src/completion/c_rematch.c | 71 ++++++++++++++++++++++++++++++++ 42sh/src/completion/completion.c | 34 +++------------ 9 files changed, 145 insertions(+), 49 deletions(-) create mode 100644 42sh/src/completion/c_rematch.c diff --git a/42sh/Makefile b/42sh/Makefile index 2c2e3fbe..1b558a0a 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/07 20:11:22 by jhalford ### ########.fr # +# Updated: 2017/02/15 20:00:50 by alao ### ########.fr # # # # **************************************************************************** # @@ -52,6 +52,7 @@ completion/c_output.c\ completion/c_parser.c\ completion/c_pathsolver.c\ completion/c_printer.c\ +completion/c_rematch.c\ completion/c_sizing.c\ completion/c_terminal.c\ completion/completion.c\ @@ -229,7 +230,7 @@ NB = $(words $(SRC_BASE)) INDEX = 0 all : - @make -j $(NAME) + @make $(NAME) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) @$(CC) $(FLAGS) $(D_FLAGS) \ @@ -241,7 +242,7 @@ $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) @printf "\r\e[48;5;15;38;5;25m✅ MAKE $(NAME)\e[0m\e[K\n" $(LIBFT_LIB): - @make -j -C $(LIBFT_DIR) + @make -C $(LIBFT_DIR) $(OBJ_DIR) : @mkdir -p $(OBJ_DIR) diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index 86e5e57f..ca29ee0d 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/03 15:47:35 by alao ### ########.fr */ +/* Updated: 2017/02/15 20:00:32 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,8 @@ typedef struct s_clst ** c_pline : Number of item per line when printing. ** c_line : Number of line required to move to terminal up. ** win_x : Size of the window in length. +** key : The keypressed lastly. +** isfolder : If the match is a folder. boolean. ** lst : List of the item corresponding to the completion. */ @@ -71,6 +73,7 @@ typedef struct s_comp int c_line; int win_x; int key; + int isfolder; t_clst *lst; } t_comp; @@ -92,6 +95,7 @@ int c_sizing(t_comp *c); int c_updater(t_comp *c, char *select); int c_gtfo(t_comp *c, long int keypress); +int c_rematch(t_comp *c, long int keypress); /* ** Terminal related function (moving and printing the list) diff --git a/42sh/src/completion/c_files.c b/42sh/src/completion/c_files.c index 3eb56993..7fbbd5c4 100644 --- a/42sh/src/completion/c_files.c +++ b/42sh/src/completion/c_files.c @@ -6,12 +6,38 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:31:21 by alao #+# #+# */ -/* Updated: 2017/02/03 17:32:20 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 19:33:11 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" +/* +** 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 (/). +*/ + +static int c_exclusion_folder(t_comp *c) +{ + DIR *rep; + char *tmp; + + tmp = ft_strjoin(c->cpath, c->match); + if ((rep = opendir(tmp)) && (!closedir(rep))) + { + tmp ? ft_memdel((void *)&tmp) : (0); + tmp = ft_strjoin(c->rcmd, "/"); + if (c->trail) + data_singleton()->line.input = ft_strjoin(tmp, c->trail); + else + data_singleton()->line.input = ft_strdup(tmp); + data_singleton()->line.pos = data_singleton()->line.pos + 1; + c->isfolder = 1; + } + tmp ? ft_memdel((void *)&tmp) : (0); + return (0); +} /* ** Clear the binary from c->rcmd ans save the result in c->match. Return the ** path part of it if exist or NULL. @@ -56,5 +82,7 @@ int c_seek_files(t_data *s, t_comp *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); return (0); } diff --git a/42sh/src/completion/c_init.c b/42sh/src/completion/c_init.c index 0b045bff..a61129c3 100644 --- a/42sh/src/completion/c_init.c +++ b/42sh/src/completion/c_init.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 11:21:16 by alao #+# #+# */ -/* Updated: 2017/02/03 17:32:24 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 19:05:04 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ void c_init(t_data *s, long int input) s->comp->home = ft_strdup(ft_getenv(s->env, "HOME")); s->comp->pwd = ft_strdup(ft_getenv(s->env, "PWD")); s->comp->key = input; + s->comp->isfolder = 0; s->comp->prompt = s->line.prompt_size; c_matching(s, s->comp); } diff --git a/42sh/src/completion/c_matching.c b/42sh/src/completion/c_matching.c index 72bcc01d..d805e45c 100644 --- a/42sh/src/completion/c_matching.c +++ b/42sh/src/completion/c_matching.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */ -/* Updated: 2017/02/03 13:31:36 by alao ### ########.fr */ +/* Updated: 2017/02/15 19:06:32 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,13 @@ int c_matching(t_data *s, t_comp *c) { - if (!(ft_strchr(c->rcmd, ' '))) + if (c->rcmd[0] == '.' || c->rcmd[0] == '/') + { + c->rcmd[0] == '.' ? c->cpath = path_solver(c, "./", NULL) : 0; + c->rcmd[0] == '/' ? c->cpath = path_solver(c, "/", NULL) : 0; + c_parser(c, c->cpath, c->match); + } + else if (!(ft_strchr(c->rcmd, ' '))) c_seek_binary(s, c); else c_seek_files(s, c); diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index bd37994f..fe04ae4c 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/03 17:32:36 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 21:00:22 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ int c_gtfo(t_comp *c, long int keypress) if (keypress != 10) { +// c_rematch(c, keypress); c_clear(data_singleton()); return (1); } diff --git a/42sh/src/completion/c_printer.c b/42sh/src/completion/c_printer.c index 2a529fc6..694fca7b 100644 --- a/42sh/src/completion/c_printer.c +++ b/42sh/src/completion/c_printer.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 12:55:39 by alao #+# #+# */ -/* Updated: 2017/02/03 17:35:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 19:28:43 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,21 +19,29 @@ ** - (10) Symlink: Cyan "\e[96m" */ -static void c_printer_node(t_clst *lst) +static void c_printer_node(t_clst *lst, int c_sx) { - lst->cursor ? ft_putstr(tgetstr("us", NULL)) : (0); - lst->cursor ? (ft_putstr_fd("\033[31m", 2)) : (0); - lst->type == 4 ? ft_putstr_fd("\e[1;31m", 2) : (0); - lst->type == 10 ? ft_putstr_fd("\e[1;96m", 2) : (0); + int i; + + i = lst->len; + if (lst->cursor) + ft_putstr_fd("\e[7m", 2); + else + { + lst->type == 4 ? ft_putstr_fd("\e[1;31m", 2) : (0); + lst->type == 10 ? ft_putstr_fd("\e[1;96m", 2) : (0); + } ft_putstr_fd(lst->name, 2); - ft_putstr_fd("\033[00m", 2); if (lst->type == 4) ft_putstr_fd("/", 2); else if (lst->type == 10) ft_putstr_fd("@", 2); else ft_putstr_fd(" ", 2); - lst->cursor ? ft_putstr(tgetstr("ue", NULL)) : (0); + while (i++ < (c_sx)) + ft_putstr(" "); + ft_putstr_fd("\e[00m", 2); + ft_putstr(" "); } /* @@ -47,7 +55,7 @@ static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) t_clst *ptr; ptr = lst->next; - c_printer_node(lst); + c_printer_node(lst, c->c_sx); while (loop) { i = 1; @@ -60,9 +68,7 @@ static int c_printer_line(t_comp *c, t_clst *lst, int loop, int i) if (ptr != c->lst) { i = lst->len; - while (i++ < (c->c_sx + 1)) - ft_putstr(" "); - c_printer_node(ptr); + c_printer_node(ptr, c->c_sx); lst = ptr; ptr = ptr->next; } diff --git a/42sh/src/completion/c_rematch.c b/42sh/src/completion/c_rematch.c new file mode 100644 index 00000000..222e0e62 --- /dev/null +++ b/42sh/src/completion/c_rematch.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_rematch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: alao +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/15 12:03:30 by alao #+# #+# */ +/* Updated: 2017/02/15 20:17:55 by alao ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "completion.h" + +static int c_refresh_match(t_comp *c, long int keypress) +{ + DG("Refresh match"); + char *tmp; + char kpconv[2]; + + kpconv[0] = keypress; + kpconv[1] = '\0'; + tmp = c->match ? ft_strjoin(c->match, kpconv) : ft_strdup(kpconv); + c->match ? ft_memdel((void *)&c->match) : (0); + c->match = ft_strdup(tmp); + tmp ? ft_memdel((void *)&tmp) : (0); + (void)c; + 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); +} + +int c_rematch(t_comp *c, long int keypress) +{ + if (ft_isascii(keypress)) + { + c_refresh_match(c, keypress); + c_refresh_list(c); + } + return (0); +} diff --git a/42sh/src/completion/completion.c b/42sh/src/completion/completion.c index e8d70302..2768f615 100644 --- a/42sh/src/completion/completion.c +++ b/42sh/src/completion/completion.c @@ -6,38 +6,12 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/09/20 14:50:33 by alao #+# #+# */ -/* Updated: 2017/02/03 17:33:41 by jhalford ### ########.fr */ +/* Updated: 2017/02/15 19:30:18 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" -/* -** DEBUG FUNCTION -*/ - -int test(t_comp *c) -{ - t_clst *tmp; - int i; - - i = 1; - if (!(c->lst)) - { - return (0); - } - if (c->lst->prev != c->lst) - { - tmp = c->lst->next; - while (tmp != c->lst) - { - i++; - tmp = tmp->next; - } - } - return (0); -} - /* ** Function to select the next item in the list if it has already been Created ** and if the keypressed is tab. @@ -64,6 +38,11 @@ static void c_next_item(t_comp *c) static int c_dispatcher(t_data *s) { + if (s->comp && s->comp->lst == NULL && s->comp->isfolder) + { + c_clear(s); + return(1); + } if (s->comp && s->comp->lst == NULL) { c_clear(s); @@ -119,6 +98,5 @@ int completion(long int keypress) else return (c_gtfo(s->comp, keypress)); } - test(s->comp); return (c_dispatcher(s)); }