aboutsummaryrefslogtreecommitdiff
path: root/lib/ui.c
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-02-17 11:41:23 +0100
committergabrielgio <gabrielgio@workstation.lan>2024-02-17 16:48:54 +0100
commitd102e028aee6571c0fd9dfd4074cfb3c15f4594e (patch)
treee0f3bdadc96019de0a7576ea591f8b304a962e67 /lib/ui.c
parentc573d3b7954296d95a0f8a79b8ac2ca261d86a02 (diff)
downloaddict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.tar.gz
dict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.tar.bz2
dict-d102e028aee6571c0fd9dfd4074cfb3c15f4594e.zip
ref: Refactor newer folder structure
Create a lib dict and importer project. * dict: holds the main application * importer: code to read from source to a common database. * lib: shared code
Diffstat (limited to 'lib/ui.c')
-rw-r--r--lib/ui.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/ui.c b/lib/ui.c
new file mode 100644
index 0000000..1a285a0
--- /dev/null
+++ b/lib/ui.c
@@ -0,0 +1,87 @@
+#define NCURSES_WIDECHAR 1
+
+#include <ncurses.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.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;
+ 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);
+}
+
+
+TEXT_BOX* new_text_box(WINDOW* scr, int length) {
+ TEXT_BOX *text = (TEXT_BOX*)malloc(sizeof(TEXT_BOX));
+ text->scr = scr;
+ text->length = length;
+ text->current = 0;
+ text->text = malloc(sizeof(char)*(length+1));
+ memset(text->text, '\0', length);
+ return text;
+}
+
+void get_char(TEXT_BOX* text, void (*sch)(char*, int)) {
+ while(1){
+ wchar_t c;
+ get_wch((wint_t*)&c);
+
+ switch(c) {
+ case KEY_BACKSPACE:
+ if (text->current > 0) {
+ text->text[text->current--] = '\0';
+ }
+ break;
+ default:
+ if (text->current < (text->length-2)) {
+ text->text[text->current] = c;
+ text->text[++text->current] = '\0';
+ }
+ }
+
+ char str[text->length];
+ wcstombs(str, text->text, sizeof(text->text));
+ sch(str, (int)strlen(str));
+
+ move(0,0);
+ wrefresh(text->scr);
+ wprintw(text->scr, "%*ls", text->current,text->text);
+ }
+}