aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-02-14 11:59:10 +0100
committerGabriel A. Giovanini <g.giovanini@gridx.de>2024-02-14 12:29:28 +0100
commit3967b61dfbee50d2072ddefced877486ce439582 (patch)
tree800856749a232fa5ed92ab1d51b43c1218f59232
parent03aa0fe6c664f74e8e4e5877ef89b4e053b30bc5 (diff)
downloaddict-3967b61dfbee50d2072ddefced877486ce439582.tar.gz
dict-3967b61dfbee50d2072ddefced877486ce439582.tar.bz2
dict-3967b61dfbee50d2072ddefced877486ce439582.zip
feat: Add buggy ui component
-rw-r--r--data.c2
-rw-r--r--data.h2
-rw-r--r--main.c28
-rw-r--r--ui.c63
-rw-r--r--ui.h11
5 files changed, 87 insertions, 19 deletions
diff --git a/data.c b/data.c
index 777dcdf..81ee9f3 100644
--- a/data.c
+++ b/data.c
@@ -70,7 +70,7 @@ void bootstrap(Data* data) {
sqlite3_finalize(stmt);
}
-LIST* select(Data* data) {
+LIST* data_select(Data* data) {
sqlite3_stmt *stmt;
int r = sqlite3_prepare_v2(data->db, select_words, -1, &stmt, NULL);
diff --git a/data.h b/data.h
index 393f830..8bc30e9 100644
--- a/data.h
+++ b/data.h
@@ -39,7 +39,7 @@ void insert(Data*, char*, int);
/*
* Select all words.
*/
-LIST* select(Data*);
+LIST* data_select(Data*);
/*
* Print result code from sqlite.
diff --git a/main.c b/main.c
index dae1f2a..69a68b8 100644
--- a/main.c
+++ b/main.c
@@ -3,6 +3,7 @@
#include <sqlite3.h>
#include <ncurses.h>
#include "data.h"
+#include "ui.h"
#define BUF_SIZE 100
@@ -10,14 +11,13 @@ unsigned int count_lines(FILE* file);
int load_or_save_db(sqlite3 *pInMemory, const char *zFilename, int isSave);
int main() {
- Data *data = new_data(":memory:");
-
- bootstrap(data);
-
setlocale(LC_ALL, "");
- initscr();
+ initscr();
+ noecho();
+ cbreak();
- int maxx=getmaxx(stdscr);
+ Data *data = new_data(":memory:");
+ bootstrap(data);
FILE *f = fopen("dict.txt", "r");
unsigned int lines = count_lines(f);
@@ -26,22 +26,13 @@ int main() {
char * line = NULL;
size_t len = 0;
ssize_t read;
- int count = 0;
+ 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);
- count ++;
- move(0,0);
- float total = ((float)count/(float)lines);
- printw("%03.0f%% ", total*100);
- for (int x = 0; x < ((maxx-4)*total); x++) {
- printw("█");
- }
- move(1,0);
- printw("%d/%d",count,lines);
- refresh();
+ bar_step(bar, 1);
}
move(2,0);
@@ -51,6 +42,9 @@ int main() {
clear();
refresh();
+ endwin();
+
+ free_data(data);
return 0;
}
diff --git a/ui.c b/ui.c
new file mode 100644
index 0000000..8837bab
--- /dev/null
+++ b/ui.c
@@ -0,0 +1,63 @@
+#include "math.h"
+#include <ncurses.h>
+#include <stdlib.h>
+#include "ui.h"
+
+const char *uload = "█";
+
+
+PROGRESS_BAR* new_progress_bar(WINDOW* scr, float total) {
+ PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR));
+ bar->scr = scr;
+ bar->total = total;
+ bar->current = 0;
+
+ int x, y;
+ int hx, hy;
+
+ getmaxyx(scr, y, x);
+
+ hx = x/2;
+ hy = y/2;
+
+ wmove(scr, hy-1, 0);
+ wprintw(scr, uload);
+
+ wmove(scr, hy, hx-4);
+ wprintw(scr, "000%%");
+
+ wmove(scr, hy+1, hx);
+ wprintw(scr, "%.0f/%.0f", 0.0, total);
+
+ return bar;
+}
+
+void bar_step(PROGRESS_BAR* bar, float step){
+ bar->current += step;
+
+ int x, y;
+ int hx, hy;
+
+ getmaxyx(bar->scr, y, x);
+
+ hx = x/2;
+ hy = y/2;
+
+ float total = (bar->current/bar->total);
+
+ wmove(bar->scr, hy-1, 0);
+ for (int i = 0; i < ((float)x*total); i++)
+ wprintw(bar->scr, uload);
+
+ wmove(bar->scr, hy, hx-4);
+ wprintw(bar->scr,"%03.0f%% ", total*100);
+
+ int len = floor(log10(abs((int)bar->total))) + 3;
+
+ wmove(bar->scr, hy+1, hx - len);
+ wprintw(bar->scr, "%.0f/%.0f", bar->current, bar->total);
+
+
+ wmove(bar->scr,0,0);
+ wrefresh(bar->scr);
+}
diff --git a/ui.h b/ui.h
new file mode 100644
index 0000000..8035d6f
--- /dev/null
+++ b/ui.h
@@ -0,0 +1,11 @@
+#include <ncurses.h>
+
+typedef struct progress_bar {
+ float total;
+ float current;
+ WINDOW *scr;
+} PROGRESS_BAR;
+
+PROGRESS_BAR* new_progress_bar(WINDOW*, float);
+
+void bar_step(PROGRESS_BAR*, float);