diff --git a/42sh/Makefile b/42sh/Makefile index c066e723..b2a8cb9a 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -267,8 +267,12 @@ line_editing/print_and_del.c\ line_editing/queue.c\ line_editing/readline.c\ line_editing/resize.c\ +line_editing/underline_end.c\ line_editing/underline_function.c\ +line_editing/underline_home.c\ +line_editing/underline_left.c\ line_editing/underline_reset.c\ +line_editing/underline_right.c\ main/data_exit.c\ main/data_init.c\ main/data_singleton.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 9536c4ab..f6ed61f0 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/03/22 21:14:55 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 23:19:10 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,7 @@ # define TOUCHE_F5 892427035 # define TOUCHE_F6 925981467 -# define SIZE_LINE 512 +# define SIZE_LINE 16384 # define CORRUPT 1 @@ -127,11 +127,14 @@ int ft_nb_of_line(char *str, size_t pos); int ft_get_size_prev(char *str, size_t pos); void sigwinch_resize(int sig); size_t ft_hist_len(void); -void underline_right(char **str, size_t *pos, size_t pos_ref); -void underline_left(char **str, size_t *pos, size_t pos_ref); + +int underline_right(char **str, size_t *pos, size_t pos_ref); +int underline_left(char **str, size_t *pos, size_t pos_ref); +void underline_home(char **str, size_t *pos, size_t pos_ref); +void underline_end(char **str, size_t *pos, size_t pos_ref); +void underline_check_end_of_line(char *str, size_t pos); void reset_term(char **str, size_t *pos); void reset_and_remove_term(char **str, size_t *pos, char *copy_tmp); -int reset_term_hard(void); int ft_read_stdin(char **input); diff --git a/42sh/src/line_editing/copy_cut_paste.c b/42sh/src/line_editing/copy_cut_paste.c index 23411367..a562f968 100644 --- a/42sh/src/line_editing/copy_cut_paste.c +++ b/42sh/src/line_editing/copy_cut_paste.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ -/* Updated: 2017/03/22 21:14:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 23:19:57 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,12 +27,10 @@ int ft_x(char **str, size_t *pos) ret = 0; if (read(STDIN, &ret, sizeof(int)) < 0) return (-1); - if ((*str)[*pos] == '\n') - return (reset_term_hard()); - if (ret == FLECHE_GAUCHE) - underline_left(str, pos, pos_ref); - else if (ret == FLECHE_DROITE) - underline_right(str, pos, pos_ref); + if (ret == FLECHE_GAUCHE && !underline_left(str, pos, pos_ref)) + return (0); + else if (ret == FLECHE_DROITE && !underline_right(str, pos, pos_ref)) + return (0); else break ; } @@ -56,13 +54,15 @@ int ft_c(char **str, size_t *pos) ret = 0; if (read(STDIN, &ret, sizeof(int)) < 0) return (-1); - if ((*str)[*pos] == '\n') - return (reset_term_hard()); - if (ret == FLECHE_GAUCHE) - underline_left(str, pos, pos_ref); - else if (ret == FLECHE_DROITE) - underline_right(str, pos, pos_ref); - else + if (ret == FLECHE_GAUCHE && !underline_left(str, pos, pos_ref)) + return (0); + else if (ret == FLECHE_DROITE && !underline_right(str, pos, pos_ref)) + return (0); + else if (ret == TOUCHE_HOME) + underline_home(str, pos, pos_ref); + else if (ret == TOUCHE_END) + underline_end(str, pos, pos_ref); + else if (ret != FLECHE_DROITE && ret != FLECHE_GAUCHE) break ; } reset_term(str, pos); diff --git a/42sh/src/line_editing/underline_end.c b/42sh/src/line_editing/underline_end.c new file mode 100644 index 00000000..c43e9ce5 --- /dev/null +++ b/42sh/src/line_editing/underline_end.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* underline_end.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/22 23:18:15 by gwojda #+# #+# */ +/* Updated: 2017/03/22 23:42:27 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void underline_end(char **str, size_t *pos, size_t pos_ref) +{ + size_t pos_tmp; + + pos_tmp = *pos; + if ((*str)[pos_tmp] == '\n') + { + underline_right(str, pos, pos_ref); + ++pos_tmp; + } + while ((*str)[pos_tmp] && (*str)[pos_tmp] != '\n') + { + underline_right(str, pos, pos_ref); + ++pos_tmp; + } + if ((*str)[pos_tmp] == '\n') + { + underline_left(str, pos, pos_ref); + --pos_tmp; + } +} diff --git a/42sh/src/line_editing/underline_function.c b/42sh/src/line_editing/underline_function.c index 5412e6af..34c0f318 100644 --- a/42sh/src/line_editing/underline_function.c +++ b/42sh/src/line_editing/underline_function.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 20:55:24 by gwojda #+# #+# */ -/* Updated: 2017/03/22 21:28:31 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 22:59:02 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void underline_check_end_of_line(char *str, size_t pos) +void underline_check_end_of_line(char *str, size_t pos) { if (!str) return ; @@ -22,61 +22,3 @@ static void underline_check_end_of_line(char *str, size_t pos) ft_puttermcaps("le"); } } - -void underline_right(char **str, size_t *pos, size_t pos_ref) -{ - if (!(*pos < ft_strlen(*str))) - return ; - if (*pos >= pos_ref) - { - ft_puttermcaps("mr"); - data_singleton()->line.copy_tmp = ft_realloc_imput(data_singleton()-> - line.copy_tmp, (*str)[*pos], data_singleton()->line.pos_tmp); - ft_putchar((*str)[*pos]); - ft_puttermcaps("me"); - ++(data_singleton()->line.pos_tmp); - ++(*pos); - underline_check_end_of_line(*str, *pos); - } - else - { - data_singleton()->line.copy_tmp = ft_remove_imput(data_singleton()-> - line.copy_tmp, data_singleton()->line.pos_tmp); - ft_putchar((*str)[*pos]); - if (data_singleton()->line.pos_tmp) - --(data_singleton()->line.pos_tmp); - ++(*pos); - underline_check_end_of_line(*str, *pos); - } -} - -void underline_left(char **str, size_t *pos, size_t pos_ref) -{ - if (!*pos) - return ; - if (*pos > pos_ref) - { - --(*pos); - ft_putchar('\b'); - ft_putchar((*str)[*pos]); - underline_check_end_of_line(*str, *pos + 1); - ft_putchar('\b'); - data_singleton()->line.copy_tmp = ft_remove_imput(data_singleton()-> - line.copy_tmp, data_singleton()->line.pos_tmp); - if (data_singleton()->line.pos_tmp) - --(data_singleton()->line.pos_tmp); - } - else - { - data_singleton()->line.pos_tmp = 0; - --(*pos); - ft_putchar('\b'); - ft_puttermcaps("mr"); - data_singleton()->line.copy_tmp = ft_realloc_imput(data_singleton()-> - line.copy_tmp, (*str)[*pos], data_singleton()->line.pos_tmp); - ft_putchar((*str)[*pos]); - underline_check_end_of_line(*str, *pos + 1); - ft_puttermcaps("me"); - ft_putchar('\b'); - } -} diff --git a/42sh/src/line_editing/underline_home.c b/42sh/src/line_editing/underline_home.c new file mode 100644 index 00000000..0c6fe355 --- /dev/null +++ b/42sh/src/line_editing/underline_home.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* underline_home.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/22 23:11:05 by gwojda #+# #+# */ +/* Updated: 2017/03/22 23:44:44 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void underline_home(char **str, size_t *pos, size_t pos_ref) +{ + size_t pos_tmp; + + pos_tmp = *pos; + if ((*str)[pos_tmp] == '\n') + { + underline_left(str, pos, pos_ref); + --pos_tmp; + } + while ((pos_tmp && (*str)[pos_tmp] != '\n') + && (pos_tmp + 1 > 0 && (*str)[pos_tmp - 1] != '\n')) + { + underline_left(str, pos, pos_ref); + --pos_tmp; + } + *pos = pos_tmp; +} diff --git a/42sh/src/line_editing/underline_left.c b/42sh/src/line_editing/underline_left.c new file mode 100644 index 00000000..0d3cd41a --- /dev/null +++ b/42sh/src/line_editing/underline_left.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* underline_left.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/22 22:54:28 by gwojda #+# #+# */ +/* Updated: 2017/03/22 23:08:58 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int reset_term_hard(void) +{ + ft_putstr(data_singleton()->line.copy_tmp); + ft_putnc('\b', ft_strlen(data_singleton()->line.copy_tmp)); + ft_strdel(&data_singleton()->line.copy_tmp); + data_singleton()->line.pos_tmp = 0; + return (0); +} + +static void left_abs(char **str, size_t *pos) +{ + --(*pos); + ft_putchar('\b'); + data_singleton()->line.pos_tmp = 0; + ft_puttermcaps("mr"); + data_singleton()->line.copy_tmp = ft_realloc_imput(data_singleton()-> + line.copy_tmp, (*str)[*pos], data_singleton()->line.pos_tmp); + ft_putchar((*str)[*pos]); + underline_check_end_of_line(*str, *pos + 1); + ft_puttermcaps("me"); + ft_putchar('\b'); +} + +static void left_mv_back(char **str, size_t *pos) +{ + --(*pos); + ft_putchar('\b'); + ft_putchar((*str)[*pos]); + underline_check_end_of_line(*str, *pos + 1); + data_singleton()->line.copy_tmp = ft_remove_imput(data_singleton()-> + line.copy_tmp, data_singleton()->line.pos_tmp); + if (data_singleton()->line.pos_tmp) + --(data_singleton()->line.pos_tmp); + ft_putchar('\b'); +} + +int underline_left(char **str, size_t *pos, size_t pos_ref) +{ + if (!*pos) + return (1); + if ((*str)[*pos - 1] == '\n') + return (reset_term_hard()); + if (*pos > pos_ref) + left_mv_back(str, pos); + else + left_abs(str, pos); + return (1); +} diff --git a/42sh/src/line_editing/underline_reset.c b/42sh/src/line_editing/underline_reset.c index bddcf88c..6fd150a5 100644 --- a/42sh/src/line_editing/underline_reset.c +++ b/42sh/src/line_editing/underline_reset.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 21:14:07 by gwojda #+# #+# */ -/* Updated: 2017/03/22 21:16:24 by gwojda ### ########.fr */ +/* Updated: 2017/03/22 23:40:39 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ void reset_term(char **str, size_t *pos) { - size_t pos_ref; + size_t pos_ref; pos_ref = *pos; if (*pos) @@ -26,39 +26,30 @@ void reset_term(char **str, size_t *pos) ft_current_str(*str, *pos); ft_get_next_str(*str, pos); ft_putnc('\b', *pos - pos_ref); - (*pos) = pos_ref; + (*pos) = pos_ref + (((*str)[*pos]) ? 1 : 0); } void reset_and_remove_term(char **str, size_t *pos, char *copy_tmp) { - size_t pos_ref; + size_t pos_ref; pos_ref = *pos; if (!data_singleton()->line.pos_tmp) pos_ref += ft_strlen(data_singleton()->line.copy_tmp); + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(*str, pos); + } while (*copy_tmp) { --pos_ref; *str = ft_remove_imput(*str, pos_ref); ++copy_tmp; } - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(*str, pos); - } ft_puttermcaps("cd"); ft_current_str(*str, *pos); ft_get_next_str(*str, pos); ft_putnc('\b', *pos - pos_ref); - (*pos) = pos_ref; -} - -int reset_term_hard(void) -{ - ft_putnc('\b', ft_strlen(data_singleton()->line.copy_tmp)); - ft_putstr(data_singleton()->line.copy_tmp); - ft_strdel(&data_singleton()->line.copy_tmp); - data_singleton()->line.pos_tmp = 0; - return (0); + (*pos) = pos_ref + (((*str)[*pos]) ? 1 : 0); } diff --git a/42sh/src/line_editing/underline_right.c b/42sh/src/line_editing/underline_right.c new file mode 100644 index 00000000..85ba5b41 --- /dev/null +++ b/42sh/src/line_editing/underline_right.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* underline_right.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/22 22:50:52 by gwojda #+# #+# */ +/* Updated: 2017/03/22 23:00:32 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int reset_term_hard(void) +{ + ft_putnc('\b', ft_strlen(data_singleton()->line.copy_tmp)); + ft_putstr(data_singleton()->line.copy_tmp); + ft_strdel(&data_singleton()->line.copy_tmp); + data_singleton()->line.pos_tmp = 0; + return (0); +} + +static void right_abs(char **str, size_t *pos) +{ + ft_puttermcaps("mr"); + data_singleton()->line.copy_tmp = ft_realloc_imput(data_singleton()-> + line.copy_tmp, (*str)[*pos], data_singleton()->line.pos_tmp); + ft_putchar((*str)[*pos]); + ft_puttermcaps("me"); + ++(data_singleton()->line.pos_tmp); + ++(*pos); + underline_check_end_of_line(*str, *pos); +} + +static void right_mv_back(char **str, size_t *pos) +{ + data_singleton()->line.copy_tmp = ft_remove_imput(data_singleton()-> + line.copy_tmp, data_singleton()->line.pos_tmp); + ft_putchar((*str)[*pos]); + if (data_singleton()->line.pos_tmp) + --(data_singleton()->line.pos_tmp); + ++(*pos); + underline_check_end_of_line(*str, *pos); +} + +int underline_right(char **str, size_t *pos, size_t pos_ref) +{ + if (!(*pos < ft_strlen(*str))) + return (1); + if ((*str)[*pos] == '\n') + return (reset_term_hard()); + if (*pos >= pos_ref) + right_abs(str, pos); + else + right_mv_back(str, pos); + return (1); +}