summaryrefslogtreecommitdiffstats
path: root/helpers.c
blob: 9973093f5eb5a7ebf661073f81aaa9de03c7c327 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <assert.h>
#include <locale.h>
#include <stdarg.h>
#include <stdlib.h>

#include "helpers.h"

void *xmalloc(size_t size) {
    void *ptr = malloc((size ? size : 1));
    assert(ptr != NULL);
    return ptr;
}

unsigned long strict_strtoul(const char *nptr, int base) {
    char *endptr;
    unsigned long ret = strtoul(nptr, &endptr, base);
    return (*endptr ? 0 : ret);
}

void cursinit(void) {
    setlocale(LC_ALL, "");
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    curs_set(FALSE);
    timeout(25);
    refresh();

    start_color();
    use_default_colors();

    init_pair(1, COLOR_GREEN, -1);
    init_pair(2, COLOR_CYAN, -1);
    init_pair(3, COLOR_RED, -1);
    init_pair(4, COLOR_YELLOW, -1);
}

void cursupdate(void) {
    update_panels();
    doupdate();
}

PANEL *newpan(int h, int w, int y, int x) {
    WINDOW *win = newwin(h, w, y, x);
    scrollok(win, TRUE);
    return new_panel(win);
}

void delpan(PANEL *pan) {
    WINDOW *win = panel_window(pan);
    del_panel(pan);
    delwin(win);
}

void reset_panel(PANEL *pan, int h, int w, int y, int x) {
    WINDOW *win = panel_window(pan);
    wresize(win, h, w);
    replace_panel(pan, win);
    move_panel(pan, y, x);
}

int pprintw(PANEL *pan, const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);
    int res = vw_printw(panel_window(pan), fmt, args);
    va_end(args);
    return res;
}

int pclear(PANEL *pan) {
    return wclear(panel_window(pan));
}

int pattron(PANEL *pan, int attrs) {
    return wattron(panel_window(pan), attrs);
}

int pattroff(PANEL *pan, int attrs) {
    return wattroff(panel_window(pan), attrs);
}