renamed 'le' functions and created headers

This commit is contained in:
Maieul BOYER 2024-07-10 15:44:43 +02:00
parent 4303370d55
commit 46e96a2120
No known key found for this signature in database
6 changed files with 258 additions and 210 deletions

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_functions.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:23:25 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:38:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_FUNCTIONS_H
#define _LINE_FUNCTIONS_H
#include "line/_line_structs.h"
#include "me/fs/fs.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
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);
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);
void line_edit_move_end(t_line_state *state);
void line_edit_move_home(t_line_state *state);
void line_edit_move_left(t_line_state *state);
void line_edit_move_right(t_line_state *state);
void line_edit_stop(t_line_state *state);
t_str linenoise(t_const_str prompt);
t_str line_blocking_edit(t_fd *stdin_fd, t_fd *stdout_fd, t_const_str prompt);
bool line_history_add(t_const_str line);
t_error line_history_load(t_str name);
t_error line_history_save(t_str name);
void line_clear_screen(t_fd *output);
void line_refresh(t_line_state *state, t_line_flags flags);
void line_hide(t_line_state *state);
void line_show(t_line_state *state);
#endif /* _LINE_FUNCTIONS_H */

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_interal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:37:40 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:38:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_INTERAL_H
#define _LINE_INTERAL_H
#include "line/_line_functions.h"
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
t_const_str get_unfinished_str(void);
t_raw_mode_state *get_raw_mode_state(void);
t_vec_str *get_history(void);
void free_history(void);
t_error gnl_wrapper(t_fd *fd, t_string *out);
void line_uninit_lib(void);
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);
void line_print_key_codes(void);
t_error line_enable_raw_mode(t_fd *fd);
void line_disable_raw_mode(t_fd *fd);
#endif /* _LINE_INTERAL_H */

View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _line_structs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 15:24:34 by maiboyer #+# #+# */
/* Updated: 2024/07/10 15:32:35 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _LINE_STRUCTS_H
#define _LINE_STRUCTS_H
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
#include <termios.h>
typedef struct s_line_state t_line_state;
typedef struct s_raw_mode_state t_raw_mode_state;
typedef enum e_line_flags t_line_flags;
typedef enum e_key_action t_key_action;
typedef enum e_history_direction t_history_direction;
struct s_line_state
{
t_fd *input_fd; /* Terminal stdin file descriptor. */
t_fd *output_fd; /* Terminal stdout file descriptor. */
t_string buf; /* Edited line buffer. */
t_const_str prompt; /* Prompt to display. */
t_usize prompt_len; /* Prompt length. */
t_usize pos; /* Current cursor position. */
t_usize columns; /* Number of columns in terminal. */
t_i32 history_index; /* The history index we are currently editing. */
};
struct s_raw_mode_state
{
bool enabled;
struct termios state;
};
enum e_key_action
{
K_KEY_NULL = 0, /* NULL */
K_CTRL_A = 1, /* Ctrl+a */
K_CTRL_B = 2, /* Ctrl-b */
K_CTRL_C = 3, /* Ctrl-c */
K_CTRL_D = 4, /* Ctrl-d */
K_CTRL_E = 5, /* Ctrl-e */
K_CTRL_F = 6, /* Ctrl-f */
K_CTRL_H = 8, /* Ctrl-h */
K_TAB = 9, /* Tab */
K_NEWLINE = 10, /* Newline */
K_CTRL_K = 11, /* Ctrl+k */
K_CTRL_L = 12, /* Ctrl+l */
K_ENTER = 13, /* Enter */
K_CTRL_N = 14, /* Ctrl-n */
K_CTRL_P = 16, /* Ctrl-p */
K_CTRL_T = 20, /* Ctrl-t */
K_CTRL_U = 21, /* Ctrl+u */
K_CTRL_W = 23, /* Ctrl+w */
K_ESC = 27, /* Escape */
K_BACKSPACE = 127 /* Backspace */
};
enum e_line_flags
{
REFRESH_CLEAN = 1 << 0, // Clean the old prompt from the screen
REFRESH_WRITE = 1 << 1, // Rewrite the prompt on the screen.
REFRESH_ALL = REFRESH_CLEAN | REFRESH_WRITE, // Do both.
};
enum e_history_direction
{
HIST_PREV,
HIST_NEXT,
};
#endif /* _LINE_STRUCTS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:07:02 by maiboyer #+# #+# */
/* Updated: 2024/07/08 22:21:38 by maiboyer ### ########.fr */
/* Updated: 2024/07/10 15:28:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,59 +16,7 @@
#ifndef LINE_H
#define LINE_H
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
#include <termios.h>
extern t_str linenoiseEditMore;
typedef struct s_line_state t_line_state;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct s_line_state
{
t_fd *input_fd; /* Terminal stdin file descriptor. */
t_fd *output_fd; /* Terminal stdout file descriptor. */
t_string buf; /* Edited line buffer. */
t_const_str prompt; /* Prompt to display. */
t_usize prompt_len; /* Prompt length. */
t_usize pos; /* Current cursor position. */
t_usize old_pos; /* Previous refresh cursor position. */
t_usize columns; /* Number of columns in terminal. */
t_usize old_rows; /* Rows used by last refrehsed line (multiline mode) */
t_i32 history_index; /* The history index we are currently editing. */
};
struct s_raw_mode_state
{
bool enabled;
struct termios state;
};
typedef struct s_raw_mode_state t_raw_mode_state;
/* Non blocking API. */
int linenoiseEditStart(t_line_state *state, int stdin_fd, int stdout_fd, char *buf, t_usize buflen, const char *prompt);
char *linenoiseEditFeed(t_line_state *state);
void linenoiseEditStop(t_line_state *state);
void linenoiseHide(t_line_state *state);
void linenoiseShow(t_line_state *state);
/* Blocking API. */
char *linenoise(const char *prompt);
void linenoise_free(void *ptr);
/* History API. */
bool linenoise_history_add(t_const_str line);
t_error linenoiseHistorySave(t_str name);
t_error linenoise_history_load(t_str name);
/* Other utilities. */
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
#include "line/_line_functions.h"
#include "line/_line_structs.h"
#endif /* LINE_H */