From e167b7a77149cd58de4d0c56cc529bcd09bfe7bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Mon, 4 Mar 2024 21:54:55 +0100 Subject: [PATCH] Add the bare bones of the new input system The system now returns what is returned and handles arrows. Backspace, up and down arrows and other functionality handling is to be yet implemented as well as the whole "schema" mode. --- TODO | 3 ++ inc/FET_sim.h | 12 +++++-- src/main.c | 92 +++++++++++++++++++++++++++++++++++---------------- 3 files changed, 76 insertions(+), 31 deletions(-) diff --git a/TODO b/TODO index 91dfe5c..4a9f33b 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,8 @@ TODO implement vim-esque modes and gdb-like split screen/window for commands and schematics (TUI?) implement new method of input - echoing commands, moving cursor and more + handle the behaviour better + sync a char *str_inp to what the user sees in terminal implement "split screen" - one for commands, one for schematics (gdb like) commands screen should work just similarly to normal terminal usage schematics should use vim like controls @@ -25,6 +27,7 @@ implement vim-esque modes and gdb-like split screen/window for commands and sche implement new method of input - echoing commands, moving cursor and more use alternate screen implement non-canonical mode + return input rewrite t_fet and t_node to store indexes insted of pointers to connected parts - goal shifted a bit, nodes store their ID and FETs have no use for havind ID so far foundations of logic diff --git a/inc/FET_sim.h b/inc/FET_sim.h index eee311d..263197d 100644 --- a/inc/FET_sim.h +++ b/inc/FET_sim.h @@ -49,6 +49,14 @@ typedef struct s_mosfet t_node *drain; } t_mosfet; +typedef enum e_mode +{ + mode_schema, + mode_command, + mode_exit, + mode_error, +} t_mode; + typedef enum e_command { none, @@ -99,8 +107,8 @@ void add_mosfet(t_vec *mosfets, t_type type); void bind_fet_node(t_mosfet *mosfet, t_node *node, t_terminal terminal); void free_node(void *node); -int process_input(t_vec *nodes, t_vec *mosfets, int fd); -int get_input(t_input *input, int fd); +t_mode process_input(t_vec *nodes, t_vec *mosfets, int fd); +t_mode get_input(t_input *input, int fd); const char *state_color_escape(t_state state); void draw_single(t_vec *mosfets, size_t i); diff --git a/src/main.c b/src/main.c index bfcdeb4..746a3d2 100644 --- a/src/main.c +++ b/src/main.c @@ -213,63 +213,89 @@ int construct_input(t_input *input, char **split_inp) return (1); } -int get_input(t_input *input, int fd) +t_mode process_input_string(t_input *input, char *str_inp) { - int res; - char *str_inp; char **split_inp; - str_inp = get_next_line(fd); - if (!str_inp) - { - input->command = exitsim; - input->argc = 0; - return (1); - } if (str_inp[0] == '\n') { free(str_inp); - return (1); + return (mode_command); } if (str_inp[0] && str_inp[ft_strlen(str_inp) - 1] == '\n') str_inp[ft_strlen(str_inp) - 1] = '\0'; split_inp = ft_split(str_inp, " "); - res = construct_input(input, split_inp); - if (!res) + if (!construct_input(input, split_inp)) { command_not_found(str_inp); } - free(str_inp); free_split(split_inp); + return (mode_command); +} + +int handle_escape_code(__attribute__((unused)) t_input *input, __attribute__((unused)) const char *code) +{ + return (1); +} + +t_mode get_input(t_input *input, int fd) +{ + int res; + static char *str_inp; + char buff[10]; + size_t len; + + while(1) + { + len = read(fd, buff, 10); + buff[len] = '\0'; + if (buff[0] == '\033' && len == 1) + return (mode_schema); + else if (buff[0] == '\033') + { + if (handle_escape_code(input, buff)) + ft_printf("%s", buff); + } + else + ft_printf("%s", buff); + } + if (!str_inp) + return (mode_exit); + process_input_string(input, str_inp); + free(str_inp); return (res); } -int process_input(t_vec *nodes, t_vec *mosfets, int fd) +t_mode process_input(t_vec *nodes, t_vec *mosfets, int fd) { - int res; + t_mode res; + int success; static t_input input = {.command = help, .argc = 0}; ft_printf("FET_sim> "); - if (!get_input(&input, fd)) - return (1); - res = 1; + res = get_input(&input, fd); + if (res != mode_command) + return (res); + success = 1; if (input.command == next) - res = c_next(input, nodes, mosfets); + success = c_next(input, nodes, mosfets); else if (input.command == draw) - res = c_draw(input, mosfets); + success = c_draw(input, mosfets); else if (input.command == setnode) - res = c_setnode(input, nodes); + success = c_setnode(input, nodes); else if (input.command == addnode) - res = c_addnode(input, nodes); + success = c_addnode(input, nodes); else if (input.command == addfet) - res = c_addfet(input, mosfets); + success = c_addfet(input, mosfets); else if (input.command == bind) - res = c_bind(input, nodes, mosfets); + success = c_bind(input, nodes, mosfets); else if (input.command == help) - res = c_help(input); + success = c_help(input); else if (input.command == exitsim) - res = 0; - return (res); + res = mode_exit; + if (success) + return (res); + return (mode_error); } void cleanup(t_vec *nodes, t_vec *mosfets) @@ -286,6 +312,7 @@ int main(int argc, char **argv) { t_vec nodes; t_vec mosfets; + t_mode mode; setup_terminal(); print_start(); @@ -293,7 +320,14 @@ int main(int argc, char **argv) ft_vec_init(&mosfets, sizeof(t_mosfet)); if (argc > 1) build_graph(argv[1], &nodes, &mosfets); - while (process_input(&nodes, &mosfets, STDIN_FILENO)); + mode = mode_command; + while (mode != mode_exit && mode != mode_error) + { + if (mode == mode_command) + mode = process_input(&nodes, &mosfets, STDIN_FILENO); +// else if (mode == mode_schema) +// mode = schema_mode(&nodes, &mosfets, STDIN_FILENO); + } cleanup(&nodes, &mosfets); return (0); } -- 2.30.2