changed some stuff with lineedit

This commit is contained in:
Maieul BOYER 2024-07-20 14:37:38 +02:00
parent 1d4fd27f97
commit 0cb2d91cac
No known key found for this signature in database
9 changed files with 95 additions and 139 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:23:25 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:24:26 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 13:52:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@
t_error line_edit_insert(t_line_state *state, char c);
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
t_const_str prompt);
t_str line_edit_feed(t_line_state *state);
bool line_edit_feed(t_line_state *state, t_str *out);
void line_edit_backspace(t_line_state *state);
void line_edit_delete(t_line_state *state);
void line_edit_history_next(t_line_state *state, t_history_direction dir);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:37:40 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:48:56 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 13:58:35 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,7 +30,7 @@ t_error line_get_cursor_position(t_fd *input, t_fd *output,
t_u32 *column_out);
t_u32 line_get_columns(t_fd *input, t_fd *output);
t_usize line_get_prompt_len(t_const_str s);
t_str line_no_tty_impl(void);
bool line_no_tty_impl(t_str *out);
void line_print_key_codes(void);
t_error line_enable_raw_mode(t_fd *fd);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:24:34 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:53:54 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 14:31:55 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,6 +71,7 @@ enum e_key_action
K_CTRL_U = 21,
K_CTRL_W = 23,
K_ESC = 27,
K_SIGQUIT = 28,
K_BACKSPACE = 127
};

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:26:46 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 14:00:06 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,11 +22,6 @@
#include <sys/ioctl.h>
#include <termios.h>
static t_str _set_line_edit_feed(t_line_state *state, t_str *res)
{
*res = line_edit_feed(state);
return (*res);
}
/* This just implements a blocking loop for the multiplexed API.
* In many applications that are not event-drivern, we can just call
@ -38,7 +33,7 @@ t_str line_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt)
t_str res;
line_edit_start(&state, stdin_fd, stdout_fd, prompt);
while (_set_line_edit_feed(&state, &res) == get_unfinished_str())
while (!line_edit_feed(&state, &res))
;
line_edit_stop(&state);
return (res);
@ -54,7 +49,7 @@ t_str linenoise(t_const_str prompt)
t_str retval;
if (!isatty(get_stdin()->fd))
return (line_no_tty_impl());
return (line_no_tty_impl(&retval), retval);
retval = line_blocking_edit(get_stdin(), get_stdout(), prompt);
return (retval);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:26:32 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:27:56 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 14:32:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -45,8 +45,7 @@
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use
* STDIN_FILENO and STDOUT_FILENO.
*/
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
t_const_str prompt)
t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt)
{
if (stdin_fd == NULL)
stdin_fd = get_stdin();
@ -89,27 +88,29 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd,
*
* Some other errno: I/O error.
*/
t_str line_edit_feed(t_line_state *state)
bool line_edit_feed(t_line_state *state, t_str *out)
{
char c;
t_isize nread;
char seq[3];
t_vec_str *history;
t_str tmp;
char c;
t_isize nread;
char seq[3];
t_vec_str *history;
t_str tmp;
if (out == NULL)
return (true);
if (!isatty(state->input_fd->fd))
return (line_no_tty_impl());
return (line_no_tty_impl(out));
history = get_history();
if (read_fd(state->input_fd, (t_u8 *)&c, 1, &nread))
return (NULL);
return (*out = NULL, true);
if (c == K_NEWLINE || c == K_ENTER)
{
if (!vec_str_pop(history, &tmp))
mem_free(tmp);
return (str_clone(state->buf.buf));
return (*out = str_clone(state->buf.buf), true);
}
else if (c == K_CTRL_C)
return (errno = EAGAIN, NULL);
return (errno = EAGAIN, *out = NULL, NULL);
else if (c == K_BACKSPACE || c == K_CTRL_H)
line_edit_backspace(state);
else if (c == K_CTRL_D)
@ -120,7 +121,7 @@ t_str line_edit_feed(t_line_state *state)
{
history->len--;
mem_free(history->buffer[history->len]);
return (errno = ENOENT, NULL);
return (errno = ENOENT, *out = NULL, true);
}
}
else if (c == K_CTRL_B)
@ -131,18 +132,20 @@ t_str line_edit_feed(t_line_state *state)
line_edit_history_next(state, HIST_PREV);
else if (c == K_CTRL_N)
line_edit_history_next(state, HIST_PREV);
else if (c == K_SIGQUIT)
return (false);
else if (c == K_ESC)
{
if (read_fd(state->input_fd, (t_u8 *)seq, 1, NULL))
return ((t_str)get_unfinished_str());
return (false);
if (read_fd(state->input_fd, (t_u8 *)(seq + 1), 1, NULL))
return ((t_str)get_unfinished_str());
return (false);
if (seq[0] == '[')
{
if (seq[1] >= '0' && seq[1] <= '9')
{
if (read_fd(state->input_fd, (t_u8 *)(seq + 2), 1, NULL))
return ((t_str)get_unfinished_str());
return (false);
if (seq[1] == '3' && seq[2] == '~')
line_edit_delete(state);
}
@ -191,18 +194,18 @@ t_str line_edit_feed(t_line_state *state)
line_refresh_line(state);
}
else if (line_edit_insert(state, c))
return (NULL);
return ((t_str)get_unfinished_str());
return (*out = NULL, true);
return (false);
}
/* This is part of the multiplexed linenoise API. See linenoiseEditStart()
* for more information. This function is called when linenoiseEditFeed()
* returns something different than NULL. At this point the user input
* is in the buffer, and we can restore the terminal in normal mode. */
void line_edit_stop(t_line_state *state)
void line_edit_stop(t_line_state *state)
{
if (!isatty(state->input_fd->fd))
return ;
return;
line_disable_raw_mode(state->input_fd);
me_printf_fd(state->output_fd, "\n");
string_free(state->buf);

View file

@ -6,37 +6,37 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:10:24 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:15:42 by maiboyer ### ########.fr */
/* Updated: 2024/07/20 13:51:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "line/_line_internal.h"
#include "line/_line_functions.h"
#include "line/_line_internal.h"
#include "line/_line_structs.h"
#include "me/types.h"
/* This function is called when linenoise() is called with the standard
* input file descriptor not attached to a TTY. So for example when the
* program using linenoise is called in pipe or with a file redirected
* to its standard input. In this case, we want to be able to return the
* line regardless of its length (by default we are limited to 4k). */
t_str line_no_tty_impl(void)
bool line_no_tty_impl(t_str *out)
{
t_string line;
t_isize ret;
char chr;
t_string line;
t_isize ret;
char chr;
line = string_new(16);
while (true)
{
chr = '\n';
if (read_fd(get_stdin(), (t_u8 *)&chr, 1, &ret))
return (string_free(line), NULL);
return (string_free(line), *out = NULL, true);
if (ret == 0 || chr == '\n')
{
if (line.len == 0)
return (string_free(line), NULL);
return (line.buf);
return (string_free(line), *out = NULL, true);
return (*out = line.buf, true);
}
else
string_push_char(&line, chr);