diff options
author | salad <fmoenne-@student.le-101.fr> | 2020-09-09 14:10:09 +0200 |
---|---|---|
committer | salad <fmoenne-@student.le-101.fr> | 2020-09-09 14:10:09 +0200 |
commit | ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f (patch) | |
tree | 4737eb8d8523c682eb13e491ab3f85dad5d86af0 | |
parent | clean, ca fait ziz (diff) | |
download | 42-minishell-ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f.tar.gz 42-minishell-ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f.tar.bz2 42-minishell-ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f.tar.xz 42-minishell-ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f.tar.zst 42-minishell-ca3cc66deebc4dc5ac52b14ed099c05f416ccc7f.zip |
insert a char try it
-rw-r--r-- | src/c_init.c | 9 | ||||
-rw-r--r-- | src/c_utils.c | 31 | ||||
-rw-r--r-- | src/c_utils.h | 1 |
3 files changed, 40 insertions, 1 deletions
diff --git a/src/c_init.c b/src/c_init.c index ad18d92..effcc7d 100644 --- a/src/c_init.c +++ b/src/c_init.c @@ -21,6 +21,7 @@ #include "c_input.h" #include "c_utils.h" #include "s_struct.h" +#include "m_prompt.h" static char *ft_strjoin_m(char *s1, @@ -84,6 +85,8 @@ int16_t return (1); } +/* c_insert_char */ + int16_t c_process_key(char *buf, t_caps *tcaps, @@ -98,7 +101,10 @@ int16_t return (0); if (ft_isprint(buf[0])) { - line = ft_strjoin_m(line, buf); + if (tcaps->cpos == ft_strlen(line)) + line = ft_strjoin_m(line, buf); + else + line = c_insert_char(line, buf[0], tcaps); c_redraw_line(line, tcaps->cpos, msh); tcaps->cpos++; } @@ -140,6 +146,7 @@ int16_t tcaps.cpos = 0; c_set_term_raw(1); c_get_struct(1, &tcaps); + m_prompt_psx(1, msh); if (!(c_get_win_size(&tcaps.ws))) return (-1); while (1) diff --git a/src/c_utils.c b/src/c_utils.c index 3eb7f4d..6621a1a 100644 --- a/src/c_utils.c +++ b/src/c_utils.c @@ -45,6 +45,37 @@ uint16_t } char +*c_insert_char(char *str, + char c, + t_caps *tcaps) +{ + size_t i; + size_t j; + size_t len; + char *dst; + + i = 0; + j = 0; + len = ft_strlen(str) + 2; + if (!(dst = (char*)malloc((len) * sizeof(char)))) + return (NULL); + while (i < len) + { + if (i == tcaps->cpos) + dst[i++] = c; + else + { + dst[i] = str[j]; + i++; + j++; + } + } + dst[len] = '\0'; + ft_memdel((void*)&str); + return (dst); +} + +char *c_delchar(char *str, uint16_t cpos) { diff --git a/src/c_utils.h b/src/c_utils.h index 967939f..add7d34 100644 --- a/src/c_utils.h +++ b/src/c_utils.h @@ -19,5 +19,6 @@ int16_t c_redraw_line(char *line, uint16_t cpos, t_msh *msh); char *c_delchar(char *str, uint16_t cpos); uint16_t c_get_win_size(struct winsize *ws); int32_t c_catch_tcaps(char *buf, char **line, t_caps *tcaps, t_msh *msh); +char *c_insert_char(char *str, char c, t_caps *tcaps); #endif |