dict @ f2200e22b05c3801b722cd46617e7bcf64538d17

feat: Add a more "refined" UI
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a88fc556f01258b3ea3de17980acb4881a84de81..63c9d1e916652e6435fac4afe837a34742833d9b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,3 +5,4 @@
 add_subdirectory(ext)
 add_subdirectory(importer)
 add_subdirectory(lib)
+add_subdirectory(dict)
diff --git a/dict/main.c b/dict/main.c
index b743e07a307618cbf522b7a770da37953dd605f2..98608ccc050f069605e61e62f4af3b65775cc990 100644
--- a/dict/main.c
+++ b/dict/main.c
@@ -10,21 +10,19 @@ #include "../lib/ui.h"
 #include "../lib/util.h"
 
 Data *data;
+WINDOW* tpanel;
+PANEL* panel;
 
 void search(char*, int);
-int run(const char*, const char*);
+int run(const char*);
 
 int main(int argc, char** argv)
 {
     int opt;
-    char* txt = NULL;
     char* db = NULL;
 
     while ((opt = getopt(argc, argv, "t:d:h")) != -1) {
         switch(opt) {
-        case 't':
-            txt = copy_achar(optarg);
-            break;
         case 'd':
             db = copy_achar(optarg);
             break;
@@ -36,21 +34,19 @@             goto end;
         }
     }
 
-    int r = run(db, txt);
+    int r = run(db);
 
 end:
-    if (txt != NULL)
-        free(txt);
     if (db != NULL)
         free(db);
 
     return r;
 }
 
-int run(const char *db, const char *txt)
+int run(const char *db)
 {
-    data = new_data(db);
-    bootstrap(data);
+    data = new_data(":memory:");
+    load_or_save_db(data->db, db, 0);
 
     setlocale(LC_ALL, "");
     initscr();
@@ -58,34 +54,14 @@     noecho();
     cbreak();
     keypad(stdscr, TRUE);
 
-    FILE *f = fopen(txt, "r");
-    unsigned int lines = count_file_lines(f);
-    fseek(f, 0, SEEK_SET);
+    WINDOW* tbox = newwin(3,COLS,0,0);
+    TEXT_BOX *box = new_text_box(tbox, 100);
 
-    char * line = NULL;
-    size_t len = 0;
-    ssize_t read;
-    PROGRESS_BAR *bar = new_progress_bar(stdscr, lines);
-    while ((read = getline(&line, &len, f)) != -1) {
-        if (line[0] == '#' || line[0] == '\n')
-            continue;
+    tpanel = newwin(LINES-3, COLS, 3,0);
+    panel = new_panel(tpanel);
 
-        insert(data, line, read-1);
-        bar_step(bar, 1);
-    }
-
-    move(2,0);
-    printw("Saving db...");
-    refresh();
-    load_or_save_db(data->db, "backup.db", 1);
-
-    clear();
-    refresh();
-
-    TEXT_BOX *box = new_text_box(stdscr, 100);
     get_char(box, search);
 
-    clear();
     refresh();
 
     endwin();
@@ -100,13 +76,12 @@     char s[len+2];
 
     sprintf(s, "%%%*s%%", len, sch);
 
-    LIST* l = data_select(data, s, len+2);
+    LIST* l = data_select(data, s, len+2, LINES-5);
 
-    for (int y = 1; y < 20; y++) {
+    for (int y = 0; y < (LINES-5); y++) {
         move(y, 0);
         Word *item = (Word*)list_get(l, y);
         if (item != NULL)
-            printw("%s", item->Line);
+            write_char(panel, y, (char*)item->Line);
     }
-    refresh();
 }
diff --git a/importer/main.c b/importer/main.c
index b83e85086c5fac3b57c0fb27dfd4e7fd041282c8..087fc48ae3baeae869ddd9d6631cfa5c17fc7b93 100644
--- a/importer/main.c
+++ b/importer/main.c
@@ -67,10 +67,8 @@
         insert(data, line, read-1);
         count++;
 
-        if ((count % 321) == 0) {
-            float t = ((float)count/(float)total)*100;
-            printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
-        }
+        float t = ((float)count/(float)total)*100;
+        printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
     }
 
     float t = ((float)count/(float)total)*100;
