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{
14 PROGRESS_BAR *bar = (PROGRESS_BAR*)malloc(sizeof(PROGRESS_BAR));
15 bar->scr = scr;
16 bar->total = total;
17 bar->current = 0;
18 return bar;
19}
20
21void bar_step(PROGRESS_BAR* bar, float step)
22{
23 bar->current += step;
24
25 int x, y;
26 int hx, hy;
27
28 getmaxyx(bar->scr, y, x);
29
30 hx = x/2;
31 hy = y/2;
32
33 float total = (bar->current/bar->total);
34
35 wmove(bar->scr, hy-1, 0);
36 for (int i = 0; i < ((float)x*total); i++)
37 wprintw(bar->scr, uload);
38
39 wmove(bar->scr, hy, hx-4);
40 wprintw(bar->scr,"%03.0f%% ", total*100);
41
42 int len = floor(log10(abs((int)bar->total))) + 3;
43
44 wmove(bar->scr, hy+1, hx - len);
45 wprintw(bar->scr, "%.0f/%.0f", bar->current, bar->total);
46
47 wmove(bar->scr,0,0);
48 wrefresh(bar->scr);
49}
50
51TEXT_BOX* new_text_box(WINDOW* scr, int length)
52{
53 TEXT_BOX *text = (TEXT_BOX*)malloc(sizeof(TEXT_BOX));
54 text->scr = scr;
55 text->length = length;
56 text->current = 0;
57 text->text = malloc(sizeof(char)*(length+1));
58 memset(text->text, '\0', length);
59 box(scr, 0,0);
60 return text;
61}
62
63void get_char(TEXT_BOX* text, void (*sch)(char*, int))
64{
65 while(1) {
66 wchar_t c;
67 get_wch((wint_t*)&c);
68
69 switch(c) {
70 case KEY_BACKSPACE:
71 if (text->current > 0) {
72 text->text[text->current--] = '\0';
73 }
74 break;
75 default:
76 if (text->current < (text->length-2)) {
77 text->text[text->current] = c;
78 text->text[++text->current] = '\0';
79 }
80 }
81
82 char str[text->length];
83 wcstombs(str, text->text, sizeof(text->text));
84 sch(str, (int)strlen(str));
85
86 wmove(text->scr,1,1);
87 wprintw(text->scr, "%*ls", text->current,text->text);
88 wrefresh(text->scr);
89 }
90}
91
92PANEL* new_panel(WINDOW* scr)
93{
94 PANEL *panel = (PANEL*)malloc(sizeof(PANEL));
95 panel->scr = scr;
96 box(scr, 0,0);
97 return panel;
98}
99void write_char(PANEL* panel, int l, char *text)
100{
101 int x = getmaxx(panel->scr);
102 wmove(panel->scr, l+1, 1);
103 wprintw(panel->scr, "%.*s", x-3, text);
104 wrefresh(panel->scr);
105}