From 46d7e7ce4e436b94bd13cead50f19045779fa594 Mon Sep 17 00:00:00 2001 From: M600 Date: Thu, 16 Feb 2017 18:05:21 +0100 Subject: [PATCH] Fix completion nested in the command --- 42sh/includes/completion.h | 5 ++++- 42sh/src/completion/c_clear.c | 4 +++- 42sh/src/completion/c_files.c | 16 +++++----------- 42sh/src/completion/c_init.c | 26 ++++++++++++++++++++++++-- 42sh/src/completion/c_matching.c | 4 ++-- 42sh/src/completion/c_output.c | 31 ++++++++++++++++++++++++------- 42sh/src/completion/c_rematch.c | 7 ++++++- 42sh/src/completion/c_terminal.c | 6 +++--- 42sh/src/completion/completion.c | 4 +++- 9 files changed, 74 insertions(+), 29 deletions(-) diff --git a/42sh/includes/completion.h b/42sh/includes/completion.h index 5848e590..70951fdf 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/16 01:19:17 by alao ### ########.fr */ +/* Updated: 2017/02/16 17:09:17 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,10 @@ typedef struct s_comp char *match; char *home; char *pwd; + char *start; + char *between; char *trail; + int cutpoint; int prompt; int c_sx; int c_sy; diff --git a/42sh/src/completion/c_clear.c b/42sh/src/completion/c_clear.c index 31c25201..fac24088 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/16 11:37:43 by alao ### ########.fr */ +/* Updated: 2017/02/16 17:43:57 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,8 @@ int c_clear(t_data *s) ptr->cpath ? ft_memdel((void *)&ptr->cpath) : (0); ptr->home ? ft_memdel((void *)&ptr->home) : (0); ptr->pwd ? ft_memdel((void *)&ptr->pwd) : (0); + ptr->start ? ft_memdel((void *)&ptr->start) : (0); + ptr->between ? ft_memdel((void *)&ptr->between) : (0); ptr->trail ? ft_memdel((void *)&ptr->trail) : (0); if (ptr->lst && ptr->lst->name) c_clear_lst(ptr); diff --git a/42sh/src/completion/c_files.c b/42sh/src/completion/c_files.c index 941e6ba8..5df12b3a 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/16 16:31:34 by alao ### ########.fr */ +/* Updated: 2017/02/16 18:00:04 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,22 +21,16 @@ static int c_exclusion_folder(t_comp *c) { - DG("Exclusion"); DIR *rep; char *tmp; + char *tmp2; 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); + tmp2 = ft_strjoin(c->match, "/"); + c_updater(c, tmp2); + tmp2 ? ft_memdel((void *)&tmp2) : (0); return (1); } tmp ? ft_memdel((void *)&tmp) : (0); diff --git a/42sh/src/completion/c_init.c b/42sh/src/completion/c_init.c index a61129c3..b9978300 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/15 19:05:04 by alao ### ########.fr */ +/* Updated: 2017/02/16 17:45:54 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,11 @@ static char *c_trimmer(char *cmd, int st, int nd) { char *rt; + char *tmp; int len; rt = NULL; + tmp = NULL; len = ft_strlen(cmd); if (st == len) st--; @@ -31,7 +33,15 @@ static char *c_trimmer(char *cmd, int st, int nd) while (nd < len && cmd[nd] && cmd[nd] != ';' && cmd[nd] != ' ') nd++; rt = ft_strsub(cmd, st, nd - st); - return (rt); + data_singleton()->comp->cutpoint = st; + st = 0; + while (rt[st] == ' ') + st++; + tmp = ft_strsub(rt, st, ft_strlen(rt)); + if (st) + data_singleton()->comp->between = ft_strsub(rt, 0, st); + rt ? ft_memdel((void *)&rt) : (0); + return (tmp); } /* @@ -46,12 +56,19 @@ void c_init(t_data *s, long int input) if (!(s->comp = (t_comp *)malloc((sizeof(t_comp))))) return ; + s->comp->cutpoint = 0; + s->comp->between = NULL; s->comp->rcmd = c_trimmer(s->line.input, s->line.pos, s->line.pos); len_trail = ft_strlen(s->line.input) - s->line.pos; if (ft_strlen(s->line.input) > s->line.pos) s->comp->trail = ft_strsub(s->line.input, s->line.pos, len_trail); else s->comp->trail = NULL; + + if (s->comp->cutpoint) + s->comp->start = ft_strsub(s->line.input, 0, s->comp->cutpoint); + else + s->comp->start = NULL; s->comp->ircmd = s->line.pos; s->comp->match = NULL; s->comp->cpath = NULL; @@ -63,5 +80,10 @@ void c_init(t_data *s, long int input) s->comp->key = input; s->comp->isfolder = 0; s->comp->prompt = s->line.prompt_size; +// DG("Init end as:"); +// DG("Start [%s]", s->comp->start); +// DG("Between [%s]", s->comp->between); +// DG("RCMD [%s]", s->comp->rcmd); +// DG("Trail [%s]", s->comp->trail); c_matching(s, s->comp); } diff --git a/42sh/src/completion/c_matching.c b/42sh/src/completion/c_matching.c index 7b267ac7..a698ad66 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/16 15:16:05 by alao ### ########.fr */ +/* Updated: 2017/02/16 17:57:51 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ int c_matching(t_data *s, t_comp *c) c_seek_binary(s, c); else c_seek_files(s, c); - if (c->lst) + if (s->comp && c->lst) { c_sizing(c); return (1); diff --git a/42sh/src/completion/c_output.c b/42sh/src/completion/c_output.c index 72f36ec0..e70f006c 100644 --- a/42sh/src/completion/c_output.c +++ b/42sh/src/completion/c_output.c @@ -6,12 +6,31 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:10:38 by alao #+# #+# */ -/* Updated: 2017/02/16 15:16:10 by alao ### ########.fr */ +/* Updated: 2017/02/16 18:02:47 by alao ### ########.fr */ /* */ /* ************************************************************************** */ #include "completion.h" +static int c_updater_rcmd(t_comp *c) +{ + char *tmp; + char *tmp2; + int new_pos; + + tmp = ft_strjoin(c->start, c->between); + tmp2 = ft_strjoin(tmp, c->rcmd); + c->rcmd ? ft_memdel((void *)&c->rcmd) : (0); + c->rcmd = ft_strjoin(tmp2, c->trail); + tmp ? ft_memdel((void *)&tmp) : (0); + tmp2 ? ft_memdel((void *)&tmp2) : (0); + data_singleton()->line.input ? ft_memdel((void *)&data_singleton()->line.input) : (0); + data_singleton()->line.input = ft_strdup(c->rcmd); + new_pos = ft_strlen(c->start) + ft_strlen(c->between) + ft_strlen(c->rcmd); + data_singleton()->line.pos = new_pos; + return (1); +} + /* ** Output function. Will update the data->line.input along with the ** position of the cursor in data->line.pos. If the autocompletion occur in @@ -34,14 +53,12 @@ int c_updater(t_comp *c, char *select) tmp = ft_strdup(c->rcmd); rt = ft_strjoin(tmp, select); tmp ? ft_memdel((void *)&tmp) : (0); - data_singleton()->line.input ? ft_memdel((void *)&data_singleton()->line.input) : (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 = ft_strlen(c->rcmd) + ft_strlen(select); + c->rcmd ? ft_memdel((void *)&c->rcmd) : (0); + c->rcmd = ft_strdup(rt); + c_updater_rcmd(c); rt ? ft_memdel((void *)&rt) : (0); c_clear(data_singleton()); +// DG("Module complete commands [%s] with pos [%d]", data_singleton()->line.input, data_singleton()->line.pos); return (1); } diff --git a/42sh/src/completion/c_rematch.c b/42sh/src/completion/c_rematch.c index 9d92a979..d7d33ba1 100644 --- a/42sh/src/completion/c_rematch.c +++ b/42sh/src/completion/c_rematch.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */ -/* Updated: 2017/02/16 16:37:35 by alao ### ########.fr */ +/* Updated: 2017/02/16 18:04:15 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,5 +68,10 @@ int c_rematch(t_comp *c, long int keypress) return(1); } } + else + { + c_term_clear(c); + c_clear(data_singleton()); + } return (0); } diff --git a/42sh/src/completion/c_terminal.c b/42sh/src/completion/c_terminal.c index c98efe70..b11c754c 100644 --- a/42sh/src/completion/c_terminal.c +++ b/42sh/src/completion/c_terminal.c @@ -6,7 +6,7 @@ /* By: alao +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/10/11 10:44:40 by alao #+# #+# */ -/* Updated: 2017/02/16 12:16:30 by alao ### ########.fr */ +/* Updated: 2017/02/16 16:58:53 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ void c_term_clear(t_comp *c) ft_putstr(tgetstr("up", NULL)); i = 0; lcmd = 0; - c->rcmd ? lcmd += (int)ft_strlen(c->rcmd) + c->prompt + 1 : 0; + c->rcmd ? lcmd += c->ircmd + c->prompt + 1 : 0; while (i < lcmd) { ft_putstr(tgetstr("nd", NULL)); @@ -53,7 +53,7 @@ void c_term_mv_back(t_comp *c) ft_putstr(tgetstr("cr", NULL)); i = 0; lcmd = 0; - c->rcmd ? lcmd += (int)ft_strlen(c->rcmd) + c->prompt + 1 : 0; + c->rcmd ? lcmd += c->ircmd + c->prompt + 1 : 0; while (i < lcmd) { ft_putstr(tgetstr("nd", NULL)); diff --git a/42sh/src/completion/completion.c b/42sh/src/completion/completion.c index 595ee337..f1732087 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/16 11:47:59 by alao ### ########.fr */ +/* Updated: 2017/02/16 17:59:23 by alao ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,6 +91,8 @@ int completion(long int keypress) s->line.input[s->line.pos] != '\0') return (0); c_init(s, keypress); + if (s->comp == NULL) + return (1); } else {