dict @ 0a2e62f57734f820cd20e151d1150342408552ed

 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    return text;
60}
61
62void get_char(TEXT_BOX* text, void (*sch)(char*, int))
63{
64    while(1) {
65        wchar_t c;
66        get_wch((wint_t*)&c);
67
68        switch(c) {
69        case KEY_BACKSPACE:
70            if (text->current > 0) {
71                text->text[text->current--] = '\0';
72            }
73            break;
74        default:
75            if (text->current < (text->length-2)) {
76                text->text[text->current] = c;
77                text->text[++text->current] = '\0';
78            }
79        }
80
81        char str[text->length];
82        wcstombs(str, text->text, sizeof(text->text));
83        sch(str, (int)strlen(str));
84
85        move(0,0);
86        wrefresh(text->scr);
87        wprintw(text->scr, "%*ls", text->current,text->text);
88    }
89}