From f2200e22b05c3801b722cd46617e7bcf64538d17 Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Sat, 17 Feb 2024 21:55:15 +0100 Subject: feat: Add a more "refined" UI --- CMakeLists.txt | 1 + dict/main.c | 53 ++++++++++++++--------------------------------------- importer/main.c | 6 ++---- lib/data.c | 6 ++++-- lib/data.h | 2 +- lib/ui.c | 20 ++++++++++++++++++-- lib/ui.h | 9 +++++++++ 7 files changed, 49 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a88fc55..63c9d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,3 +5,4 @@ project(dict VERSION 0.1 LANGUAGES C) add_subdirectory(ext) add_subdirectory(importer) add_subdirectory(lib) +add_subdirectory(dict) diff --git a/dict/main.c b/dict/main.c index b743e07..98608cc 100644 --- a/dict/main.c +++ b/dict/main.c @@ -10,21 +10,19 @@ #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 @@ int main(int argc, char** argv) } } - 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 @@ int run(const char *db, const char *txt) cbreak(); keypad(stdscr, TRUE); - FILE *f = fopen(txt, "r"); - unsigned int lines = count_file_lines(f); - fseek(f, 0, SEEK_SET); - - 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; - - 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); + WINDOW* tbox = newwin(3,COLS,0,0); + TEXT_BOX *box = new_text_box(tbox, 100); - clear(); - refresh(); + tpanel = newwin(LINES-3, COLS, 3,0); + panel = new_panel(tpanel); - TEXT_BOX *box = new_text_box(stdscr, 100); get_char(box, search); - clear(); refresh(); endwin(); @@ -100,13 +76,12 @@ void search(char *sch, int len) 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 b83e850..087fc48 100644 --- a/importer/main.c +++ b/importer/main.c @@ -67,10 +67,8 @@ int run(const char *db, const char *txt) 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 e94e0a7..1c1a754 100644 --- a/lib/data.c +++ b/lib/data.c @@ -3,9 +3,10 @@ #include #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 @@ void bootstrap(Data* data) 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* data_select(Data* data, char *sch, int len) 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 fcde55a..6c9f30f 100644 --- a/lib/data.h +++ b/lib/data.h @@ -39,7 +39,7 @@ void insert(Data*, char*, int); /* * 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 3eae201..cd54cd4 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -56,6 +56,7 @@ TEXT_BOX* new_text_box(WINDOW* scr, int 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 @@ void get_char(TEXT_BOX* text, void (*sch)(char*, int)) wcstombs(str, text->text, sizeof(text->text)); sch(str, (int)strlen(str)); - move(0,0); - wrefresh(text->scr); + wmove(text->scr,1,1); wprintw(text->scr, "%*ls", text->current,text->text); + wrefresh(text->scr); } } + +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 12ee2f4..271105a 100644 --- a/lib/ui.h +++ b/lib/ui.h @@ -21,3 +21,12 @@ typedef struct 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*); -- cgit v1.2.3