dict @ 3967b61dfbee50d2072ddefced877486ce439582

feat: Add buggy ui component
  1diff --git a/data.c b/data.c
  2index 777dcdf58b260a0ecf8c50fb56132b34d0a6c35d..81ee9f31356c495fe4754f9105a3211f863e9956 100644
  3--- a/data.c
  4+++ b/data.c
  5@@ -70,7 +70,7 @@
  6     sqlite3_finalize(stmt);
  7 }
  8 
  9-LIST* select(Data* data) {
 10+LIST* data_select(Data* data) {
 11     sqlite3_stmt *stmt; 
 12     int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
 13 
 14diff --git a/data.h b/data.h
 15index 393f8308c1ccf9032745260ee3ef84b9d5af3462..8bc30e9396446df274a6dd39022d5e32f5d42033 100644
 16--- a/data.h
 17+++ b/data.h
 18@@ -39,7 +39,7 @@
 19 /*
 20  * Select all words.
 21  */
 22-LIST* select(Data*);
 23+LIST* data_select(Data*);
 24 
 25 /*
 26  * Print result code from sqlite.
 27diff --git a/main.c b/main.c
 28index dae1f2aa50de122aa0d00c8cfec74b3f939a5bfc..69a68b8de4df79435dead3ef0535736d7c6422e0 100644
 29--- a/main.c
 30+++ b/main.c
 31@@ -3,6 +3,7 @@ #include <stdio.h>
 32 #include <sqlite3.h>
 33 #include <ncurses.h>
 34 #include "data.h"
 35+#include "ui.h"
 36 
 37 #define BUF_SIZE 100
 38 
 39@@ -10,14 +11,13 @@ unsigned int count_lines(FILE* file);
 40 int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave);
 41 
 42 int main() {
 43-    Data *data = new_data(":memory:");
 44-
 45-    bootstrap(data);
 46-
 47     setlocale(LC_ALL, "");
 48-    initscr();
 49+    initscr(); 
 50+    noecho();
 51+    cbreak();
 52 
 53-    int maxx=getmaxx(stdscr);
 54+    Data *data = new_data(":memory:");
 55+    bootstrap(data);
 56 
 57     FILE *f = fopen("dict.txt", "r");
 58     unsigned int lines = count_lines(f);
 59@@ -26,22 +26,13 @@
 60     char * line = NULL;
 61     size_t len = 0;
 62     ssize_t read;
 63-    int count = 0;
 64+    PROGRESS_BAR *bar = new_progress_bar(stdscr, lines);
 65     while ((read = getline(&line, &len, f)) != -1) {
 66         if (line[0] == '#' || line[0] == '\n')
 67             continue;
 68 
 69         insert(data, line, read-1);
 70-        count ++;
 71-        move(0,0);
 72-        float total = ((float)count/(float)lines);
 73-        printw("%03.0f%% ", total*100);
 74-        for (int x = 0; x < ((maxx-4)*total); x++) {
 75-            printw("█");
 76-        }
 77-        move(1,0);
 78-        printw("%d/%d",count,lines);
 79-        refresh();
 80+        bar_step(bar, 1);
 81     }
 82 
 83     move(2,0);
 84@@ -51,6 +42,9 @@     load_or_save_db(data->db, "backup.db", 1);
 85 
 86     clear();
 87     refresh();
 88+    endwin();
 89+
 90+    free_data(data);
 91     return 0;
 92 }
 93 
 94diff --git a/ui.c b/ui.c
 95new file mode 100644
 96index 0000000000000000000000000000000000000000..8837bab19ca394e22205abe5312b37350ddad5e5
 97--- /dev/null
 98+++ b/ui.c
 99@@ -0,0 +1,63 @@
100+#include "math.h"
101+#include <ncurses.h>
102+#include <stdlib.h>
103+#include "ui.h"
104+
105+const char *uload = "█";
106+
107+
108+PROGRESS_BAR* new_progress_bar(WINDOW* scr, float total) {
109+    PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR));
110+    bar->scr = scr;
111+    bar->total = total;
112+    bar->current = 0;
113+
114+    int x, y;
115+    int hx, hy;
116+
117+    getmaxyx(scr, y, x);
118+
119+    hx = x/2;
120+    hy = y/2;
121+
122+    wmove(scr, hy-1, 0);
123+    wprintw(scr, uload);
124+
125+    wmove(scr, hy, hx-4);
126+    wprintw(scr, "000%%");
127+
128+    wmove(scr, hy+1, hx);
129+    wprintw(scr, "%.0f/%.0f", 0.0, total);
130+
131+    return bar;
132+}
133+
134+void bar_step(PROGRESS_BAR* bar, float step){
135+    bar->current += step;
136+
137+    int x, y;
138+    int hx, hy;
139+
140+    getmaxyx(bar->scr, y, x);
141+
142+    hx = x/2;
143+    hy = y/2;
144+
145+    float total = (bar->current/bar->total);
146+
147+    wmove(bar->scr, hy-1, 0);
148+    for (int i = 0; i < ((float)x*total); i++)
149+        wprintw(bar->scr, uload);
150+
151+    wmove(bar->scr, hy, hx-4);
152+    wprintw(bar->scr,"%03.0f%% ", total*100);
153+
154+    int len = floor(log10(abs((int)bar->total))) + 3;
155+
156+    wmove(bar->scr, hy+1, hx - len);
157+    wprintw(bar->scr, "%.0f/%.0f", bar->current, bar->total);
158+
159+
160+    wmove(bar->scr,0,0);
161+    wrefresh(bar->scr);
162+}
163diff --git a/ui.h b/ui.h
164new file mode 100644
165index 0000000000000000000000000000000000000000..8035d6f9955b77119212f3920905a615f49fc912
166--- /dev/null
167+++ b/ui.h
168@@ -0,0 +1,11 @@
169+#include <ncurses.h>
170+
171+typedef struct progress_bar {
172+    float total;
173+    float current;
174+    WINDOW *scr;
175+} PROGRESS_BAR;
176+
177+PROGRESS_BAR* new_progress_bar(WINDOW*, float);
178+
179+void bar_step(PROGRESS_BAR*, float);