Add the bare bones of the new input system
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 4 Mar 2024 20:54:55 +0000 (21:54 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 15 Dec 2024 08:02:00 +0000 (09:02 +0100)
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
inc/FET_sim.h
src/main.c

diff --git a/TODO b/TODO
index 91dfe5cc496d7b409859b15f69eaa523dc8c35c4..4a9f33bf536f2b60a9aeef2470a01e96142a46b6 100644 (file)
--- 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
index eee311d7aca78f5e3368966ed08e469f170b1e70..263197dfc74311696b458e17abaaf56ce3cc9eb3 100644 (file)
@@ -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);
index bfcdeb4ae94a6dd027b7086d6434edbfcf164f10..746a3d22a53f90f7b48c40fd16cc61a336ca32f2 100644 (file)
@@ -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);
 }