diff --git a/lib/data.c b/lib/data.c
index e94e0a777c5de6b40530247fecb6942c985e803d..1c1a754f5260b5e15d9f532ad9667689f74f387f 100644
--- a/lib/data.c
+++ b/lib/data.c
@@ -3,9 +3,10 @@ #include <stdlib.h>
 #include <stdio.h>
 
 #include "data.h"
+#include "../lib/util.h"
 
 const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);";
-const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT 10;";
+const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT $NNN;";
 const char *create_table =  "CREATE TABLE IF NOT EXISTS words (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);";
 
 Data* new_data(const char* con)
@@ -80,7 +81,7 @@
     sqlite3_finalize(stmt);
 }
 
-LIST* data_select(Data* data, char *sch, int len)
+LIST* data_select(Data* data, char *sch, int len, int limit)
 {
     sqlite3_stmt *stmt;
     int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
@@ -95,6 +96,7 @@
     LIST *list = NULL;
 
     sqlite3_bind_text(stmt, 1, sch, len, NULL);
+    sqlite3_bind_int(stmt, 2, limit);
 
     int m =  sqlite3_step(stmt);
     while(m == SQLITE_ROW) {
diff --git a/lib/data.h b/lib/data.h
index fcde55ada917c1c87850044c1bb79b859d6762bf..6c9f30ff6a9d696354578a9af07c8a8e02db881a 100644
--- a/lib/data.h
+++ b/lib/data.h
@@ -39,7 +39,7 @@
 /*
  * Select all words.
  */
-LIST* data_select(Data*, char*, int);
+LIST* data_select(Data*, char*, int, int);
 
 /*
  * Print result code from sqlite.
diff --git a/lib/ui.c b/lib/ui.c
index 3eae2017ce609363744cc85f4b98c797b63e35fc..cd54cd409739ba617cc3ced65e3840ce64ee90d8 100644
--- a/lib/ui.c
+++ b/lib/ui.c
@@ -56,6 +56,7 @@     text->length = length;
     text->current = 0;
     text->text = malloc(sizeof(char)*(length+1));
     memset(text->text, '\0', length);
+    box(scr, 0,0);
     return text;
 }
 
@@ -82,8 +83,23 @@         char str[text->length];
         wcstombs(str, text->text, sizeof(text->text));
         sch(str, (int)strlen(str));
 
-        move(0,0);
+        wmove(text->scr,1,1);
+        wprintw(text->scr, "%*ls", text->current,text->text);
         wrefresh(text->scr);
-        wprintw(text->scr, "%*ls", text->current,text->text);
     }
 }
+
+PANEL* new_panel(WINDOW* scr)
+{
+    PANEL *panel = (PANEL*)malloc(sizeof(PANEL));
+    panel->scr = scr;
+    box(scr, 0,0);
+    return panel;
+}
+void write_char(PANEL* panel, int l, char *text)
+{
+    int x = getmaxx(panel->scr);
+    wmove(panel->scr, l+1, 1);
+    wprintw(panel->scr, "%.*s", x-3, text);
+    wrefresh(panel->scr);
+}
diff --git a/lib/ui.h b/lib/ui.h
index 12ee2f4932a6fccf18ea2d71a7694baee3e4ea88..271105ae2c3e97e61eae9831a863fd9c4fc3638f 100644
--- a/lib/ui.h
+++ b/lib/ui.h
@@ -21,3 +21,12 @@ } TEXT_BOX;
 
 TEXT_BOX* new_text_box(WINDOW*, int);
 void get_char(TEXT_BOX* text, void (*sch)(char*, int));
+
+typedef struct panel
+{
+    WINDOW *scr;
+} PANEL;
+
+
+PANEL* new_panel(WINDOW*);
+void write_char(PANEL*, int, char*);