modif des termcaps : nouvelle version avec ajout/suppr/del/mouv par ligne (option fleche haut/fleche bas)/ home/end qui fonctionne.

This commit is contained in:
Gautier WOJDA 2017-01-21 18:42:42 +01:00
parent a11d97c432
commit e853458465
11 changed files with 321 additions and 294 deletions

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/08 18:02:25 by sbenning #+# #+# */
/* Updated: 2017/01/20 17:35:02 by gwojda ### ########.fr */
/* Updated: 2017/01/20 19:08:51 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,7 +58,16 @@ typedef struct s_list_history
struct s_list_history *next;
} t_list_history;
int ft_is_next_char(char *str, char c);
void ft_get_beggin_with_curs(char *str, size_t *pos);
void ft_history(char **str, int ret, t_list_history **head, size_t *pos);
void ft_suppr(char **str, size_t *i);
void ft_del(char **str, size_t *i);
void ft_current_str(char *str, size_t pos);
int ft_strlen_next(char *str, size_t pos);
void ft_putall_current_str(char *str, size_t *pos);
void ft_get_next_str(char *str, size_t *pos);
void ft_get_beggin(char *str, size_t *pos);
long long ft_pow(int nbr, int power);
char *ft_strndup(char const *s, int n);
char *ft_strdupi(char const *s);
@ -83,10 +92,6 @@ void ft_init_line(void);
void ft_read_it(int input, size_t *pos, char **str);
void ft_check_quotes(char **s, t_list_history *head);
int ft_check_quotes_num(char *s);
void ft_del_2(char **str, size_t *i);
void ft_del_1(char **str, size_t *i);
void ft_suppr_2(char **str, size_t *i);
void ft_suppr_1(char **str, size_t *i);
void ft_print(char **str, int ret, size_t *i);
void ft_move_to_line(int ret, size_t *pos, char *str);
void ft_get_head(t_list_history **head);
@ -96,7 +101,6 @@ void ft_curse_move(char *str);
void ft_move_suppr(char *str, size_t pos);
void ft_move_dell(char *str, size_t pos);
void ft_move_to_word(int ret, size_t *pos, char *str);
void ft_history(char **str, int ret, t_list_history **head);
void ft_move_term(int ret, size_t *pos, char *str);
void ft_move_left(size_t pos, char *str);
void ft_home_end(char *str, int ret, size_t *pos);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */
/* Updated: 2017/01/19 16:42:49 by gwojda ### ########.fr */
/* Updated: 2017/01/21 11:12:55 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,19 +15,9 @@
static int ft_lecture_3(int ret, char **str, size_t *i)
{
if (ret == 127 && (*i) > 0)
{
if ((*str)[*i - 1] != '\n')
ft_suppr_1(str, i);
else
ft_suppr_2(str, i);
}
ft_suppr(str, i);
else if (ret == TOUCHE_DELETE && (*str) && (*i) < ft_strlen((*str)))
{
if ((*str)[*i] != '\n')
ft_del_1(str, i);
else
ft_del_2(str, i);
}
ft_del(str, i);
else
return (0);
return (1);
@ -69,11 +59,9 @@ char *ft_lecture(t_list_history *head)
continue ;
else if (ret == FLECHE_BAS || ret == FLECHE_HAUT)
{
ft_history(&str, ret, &head, &i);
if (str)
ft_putstr(str + i);
ft_history(&str, ret, &head);
if (str)
i = ft_strlen(str);
i = ft_strlen_next(str, i);
else
i = 0;
}

View file

@ -6,17 +6,20 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */
/* Updated: 2017/01/19 16:42:45 by gwojda ### ########.fr */
/* Updated: 2017/01/21 11:40:51 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void ft_history_2(char **str, int ret, t_list_history **head)
void ft_history_2(char **str, int ret, t_list_history **head, size_t *pos)
{
if (*str)
{
ft_curse_move(*str);
--(*pos);
ft_get_beggin_with_curs(*str, pos);
ft_puttermcaps("cd");
*pos = 0;
ft_strdel(str);
}
if (ret == FLECHE_BAS)
@ -28,15 +31,15 @@ static void ft_history_2(char **str, int ret, t_list_history **head)
else
*str = ft_strdup((*head)->str);
if (*str)
ft_putstr(*str);
ft_current_str(*str, *pos);
}
}
void ft_history(char **str, int ret, t_list_history **head)
void ft_history(char **str, int ret, t_list_history **head, size_t *pos)
{
if (!*head)
return ;
ft_history_2(str, ret, head);
ft_history_2(str, ret, head, pos);
if (ret == FLECHE_HAUT)
{
if ((*head)->prev)
@ -46,7 +49,7 @@ void ft_history(char **str, int ret, t_list_history **head)
else
*str = ft_strdup((*head)->str);
if (*str)
ft_putstr(*str);
ft_current_str(*str, *pos);
}
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */
/* Updated: 2017/01/19 16:42:41 by gwojda ### ########.fr */
/* Updated: 2017/01/21 17:13:55 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,64 +22,60 @@ void ft_home_end(char *str, int ret, size_t *pos)
{
if (ret == TOUCHE_END && str)
{
ft_putstr(str + *pos);
(*pos) = ft_strlen(str);
if (*pos)
{
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
ft_puttermcaps("cd");
while (str[*pos])
++(*pos);
ft_get_beggin(str, pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
}
else if (ret == TOUCHE_HOME)
ft_move_to_beggin(str, pos);
}
void ft_move_left(size_t pos, char *str)
{
int i;
int nb;
int prompt_size;
prompt_size = data_singleton()->line.prompt_size;
i = 1;
nb = ft_nb_last_line(str, pos - 1) + 3;
while (i < nb)
{
ft_puttermcaps("nd");
++i;
}
if (!(pos - i) || i == 1)
{
while (--prompt_size + 3)
ft_puttermcaps("nd");
if (*pos)
{
--(*pos);
ft_get_beggin_with_curs(str, pos);
if (str[*pos + 1] == '\n')
ft_puttermcaps("nd");
}
ft_puttermcaps("cd");
*pos = 0;
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
if (!str[*pos])
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
}
void ft_move_term(int ret, size_t *pos, char *str)
{
int len;
int str_len;
size_t tmp;
len = 0;
str_len = 0;
if (ret == FLECHE_DROITE && str && ft_strlen(str) > *pos
&& str[(*pos)] == '\n')
if (ret == FLECHE_DROITE && str && ft_strlen(str) > *pos)
{
ft_puttermcaps("do");
++(*pos);
}
else if (ret == FLECHE_GAUCHE && *pos > 0
&& str[(*pos) - 1] == '\n')
{
ft_puttermcaps("up");
--(*pos);
str_len = ft_nb_line(str, *pos - 1);
len = ft_get_size_prev(str, *pos - 1) - (ft_size_term() * str_len);
if (ft_get_ind_prev(str, *pos - 1) == 0)
len += data_singleton()->line.prompt_size + 2;
while (--len + 1)
ft_puttermcaps("nd");
}
else if (ret == FLECHE_DROITE && str && ft_strlen(str) > *pos)
{
ft_putchar(str[*pos]);
++(*pos);
if (str[*pos] == '\n')
{
tmp = *pos - 1;
ft_get_beggin_with_curs(str, &tmp);
ft_puttermcaps("cd");
++(*pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
if (!str[*pos])
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
else
{
ft_putchar(str[*pos]);
++(*pos);
}
if (ft_nb_last_line(str, *pos) == ft_size_term() - 1)
{
ft_putchar(' ');
@ -88,7 +84,19 @@ void ft_move_term(int ret, size_t *pos, char *str)
}
else if (ret == FLECHE_GAUCHE && *pos > 0)
{
ft_puttermcaps("le");
--(*pos);
if (str[*pos - 1] == '\n')
{
ft_puttermcaps("cd");
(*pos) -= 2;
ft_get_beggin(str, pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
++(*pos);
}
else
{
ft_puttermcaps("le");
--(*pos);
}
}
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */
/* Updated: 2017/01/19 16:43:01 by gwojda ### ########.fr */
/* Updated: 2017/01/21 17:29:08 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,77 +14,73 @@
static void ft_up(size_t *pos, char *str)
{
int i;
int j;
int len;
int tmp;
int size_window;
int i;
int len;
size_window = ft_size_term();
i = 0;
len = 0;
j = 0;
if (!*pos)
return ;
while (len < size_window && *pos - i)
if (str[*pos - 1] == '\n')
{
if (i && str[*pos - i] == '\n')
{
ft_puttermcaps("up");
tmp = ft_nb_last_line(str, *pos - 1 - i) + 3;
if (ft_get_ind_prev(str, *pos - 1 - i))
tmp -= 2;
while (j < tmp - 1)
{
ft_puttermcaps("nd");
++j;
}
if (!ft_get_ind_prev(str, *pos - 1 - i))
ft_putchar('\b');
len += ft_size_term() - tmp;
}
else
{
ft_putchar('\b');
len++;
}
i++;
}
if (!(*pos - i))
{
(*pos) = 0;
return ;
}
if (j)
{
ft_puttermcaps("nd");
ft_puttermcaps("cd");
(*pos) -= 2;
ft_get_beggin(str, pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
++(*pos);
}
*pos -= i;
else
{
len = ft_size_term();
if (str[*pos - i] == '\n')
{
--len;
ft_puttermcaps("le");
++i;
}
while (*pos - i && str[*pos - i] != '\n' && --len + 1)
{
ft_puttermcaps("le");
++i;
}
if (str[*pos - i] == '\n')
{
ft_puttermcaps("nd");
++(*pos);
}
(*pos) -= i;
}
}
static void ft_down(size_t *pos, char *str)
{
int len;
int size_window;
char boolean;
int i;
int len;
len = 0;
boolean = 0;
size_window = ft_size_term();
while (str[*pos] && len < size_window)
i = 0;
if (str[*pos] == '\n')
{
if (str[*pos] == '\n' && !boolean)
if (*pos)
{
len += size_window - ft_nb_last_line(str, *pos - 1) - 2;
boolean = 1;
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
else if (str[*pos] == '\n' && boolean)
return ;
else
++len;
ft_putchar(str[*pos]);
++(*pos);
ft_puttermcaps("cd");
ft_get_next_str(str, pos);
(*pos) += 2;
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
if (!(str[*pos]))
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
else
{
len = ft_size_term();
while (str[i + *pos] && str[i + *pos] != '\n' && --len + 1)
{
ft_putchar(str[i + *pos]);
++i;
}
*pos += i;
}
}

View file

@ -6,12 +6,12 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/05 16:02:43 by gwojda #+# #+# */
/* Updated: 2017/01/20 17:49:25 by gwojda ### ########.fr */
/* Updated: 2017/01/21 16:57:38 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
void ft_print(char **str, int ret, size_t *i)
{
int j;
@ -25,128 +25,48 @@ void ft_print(char **str, int ret, size_t *i)
}
ft_putnc('\b', j - 1);
++(*i);
}*/
void ft_print(char **str, int ret, size_t *i)
{
size_t len;
size_t tmp_len;
char boolean;
char boolean2;
len = 0;
boolean = 0;
boolean2 = 0;
*str = ft_realloc_imput(*str, ret, *i);
tmp_len = ft_strlen(*str);
sleep(1);
ft_puttermcaps("cd");
sleep(1);
if (ft_is_next_char(*str, '\n') &&
ft_nb_last_line(*str, *i + 1) + (len % ft_size_term()) == ft_size_term() - 2)
{
sleep(1);
write(1, *str + *i, 1);
sleep(1);
ft_putnc(' ', ft_size_term());
sleep(1);
write(1, *str + *i + 1, ft_strlen(*str + *i + 1));
sleep(1);
ft_puttermcaps("up");
boolean2 = 1;
}
else
write(1, *str + *i, ft_strlen(*str + *i));
sleep(1);
++(*i);
while ((*str)[*i + len] && (*str)[*i + len] != '\n')
++len;
sleep(1);
if (len)
--len;
// if (ft_nb_last_line(*str, *i) + (len % ft_size_term()) == ft_size_term() - 1)
// {
// --(tmp_len);
// ft_move_to_beggin(*str, &tmp_len);
// boolean = 1;
// }
// else
ft_move_to_beggin(*str, &tmp_len);
// ft_printf("\n\n\n%d\n\n\n\n", ft_nb_last_line(*str, *i) + (len % ft_size_term()) );
sleep(1);
if (boolean2)
ft_puttermcaps("up");
write(1, *str, *i);
if (boolean2)
ft_puttermcaps("do");
sleep(1);
if (boolean)
ft_putstr(" \b");
}
void ft_suppr_1(char **str, size_t *i)
void ft_suppr(char **str, size_t *i)
{
int j;
size_t tmp;
j = 0;
write(1, "\b", 1);
while (*((*str) + *i + j) && *((*str) + *i + j) != '\n')
{
ft_putchar(*((*str) + *i + j));
++j;
}
--(*i);
write(1, " ", 1);
if (ft_nb_last_line(*str, *i) + j == ft_size_term() - 2)
tmp = *i;
*str = ft_remove_imput((*str), tmp);
ft_get_beggin_with_curs(*str, i);
ft_puttermcaps("cd");
ft_current_str(*str, *i);
ft_get_next_str(*str, i);
/* if (ft_nb_last_line(*str, *i) == ft_size_term() - 2)
{
ft_puttermcaps("nd");
ft_putnc('\b', j);
ft_putnc('\b', *i - tmp);
}
else
ft_putnc('\b', j + 1);
*str = ft_remove_imput((*str), (*i));
ft_putnc('\b', *i - tmp + 1);*/
ft_putnc('\b', *i - tmp);
(*i) = tmp;
}
void ft_suppr_2(char **str, size_t *i)
void ft_del(char **str, size_t *i)
{
int j;
size_t tmp;
j = 0;
ft_puttermcaps("up");
ft_move_left(*i - 1, *str);
ft_puttermcaps("cd");
ft_putstr(*str + *i);
ft_move_suppr(*str, *i);
tmp = *i;
*str = ft_remove_imput((*str), tmp);
--(*i);
*str = ft_remove_imput((*str), (*i));
}
void ft_del_1(char **str, size_t *i)
{
int j;
j = 0;
while (*((*str) + *i + j + 1) && *((*str) + *i + j + 1) != '\n')
{
ft_putchar(*((*str) + *i + j + 1));
++j;
}
write(1, " ", 1);
if (ft_nb_last_line(*str, *i) + j == ft_size_term() - 2)
ft_putnc('\b', j);
else
ft_putnc('\b', j + 1);
(*str) = ft_remove_imput((*str), (*i));
}
void ft_del_2(char **str, size_t *i)
{
int j;
j = 1;
ft_get_beggin_with_curs(*str, i);
ft_puttermcaps("cd");
ft_putstr(*str + *i + 1);
ft_move_dell(*str, *i);
*str = ft_remove_imput((*str), (*i));
ft_current_str(*str, *i);
ft_get_next_str(*str, i);
/* if (ft_nb_last_line(*str, *i) == ft_size_term() - 2)
{
ft_puttermcaps("nd");
ft_putnc('\b', *i - tmp);
}
else
ft_putnc('\b', *i - tmp + 1);*/
ft_putnc('\b', *i - tmp);
(*i) = tmp;
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tool_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 18:20:23 by gwojda #+# #+# */
/* Updated: 2017/01/21 17:04:13 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_get_beggin(char *str, size_t *pos)
{
while ((*pos) && str[(*pos)] != '\n')
--(*pos);
if (str[(*pos)] == '\n')
++(*pos);
}
void ft_get_next_str(char *str, size_t *pos)
{
while (str[(*pos)] && str[(*pos)] != '\n')
++(*pos);
if (str[(*pos)] == '\n')
--(*pos);
}
void ft_putall_current_str(char *str, size_t *pos)
{
int len;
len = 0;
ft_get_beggin(str, pos);
while (str[(*pos) + len] && str[(*pos) + len] != '\n')
++len;
write(1, str + *pos, len);
}
void ft_current_str(char *str, size_t pos)
{
int len;
len = 0;
while (str[pos + len] && str[pos + len] != '\n')
++len;
write(1, str + pos, len);
}
int ft_strlen_next(char *str, size_t pos)
{
int len;
len = 0;
while (str[pos + len] && str[pos + len] != '\n')
++len;
return (len);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tool_line_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */
/* Updated: 2017/01/21 16:52:48 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_get_beggin_with_curs(char *str, size_t *pos)
{
while ((*pos) && str[(*pos)] != '\n')
{
--(*pos);
ft_puttermcaps("le");
}
if (!*pos)
ft_puttermcaps("le");
if (str[(*pos)] == '\n')
++(*pos);
}

View file

@ -6,26 +6,12 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */
/* Updated: 2017/01/20 17:34:57 by gwojda ### ########.fr */
/* Updated: 2017/01/20 18:54:46 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_is_next_char(char *str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (1);
++i;
}
return (0);
}
void ft_putnc(char c, int n)
{
int i;

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/16 16:14:46 by gwojda #+# #+# */
/* Updated: 2017/01/19 16:43:19 by gwojda ### ########.fr */
/* Updated: 2017/01/21 18:16:56 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,40 +52,76 @@ char *ft_remove_imput(char *str, size_t pos)
return (new_str2);
}
static int ft_found_prev_word_pos(char *str, size_t *pos)
{
size_t tmp;
tmp = *pos;
while ((tmp) && (str[tmp] == ' ' || str[tmp] == '\n'))
--tmp;
while ((tmp) && str[tmp] != ' ' && str[tmp] != '\n')
--tmp;
return (tmp);
}
void ft_found_prev_word(char *str, size_t *pos)
{
size_t tmp;
int i;
tmp = *pos;
ft_move_to_beggin(str, pos);
*pos = tmp;
tmp = ft_found_prev_word_pos(str, pos);
write(1, str, tmp);
*pos = tmp;
i = 0;
if (!*pos)
return ;
if (str[*pos - 1] == '\n')
{
ft_puttermcaps("cd");
ft_get_beggin(str, pos);
--(*pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
++(*pos);
}
else
{
if (!(str[*pos - i] == '\n' || str[*pos - i] == ' '))
{
ft_puttermcaps("le");
--(*pos);
}
while (str[*pos - i] == '\n' || str[*pos - i] == ' ')
{
ft_puttermcaps("le");
++i;
}
while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ')
{
ft_puttermcaps("le");
++i;
}
if (str[*pos - i] == '\n' || str[*pos - i] == ' ')
{
ft_puttermcaps("nd");
++(*pos);
}
(*pos) -= i;
}
}
void ft_found_next_word(char *str, size_t *pos)
{
while (str[(*pos)] == ' ' || str[(*pos)] == '\n')
int i;
i = 0;
if (str[*pos] == '\n')
{
ft_putchar(str[(*pos)]);
++(*pos);
sleep(1);
if (*pos)
{
--(*pos);
ft_get_beggin_with_curs(str, pos);
}
ft_puttermcaps("cd");
ft_get_next_str(str, pos);
ft_current_str(str, *pos);
ft_get_next_str(str, pos);
(*pos) -= 2;
ft_get_beggin_with_curs(str, pos);
(*pos) += 2;
}
while (str[(*pos)] && str[(*pos)] != ' ' && str[(*pos)] != '\n')
else
{
ft_putchar(str[(*pos)]);
++(*pos);
while (str[i + *pos] && str[i + *pos] != '\n' && str[i + *pos] != ' ')
{
ft_putchar(str[i + *pos]);
++i;
}
*pos += i;
}
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/08 12:35:11 by gwojda #+# #+# */
/* Updated: 2017/01/20 18:03:59 by gwojda ### ########.fr */
/* Updated: 2017/01/21 14:31:47 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */