summaryrefslogtreecommitdiff
path: root/src/fruits/cmc/ui/panes/log.c
blob: 370e91abf67caeef428d828e03c3ca8ef826fdc8 (plain)
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
#include "log.h"

void cmc_log_pane_init(struct cmc_log_pane *lp)
{
    lp->new_message = false;
    al_array_init(lp->messages);
}

bool cmc_log_pane_layout(struct cmc_log_pane *lp, struct ncplane *parent)
{
    if (lp->n) ncplane_destroy(lp->n);
    struct ncplane_options nopts = { 0 };
    nopts.rows = ncplane_dim_y(parent);
    nopts.cols = ncplane_dim_x(parent);
    lp->n = ncplane_create(parent, &nopts);
    return true;
}

void cmc_log_pane_push_message(struct cmc_log_pane *lp, char *message)
{
    al_array_push(lp->messages, message);
    lp->new_message = true;
}

bool cmc_log_pane_handle_input(struct cmc_log_pane *lp, struct ncinput *input)
{
    (void)lp;
    (void)input;
    return false;
}

bool cmc_log_pane_tick(struct cmc_log_pane *lp)
{
    if (lp->new_message) {
        lp->new_message = false;
        return true;
    }
    return false;
}

void cmc_log_pane_clear(struct cmc_log_pane *lp)
{
    ncplane_erase(lp->n);
}

void cmc_log_pane_render(struct cmc_log_pane *lp)
{
    struct ncplane *n = lp->n;

    ncplane_erase(n);

    u32 max_height = ncplane_dim_y(n);

    u32 count = lp->messages.count;
    u32 index = (count > max_height) ? count - max_height : 0;
    for (u32 i = index; i < count; i++) {
        // We can't use putnstr to control the width because these messages will have wide characters.
        ncplane_putstr_yx(n, i - index, 0, al_array_at(lp->messages, i));
    }

    if (index > 0) {
        //for (u32 i = 0; i < index; i++) {
        //    al_free(al_array_at(lp->messages, i));
        //}
        //al_array_remove_range(lp->messages, 0, index);
    }
}