Normed the line/src

This commit is contained in:
Raphael (rparodi) 2024-07-30 17:14:34 +02:00
parent c2dec0e9f7
commit 9b1574b9c4
5 changed files with 24 additions and 22 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */ /* Created: 2024/07/07 16:53:27 by maiboyer #+# #+# */
/* Updated: 2024/07/20 14:00:06 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 17:06:59 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,6 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <termios.h> #include <termios.h>
/* This just implements a blocking loop for the multiplexed API. /* This just implements a blocking loop for the multiplexed API.
* In many applications that are not event-drivern, we can just call * In many applications that are not event-drivern, we can just call
* the blocking linenoise API, wait for the user to complete the editing * the blocking linenoise API, wait for the user to complete the editing

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:26:32 by maiboyer #+# #+# */ /* Created: 2024/07/11 18:26:32 by maiboyer #+# #+# */
/* Updated: 2024/07/23 22:24:59 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 17:13:36 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,9 @@
* fails. If stdin_fd or stdout_fd are set to -1, the default is to use * fails. If stdin_fd or stdout_fd are set to -1, the default is to use
* STDIN_FILENO and STDOUT_FILENO. * 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) if (stdin_fd == NULL)
stdin_fd = get_stdin(); stdin_fd = get_stdin();
@ -76,7 +78,7 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_
* called in a loop, and block. * called in a loop, and block.
* *
* The function returns get_unfinished_str() to signal that line editing is still * The function returns get_unfinished_str() to signal that line editing is still
* in progress, that is, the user didn't yet pressed enter / CTRL-D. Otherwise * in progress, that is, the user didn't yet pressed enter / CTRL-D. Otherwise
* the function returns the pointer to the heap-allocated buffer with the * the function returns the pointer to the heap-allocated buffer with the
* edited line, that the user should mem_free with linenoiseFree(). * edited line, that the user should mem_free with linenoiseFree().
@ -88,13 +90,13 @@ t_error line_edit_start(t_line_state *state, t_fd *stdin_fd, t_fd *stdout_fd, t_
* *
* Some other errno: I/O error. * Some other errno: I/O error.
*/ */
bool line_edit_feed(t_line_state *state, t_str *out) bool line_edit_feed(t_line_state *state, t_str *out)
{ {
char c; char c;
t_isize nread; t_isize nread;
char seq[3]; char seq[3];
t_vec_str *history; t_vec_str *history;
t_str tmp; t_str tmp;
if (out == NULL) if (out == NULL)
return (true); return (true);
@ -202,11 +204,11 @@ bool line_edit_feed(t_line_state *state, t_str *out)
* for more information. This function is called when linenoiseEditFeed() * for more information. This function is called when linenoiseEditFeed()
* returns something different than NULL. At this point the user input * returns something different than NULL. At this point the user input
* is in the buffer, and we can restore the terminal in normal mode. */ * 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)
{ {
string_free(state->buf); string_free(state->buf);
if (!isatty(state->input_fd->fd)) if (!isatty(state->input_fd->fd))
return; return ;
line_disable_raw_mode(state->input_fd); line_disable_raw_mode(state->input_fd);
me_printf_fd(state->output_fd, "\n"); me_printf_fd(state->output_fd, "\n");
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:21:45 by maiboyer #+# #+# */ /* Created: 2024/07/11 18:21:45 by maiboyer #+# #+# */
/* Updated: 2024/07/20 15:33:01 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 17:07:59 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,7 +55,7 @@ t_usize line_get_prompt_len(t_const_str s)
if ('@' <= s[i] && s[i] <= '~') if ('@' <= s[i] && s[i] <= '~')
i++; i++;
} }
else else
_modify_vars(&i, &ret, true); _modify_vars(&i, &ret, true);
} }
return (ret); return (ret);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:18:46 by maiboyer #+# #+# */ /* Created: 2024/07/11 18:18:46 by maiboyer #+# #+# */
/* Updated: 2024/07/20 15:02:53 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 17:08:24 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,8 @@
/* Use the ESC [6n escape sequence to query the horizontal cursor position /* Use the ESC [6n escape sequence to query the horizontal cursor position
* and return it. On error -1 is returned, on success the position of the * and return it. On error -1 is returned, on success the position of the
* cursor. */ * cursor. */
/*t_error line_get_cursor_position(t_fd *input, t_fd *output, t_u32 *column_out) /*t_error line_get_cursor_position( \
t_fd *input, t_fd *output, t_u32 *column_out)
{ {
char buf[32]; char buf[32];
t_u32 i; t_u32 i;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 18:10:24 by maiboyer #+# #+# */ /* Created: 2024/07/11 18:10:24 by maiboyer #+# #+# */
/* Updated: 2024/07/20 13:51:27 by maiboyer ### ########.fr */ /* Updated: 2024/07/30 17:14:18 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,11 +20,11 @@
* program using linenoise is called in pipe or with a file redirected * 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 * 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). */ * line regardless of its length (by default we are limited to 4k). */
bool line_no_tty_impl(t_str *out) bool line_no_tty_impl(t_str *out)
{ {
t_string line; t_string line;
t_isize ret; t_isize ret;
char chr; char chr;
line = string_new(16); line = string_new(16);
while (true) while (true)