dict @ f2200e22b05c3801b722cd46617e7bcf64538d17

feat: Add a more "refined" UI
  1diff --git a/CMakeLists.txt b/CMakeLists.txt
  2index a88fc556f01258b3ea3de17980acb4881a84de81..63c9d1e916652e6435fac4afe837a34742833d9b 100644
  3--- a/CMakeLists.txt
  4+++ b/CMakeLists.txt
  5@@ -5,3 +5,4 @@
  6 add_subdirectory(ext)
  7 add_subdirectory(importer)
  8 add_subdirectory(lib)
  9+add_subdirectory(dict)
 10diff --git a/dict/main.c b/dict/main.c
 11index b743e07a307618cbf522b7a770da37953dd605f2..98608ccc050f069605e61e62f4af3b65775cc990 100644
 12--- a/dict/main.c
 13+++ b/dict/main.c
 14@@ -10,21 +10,19 @@ #include "../lib/ui.h"
 15 #include "../lib/util.h"
 16 
 17 Data *data;
 18+WINDOW* tpanel;
 19+PANEL* panel;
 20 
 21 void search(char*, int);
 22-int run(const char*, const char*);
 23+int run(const char*);
 24 
 25 int main(int argc, char** argv)
 26 {
 27     int opt;
 28-    char* txt = NULL;
 29     char* db = NULL;
 30 
 31     while ((opt = getopt(argc, argv, "t:d:h")) != -1) {
 32         switch(opt) {
 33-        case 't':
 34-            txt = copy_achar(optarg);
 35-            break;
 36         case 'd':
 37             db = copy_achar(optarg);
 38             break;
 39@@ -36,21 +34,19 @@             goto end;
 40         }
 41     }
 42 
 43-    int r = run(db, txt);
 44+    int r = run(db);
 45 
 46 end:
 47-    if (txt != NULL)
 48-        free(txt);
 49     if (db != NULL)
 50         free(db);
 51 
 52     return r;
 53 }
 54 
 55-int run(const char *db, const char *txt)
 56+int run(const char *db)
 57 {
 58-    data = new_data(db);
 59-    bootstrap(data);
 60+    data = new_data(":memory:");
 61+    load_or_save_db(data->db, db, 0);
 62 
 63     setlocale(LC_ALL, "");
 64     initscr();
 65@@ -58,34 +54,14 @@     noecho();
 66     cbreak();
 67     keypad(stdscr, TRUE);
 68 
 69-    FILE *f = fopen(txt, "r");
 70-    unsigned int lines = count_file_lines(f);
 71-    fseek(f, 0, SEEK_SET);
 72+    WINDOW* tbox = newwin(3,COLS,0,0);
 73+    TEXT_BOX *box = new_text_box(tbox, 100);
 74 
 75-    char * line = NULL;
 76-    size_t len = 0;
 77-    ssize_t read;
 78-    PROGRESS_BAR *bar = new_progress_bar(stdscr, lines);
 79-    while ((read = getline(&line, &len, f)) != -1) {
 80-        if (line[0] == '#' || line[0] == '\n')
 81-            continue;
 82+    tpanel = newwin(LINES-3, COLS, 3,0);
 83+    panel = new_panel(tpanel);
 84 
 85-        insert(data, line, read-1);
 86-        bar_step(bar, 1);
 87-    }
 88-
 89-    move(2,0);
 90-    printw("Saving db...");
 91-    refresh();
 92-    load_or_save_db(data->db, "backup.db", 1);
 93-
 94-    clear();
 95-    refresh();
 96-
 97-    TEXT_BOX *box = new_text_box(stdscr, 100);
 98     get_char(box, search);
 99 
100-    clear();
101     refresh();
102 
103     endwin();
104@@ -100,13 +76,12 @@     char s[len+2];
105 
106     sprintf(s, "%%%*s%%", len, sch);
107 
108-    LIST* l = data_select(data, s, len+2);
109+    LIST* l = data_select(data, s, len+2, LINES-5);
110 
111-    for (int y = 1; y < 20; y++) {
112+    for (int y = 0; y < (LINES-5); y++) {
113         move(y, 0);
114         Word *item = (Word*)list_get(l, y);
115         if (item != NULL)
116-            printw("%s", item->Line);
117+            write_char(panel, y, (char*)item->Line);
118     }
119-    refresh();
120 }
121diff --git a/importer/main.c b/importer/main.c
122index b83e85086c5fac3b57c0fb27dfd4e7fd041282c8..087fc48ae3baeae869ddd9d6631cfa5c17fc7b93 100644
123--- a/importer/main.c
124+++ b/importer/main.c
125@@ -67,10 +67,8 @@
126         insert(data, line, read-1);
127         count++;
128 
129-        if ((count % 321) == 0) {
130-            float t = ((float)count/(float)total)*100;
131-            printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
132-        }
133+        float t = ((float)count/(float)total)*100;
134+        printf("\rLoading data [%03.0f%%] %d/%d", t, count, total);
135     }
136 
137     float t = ((float)count/(float)total)*100;
138diff --git a/lib/data.c b/lib/data.c
139index e94e0a777c5de6b40530247fecb6942c985e803d..1c1a754f5260b5e15d9f532ad9667689f74f387f 100644
140--- a/lib/data.c
141+++ b/lib/data.c
142@@ -3,9 +3,10 @@ #include <stdlib.h>
143 #include <stdio.h>
144 
145 #include "data.h"
146+#include "../lib/util.h"
147 
148 const char *insert_into = "INSERT INTO words (LINE) VALUES($VVV);";
149-const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT 10;";
150+const char *select_words = "SELECT Id, Line FROM words WHERE line like $VVV LIMIT $NNN;";
151 const char *create_table =  "CREATE TABLE IF NOT EXISTS words (ID INTEGER PRIMARY KEY AUTOINCREMENT, LINE TEXT NOT NULL);";
152 
153 Data* new_data(const char* con)
154@@ -80,7 +81,7 @@
155     sqlite3_finalize(stmt);
156 }
157 
158-LIST* data_select(Data* data, char *sch, int len)
159+LIST* data_select(Data* data, char *sch, int len, int limit)
160 {
161     sqlite3_stmt *stmt;
162     int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
163@@ -95,6 +96,7 @@
164     LIST *list = NULL;
165 
166     sqlite3_bind_text(stmt, 1, sch, len, NULL);
167+    sqlite3_bind_int(stmt, 2, limit);
168 
169     int m =  sqlite3_step(stmt);
170     while(m == SQLITE_ROW) {
171diff --git a/lib/data.h b/lib/data.h
172index fcde55ada917c1c87850044c1bb79b859d6762bf..6c9f30ff6a9d696354578a9af07c8a8e02db881a 100644
173--- a/lib/data.h
174+++ b/lib/data.h
175@@ -39,7 +39,7 @@
176 /*
177  * Select all words.
178  */
179-LIST* data_select(Data*, char*, int);
180+LIST* data_select(Data*, char*, int, int);
181 
182 /*
183  * Print result code from sqlite.
184diff --git a/lib/ui.c b/lib/ui.c
185index 3eae2017ce609363744cc85f4b98c797b63e35fc..cd54cd409739ba617cc3ced65e3840ce64ee90d8 100644
186--- a/lib/ui.c
187+++ b/lib/ui.c
188@@ -56,6 +56,7 @@     text->length = length;
189     text->current = 0;
190     text->text = malloc(sizeof(char)*(length+1));
191     memset(text->text, '\0', length);
192+    box(scr, 0,0);
193     return text;
194 }
195 
196@@ -82,8 +83,23 @@         char str[text->length];
197         wcstombs(str, text->text, sizeof(text->text));
198         sch(str, (int)strlen(str));
199 
200-        move(0,0);
201+        wmove(text->scr,1,1);
202+        wprintw(text->scr, "%*ls", text->current,text->text);
203         wrefresh(text->scr);
204-        wprintw(text->scr, "%*ls", text->current,text->text);
205     }
206 }
207+
208+PANEL* new_panel(WINDOW* scr)
209+{
210+    PANEL *panel = (PANEL*)malloc(sizeof(PANEL));
211+    panel->scr = scr;
212+    box(scr, 0,0);
213+    return panel;
214+}
215+void write_char(PANEL* panel, int l, char *text)
216+{
217+    int x = getmaxx(panel->scr);
218+    wmove(panel->scr, l+1, 1);
219+    wprintw(panel->scr, "%.*s", x-3, text);
220+    wrefresh(panel->scr);
221+}
222diff --git a/lib/ui.h b/lib/ui.h
223index 12ee2f4932a6fccf18ea2d71a7694baee3e4ea88..271105ae2c3e97e61eae9831a863fd9c4fc3638f 100644
224--- a/lib/ui.h
225+++ b/lib/ui.h
226@@ -21,3 +21,12 @@ } TEXT_BOX;
227 
228 TEXT_BOX* new_text_box(WINDOW*, int);
229 void get_char(TEXT_BOX* text, void (*sch)(char*, int));
230+
231+typedef struct panel
232+{
233+    WINDOW *scr;
234+} PANEL;
235+
236+
237+PANEL* new_panel(WINDOW*);
238+void write_char(PANEL*, int, char*);