aboutsummaryrefslogtreecommitdiff
path: root/chatty2.c
blob: 5a00feeab552a9b62b58c539872f62ac6c8a7a8d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Macro's */

#define TB_IMPL
#include "external/termbox2.h"
#undef TB_IMPL

#define DEBUG
#define MAX_INPUT_LEN 255

#define CHATTY_IMPL
#include "ui.h"
#undef CHATTY_IMPL
#include "protocol.h"

#include <locale.h>

#ifdef DEBUG
#define Assert(expr) \
    if (!(expr)) \
    { \
        tb_shutdown(); \
        raise(SIGTRAP); \
    }
#else
#define Assert(expr) ;
#endif

int
main(int Argc, char *Argv[])
{
    struct tb_event ev;
    rect TextBox = {0, 0, 24, 4};
    rect TextR = {
        TextBox.X + TEXTBOX_BORDER_WIDTH + TEXTBOX_PADDING_X, 
        TextBox.Y + TEXTBOX_BORDER_WIDTH,
        TextBox.W - TEXTBOX_BORDER_WIDTH * 2 - TEXTBOX_PADDING_X * 2, 
        TextBox.H - TEXTBOX_BORDER_WIDTH * 2
    };
    wchar_t Input[MAX_INPUT_LEN] = {0};
    u32 InputLen = 0;
    u32 InputOffset = 0;
    u32 InputPos = 0;
    u32 DidParseKey = 0;

    Assert(setlocale(LC_ALL, ""));
    tb_init();
    global.cursor_x = TextR.X;
    global.cursor_y = TextR.Y;
    bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);

    while (ev.key != TB_KEY_CTRL_C)
    {
        tb_clear();

        DrawBox(TextBox, 0);
        TextBoxDraw(TextR, Input + InputOffset, InputLen);

        InputPos = InputOffset + (global.cursor_x - TextR.X) + (global.cursor_y - TextR.Y) * TextR.W; 
        Assert(InputPos <= InputLen);

        tb_present();

        tb_poll_event(&ev);

        // TODO: Handle resize event

        // Intercept keys
        if (ev.key == TB_KEY_CTRL_M)
        {
            tb_printf(26, 0, 0, 0, "sent.");
            continue;
        }
        else
        {
            DidParseKey = TextBoxKeypress(ev, TextR,
                                          Input, &InputLen, InputPos, &InputOffset);
        }

        u32 ShouldInsert = (!DidParseKey) && (ev.ch && InputLen < MAX_INPUT_LEN);
        if (ShouldInsert)
        {
            TextBoxInsert(Input, InputPos, InputLen++, ev.ch);
            ScrollRight(TextR, &InputOffset);
        }

        // tb_clear();
    }

    tb_shutdown();
}