1#define NCURSES_WIDECHAR 1
2
3#include <ncurses.h>
4#include <math.h>
5#include <stdlib.h>
6#include <string.h>
7#include <wchar.h>
8#include "ui.h"
9
10const char *uload = "█";
11
12PROGRESS_BAR* new_progress_bar(WINDOW* scr, float total) {
13 PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR));
14 bar->scr = scr;
15 bar->total = total;
16 bar->current = 0;
17 return bar;
18}
19
20void bar_step(PROGRESS_BAR* bar, float step){
21 bar->current += step;
22
23 int x, y;
24 int hx, hy;
25
26 getmaxyx(bar->scr, y, x);
27
28 hx = x/2;
29 hy = y/2;
30
31 float total = (bar->current/bar->total);
32
33 wmove(bar->scr, hy-1, 0);
34 for (int i = 0; i < ((float)x*total); i++)
35 wprintw(bar->scr, uload);
36
37 wmove(bar->scr, hy, hx-4);
38 wprintw(bar->scr,"%03.0f%% ", total*100);
39
40 int len = floor(log10(abs((int)bar->total))) + 3;
41
42 wmove(bar->scr, hy+1, hx - len);
43 wprintw(bar->scr, "%.0f/%.0f", bar->current, bar->total);
44
45
46 wmove(bar->scr,0,0);
47 wrefresh(bar->scr);
48}
49
50
51TEXT_BOX* new_text_box(WINDOW* scr, int length) {
52 TEXT_BOX *text = (TEXT_BOX*)malloc(sizeof(TEXT_BOX));
53 text->scr = scr;
54 text->length = length;
55 text->current = 0;
56 text->text = malloc(sizeof(char)*(length+1));
57 memset(text->text, '\0', length);
58 return text;
59}
60
61void get_char(TEXT_BOX* text, void (*sch)(char*, int)) {
62 while(1){
63 wchar_t c;
64 get_wch((wint_t*)&c);
65
66 switch(c) {
67 case KEY_BACKSPACE:
68 if (text->current > 0) {
69 text->text[text->current--] = '\0';
70 }
71 break;
72 default:
73 if (text->current < (text->length-2)) {
74 text->text[text->current] = c;
75 text->text[++text->current] = '\0';
76 }
77 }
78
79 char str[text->length];
80 wcstombs(str, text->text, sizeof(text->text));
81 sch(str, (int)strlen(str));
82
83 move(0,0);
84 wrefresh(text->scr);
85 wprintw(text->scr, "%*ls", text->current,text->text);
86 }
87}