From 37fda5ceb4356f4115e974eec15c53801d896376 Mon Sep 17 00:00:00 2001
From: anon <anon@anon.anon>
Date: Sun, 22 Sep 2024 00:11:50 +0200
Subject: [PATCH] resizing code which does not work

---
 documentation/BUGS.md |  2 ++
 documentation/TODO.md |  4 +++-
 source/tui.c          | 47 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/documentation/BUGS.md b/documentation/BUGS.md
index 8d487b4..3fc11e4 100644
--- a/documentation/BUGS.md
+++ b/documentation/BUGS.md
@@ -1,2 +1,4 @@
 + refreshing can go wrong, characters being left over in what should be open space,
 i have no clue how, when or why it happens; seems like something i can live with
++ readline calling `return_input_available()` will SIGILL in debug builds;
+as far as im aware, its gcc 13 being retarded on nested functions
diff --git a/documentation/TODO.md b/documentation/TODO.md
index 44f9a2b..e0409d3 100644
--- a/documentation/TODO.md
+++ b/documentation/TODO.md
@@ -1,2 +1,4 @@
-+ resizing
++ resizing -- i added the code, the case seem to never execute;
+i have no clue why, and not being able to create debug builds is not helping,
+this will have to wait until gcc gets fixed
 + unicode support in the tui
diff --git a/source/tui.c b/source/tui.c
index 93905f3..64eedb6 100644
--- a/source/tui.c
+++ b/source/tui.c
@@ -52,6 +52,9 @@ static int input;
 //
 static inline void update_input(void);
 static void full_redraw(void);
+static void resize(void);
+static inline void init_windows();
+static inline void deinit_windows();
 
 static bool do_fullredraw = true;
 
@@ -64,13 +67,7 @@ int init_tui(void) {
     curs_set(0);
     keypad(stdscr, TRUE);
 
-    entry_lines = LINES-3;
-
-	main_window    = newwin(LINES-1, COLS,       0, 0);
-    input_window   = newwin(      1, COLS, LINES-1, 0);
-    entry_window   = subwin(main_window, entry_lines,                 COLS-2, 1, 1);
-    version_window = subwin(main_window,           1, strlen(version_string), 0, 5);
-    refresh();
+    init_windows();
 
     // Readline
     rl_bind_key('\t', rl_insert);
@@ -108,6 +105,7 @@ int init_tui(void) {
 }
 
 int deinit_tui(void) {
+    deinit_windows();
 	endwin();
     return 0;
 }
@@ -145,6 +143,25 @@ void tui_append_back(const entry_t entry) {
     ++entry_line_index;
 }
 
+static inline
+void init_windows() {
+    entry_lines = LINES-3;
+
+	main_window    = newwin(LINES-1, COLS,       0, 0);
+    input_window   = newwin(      1, COLS, LINES-1, 0);
+    entry_window   = subwin(main_window, entry_lines,                 COLS-2, 1, 1);
+    version_window = subwin(main_window,           1, strlen(version_string), 0, 5);
+    refresh();
+}
+
+static inline
+void deinit_windows() {
+    delwin(entry_window);
+    delwin(version_window);
+    delwin(main_window);
+    delwin(input_window);
+}
+
 static
 void full_redraw(void) {
     box(main_window, 0, 0);
@@ -157,8 +174,11 @@ void full_redraw(void) {
     doupdate();
 }
 
-void tui_rearm() {
-    entry_line_index = 0;
+static
+void resize(void) {
+    deinit_windows();
+    init_windows();
+    full_redraw();
 }
 
 static inline
@@ -184,6 +204,10 @@ void update_input() {
     wnoutrefresh(input_window);
 }
 
+void tui_rearm() {
+    entry_line_index = 0;
+}
+
 void tui_refresh(void) {
     // XXX: this is dirty
     if (selection_relative > last_entry_line_index-1) {
@@ -212,6 +236,7 @@ void tui_refresh(void) {
     doupdate();
 }
 
+#include <stdlib.h>
 
 void tui_take_input(void) {
     extern bool do_run;
@@ -286,6 +311,10 @@ void tui_take_input(void) {
         case '\r': {
             do_run = false;
         } break;
+        case KEY_RESIZE: {
+            //flushinp();
+            resize();
+        } break;
         case ERR: break;
         default: {
             input_available = true;