Fix "minor" issues
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 11 Mar 2024 13:03:02 +0000 (14:03 +0100)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 11 Mar 2024 13:03:02 +0000 (14:03 +0100)
Fix off by one error caused by using the whole terminal height instead
of the height used by the table (without the header).
Add missing initialization.
Fix terminal position indexing as it is 1 based (not 0 based).
Handle newline after printing entry separately in the loop mode as when
the entry at the very bottom of the terminal emits newline the
whole output shifts (erasing header).

src/loop.c
src/main.c

index 0cbd3bf57fda912135660e2c5cafdd1ebcbf8400..aa30e73e660e59a6d9ba796920da47dc76640c29 100644 (file)
@@ -19,6 +19,8 @@ void  init_term(t_term *term)
        new.c_cc[VTIME] = 10;
        tcsetattr(STDIN_FILENO, TCSANOW, &new);
        ft_putstr_fd(term_setup, STDOUT_FILENO);
+       term->top_row = 0;
+       term->y = 1;
        return ;
 }
 
@@ -53,7 +55,8 @@ void  get_terminal_size(t_term *term)
 
        ft_putstr_fd(term_size_str, STDOUT_FILENO);
        if (read(STDIN_FILENO, buffer, TERMSIZE_BUFFER_SIZE) < 0)
-       term->height = ft_atoi(&buffer[2]);
+               return ;
+       term->height = ft_atoi(&buffer[2]) - 1;
        i = 0;
        while (buffer[i] != ';')
                ++i;
@@ -65,7 +68,7 @@ void  get_terminal_size(t_term *term)
 
 static const char      *print_init_str = ""
        "\033[s"
-       "\033[1;0H"
+       "\033[2;1H"
        ;
 
 void   print_noncanon(t_vec *database, t_term *term)
@@ -76,7 +79,10 @@ void print_noncanon(t_vec *database, t_term *term)
        ft_putstr_fd(print_init_str, STDOUT_FILENO);
        while (i < database->size && i - term->top_row < term->height)
        {
-               print_entry(ft_vec_access(database, i++));
+               print_entry(ft_vec_access(database, i));
+               if (i - term->top_row < term->height - 1)
+                       ft_putchar_fd('\n', STDOUT_FILENO);
+               ++i;
        }
        ft_putstr_fd(restore_cursor, STDOUT_FILENO);
        return ;
@@ -96,7 +102,7 @@ int  handle_buffer(const char *buffer, t_vec *database, t_term *term)
        }
        else if (!ft_strcmp(buffer, "\033[B"))
        {
-               if (term->y < term->height)
+               if (term->y < term->height && term->y < database->size - term->top_row)
                {
                        ++term->y;
                        ft_putstr_fd(buffer, STDOUT_FILENO);
@@ -104,8 +110,8 @@ int handle_buffer(const char *buffer, t_vec *database, t_term *term)
                else if (database->size - term->top_row > term->height)
                        ++term->top_row;
        }
-       else if (!ft_strcmp(buffer, "k") && term->top_row + term->y < database->size)
-               kill_entry(database, term->top_row + term->y);
+       else if (!ft_strcmp(buffer, "k") && term->top_row + term->y - 1 < database->size)
+               kill_entry(database, term->top_row + term->y - 1);
        else if (!ft_strcmp(buffer, "q") || !ft_strcmp(buffer, "\033"))
                return (1);
        return (0);
index 84b0ed6ca4b0a4de09c2811efb7cc8cb85f4ddf9..07cbdd0e605dbef1f3cc7dc954c07008c70790a9 100644 (file)
@@ -64,7 +64,7 @@ void  print_entry(t_entry *entry)
 
        countdown = 3600 - (get_time_sec() - entry->kill_time);
        countdown = ft_max(0, countdown);
-       ft_printf("   %-20s%5i   %-20s%2i:%02i\n",
+       ft_printf("   %-20s%5i   %-20s%2i:%02i",
                        entry->name,
                        entry->level,
                        entry->biome,
@@ -73,13 +73,20 @@ void        print_entry(t_entry *entry)
        return ;
 }
 
+void   print_entry_nl(t_entry *entry)
+{
+       print_entry(entry);
+       ft_putchar_fd('\n', STDOUT_FILENO);
+       return ;
+}
+
 void   print_database(t_vec *database)
 {
        size_t                  i;
 
        i = 0;
        while (i < database->size)
-               print_entry(ft_vec_access(database, i++));
+               print_entry_nl(ft_vec_access(database, i++));
        return ;
 }
 
@@ -283,7 +290,7 @@ void        carry_out_command(t_command *com, t_vec *database)
                if (com->entry.name == NULL)
                        print_database(database);
                else
-                       print_entry(ft_vec_access(database, ind));
+                       print_entry_nl(ft_vec_access(database, ind));
        }
        else if (com->basic == basic_help)
                print_help();