aboutsummaryrefslogtreecommitdiff
path: root/box.c
blob: af836ed552b59abdef99155c98caac8dba67d328 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#define MAX_INPUT_LEN 128
#define DEBUG

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

#define CHATTY_IMPL
#include "chatty.h"
#undef CHATTY_IMPL

#include "ui.h"

#include <locale.h>

#ifdef DEBUG
#define Assert(expr) if (!expr) \
    { \
        tb_shutdown(); \
        *(u8*)0 = 0; \
    }
#else
#define Assert(expr) ;
#endif

typedef struct {
    s32 X, Y, W, H;
} rect;

// Characters to use for drawing a box
// See DrawBox() for an example
typedef struct {
    wchar_t ur, ru, rd, dr, lr, ud;
} box_characters;

// Draw box Rect with optional Chars characters
void
DrawBox(rect Rect, box_characters *Chars)
{
    wchar_t ur, ru, rd, dr, lr, ud;
    if (!Chars)
    {
        ur = L'╭';
        ru = L'╯';
        rd = L'╮';
        dr = L'╰';
        lr = L'─';
        ud = L'│';
    }
    else
    {
        ur = Chars->ur;
        ru = Chars->ru;
        rd = Chars->rd;
        dr = Chars->dr;
        lr = Chars->lr;
        ud = Chars->ud;
    }

    Rect.H--;
    Rect.W--;

    tb_printf(Rect.X, Rect.Y, 0, 0, "%lc", ur);
    for (u32 X = 1; X < Rect.W; X++)
    {
        tb_printf(Rect.X + X, Rect.Y, 0, 0, "%lc", lr);
    }
    tb_printf(Rect.X + Rect.W, Rect.Y, 0, 0, "%lc", rd);

    // Draw vertical bars
    for (u32 Y = 1; Y < Rect.H; Y++)
    {
        tb_printf(Rect.X, Rect.Y + Y, 0, 0, "%lc", ud);
        tb_printf(Rect.X + Rect.W, Rect.Y + Y, 0, 0, "%lc", ud);
    }

    tb_printf(Rect.X, Rect.Y + Rect.H, 0, 0, "%lc", dr);
    for (u32 X = 1; X < Rect.W; X++)
    {
        tb_printf(Rect.X + X, Rect.Y + Rect.H, 0, 0, "%lc", lr);
    }
    tb_printf(Rect.X + Rect.W, Rect.Y + Rect.H, 0, 0, "%lc", ru);
}


// Delete a character in Text at Pos
void
Delete(wchar_t* Text, u64 Pos)
{
    memmove((u8*)(Text + Pos),
            (u8*)(Text + Pos + 1),
            (MAX_INPUT_LEN - Pos - 1) * sizeof(*Text));
}

// Check if coordinate (X,Y) is in rect boundaries
Bool
IsInRect(rect Rect, u32 X, u32 Y)
{
    if ((X >= Rect.X && X <= Rect.X + Rect.W) &&
        (Y >= Rect.Y && Y <= Rect.Y + Rect.H)) return True;
    return False;
}

int
main(void)
{
    assert(setlocale(LC_ALL, ""));
    struct tb_event ev = {0};

    tb_init();

    u32 InputLen = 0;
    wchar_t Input[MAX_INPUT_LEN] = {0};

#if 0
    wchar_t *t = L"This is some text that no one would want to know, but you could.";
    u32 Len = wcslen(t);
    for (u32 At = 0; At < Len; At++) Input[At] = t[At];
    InputLen = Len;
#endif

    bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);

    rect TextBox = {0, 0, 32, 4};

#define BOX_PADDING_X 1
#define BOX_BORDER_WIDTH 1
    rect Text = {
        2, 1,
        TextBox.W - 2*BOX_PADDING_X - 2*BOX_BORDER_WIDTH,
        TextBox.H - 2*BOX_BORDER_WIDTH
    };
#undef BOX_PADDING_X
#undef BOX_BORDER_WIDTH
    u32 TextSurface = Text.W * Text.H;

    global.cursor_x = Text.X;
    global.cursor_y = Text.Y;

    u32 TextOffset = 0;
    u32 InputPos = 0;

    while (ev.key != TB_KEY_CTRL_C)
    {

        tb_clear();

        DrawBox(TextBox, 0);

        // Draw the text right of the cursor
        // NOTE: the cursor is assumed to be in the box
        Assert(IsInRect(Text, global.cursor_x, global.cursor_y));
        u32 AtX = Text.X, AtY = Text.Y;
        u32 At = TextOffset;
        while (AtY < Text.Y + Text.H)
        {
            if (At < InputLen) 
            {
                tb_printf(AtX++, AtY, 0, 0, "%lc", Input[At++]);
            }
            else
            {
                // Erase previous text
                tb_printf(AtX++, AtY, 0, 0, " ");
            }

            if (AtX == Text.X + Text.W) { AtY++; AtX = Text.X; }
        }

        // Position in input based on cursor position
        InputPos = TextOffset + (global.cursor_x - Text.X) + (global.cursor_y - Text.Y) * Text.W; 

#ifdef DEBUG
        tb_printf(TextBox.X, TextBox.Y, 0, 0,
                  "%2d,%-2d +%d #%-2d %d/%d",
                  global.cursor_x, global.cursor_y,
                  TextOffset, InputPos,
                  InputLen, MAX_INPUT_LEN);
#endif

        tb_present();

        tb_poll_event(&ev);

        if (ev.mod & TB_MOD_CTRL)
        {
            switch (ev.key)
            {
            // Delete character backwards
            case TB_KEY_CTRL_8:
            // case TB_KEY_BACKSPACE2:
            {
                if (InputPos == 0) break;
                Delete(Input, InputPos - 1);
                InputLen--;
                global.cursor_x--;
            } break;
            // Delete character forwards
            case TB_KEY_CTRL_D:
            {
                if (InputPos == InputLen) break;
                Delete(Input, InputPos);
                InputLen--;
                // Delete(Input, Position)
            } break;
            // Delete Word backwards
            case TB_KEY_CTRL_W:
            {
                u32 At = InputPos;
                // Find character to stop on
                while (At && is_whitespace(Input[At - 1])) At--;
                while (At && !is_whitespace(Input[At - 1])) At--;

                s32 NDelete = InputPos - At;
                memmove(Input + At, Input + InputPos, (InputLen - InputPos) * sizeof(Input[At]));
                InputLen -= NDelete;
#ifdef DEBUG
                Input[InputLen] = 0;
#endif

                // Update cursor position
                if (global.cursor_x - NDelete >= Text.X)
                {
                    global.cursor_x -= NDelete;
                }
                else
                {
                    global.cursor_x += Text.W - NDelete;

                    if (global.cursor_y > Text.Y)
                    {
                        global.cursor_y--;
                    }
                    else
                    {
                        // Scroll
                        global.cursor_y = Text.Y + Text.H - 1;
                        TextOffset -= TextSurface;
                    }
                }

                Assert(IsInRect(Text, global.cursor_x, global.cursor_y));

            } break;
            // Delete until start of line
            case TB_KEY_CTRL_U:
            {
                memmove(Input, Input + InputPos, (InputLen - InputPos) * sizeof(*Input));
                InputLen -= InputPos;
#ifdef DEBUG
                Input[InputLen] = 0;
#endif
                global.cursor_x = Text.X;
                global.cursor_y = Text.Y;
                TextOffset = 0;
                // memmove until first character
            } break;
            // Delete until end of input
            case TB_KEY_CTRL_K: break;
            // Move to start
            case TB_KEY_CTRL_A: global.cursor_x = Text.X; break;
            // Move to end
            case TB_KEY_CTRL_E:
            {
                if (global.cursor_x == Text.X + Text.W - 1) break;

                if (InputPos + Text.W > InputLen) 
                {
                    // Put the cursor on the last character
                    global.cursor_x = Text.X + (InputLen - TextOffset) % (Text.W);
                }
                else
                {
                    global.cursor_x = Text.X + Text.W - 1;
                }
            } break;
            // Move backwards by one character
            case TB_KEY_CTRL_B: break;
            // Move forwards by one character
            case TB_KEY_CTRL_F: break;
            // Move backwards by word
            case TB_KEY_ARROW_LEFT: break;
            // Move forwards by word
            case TB_KEY_ARROW_RIGHT: break;
            }

        }

        // Insert new character in Input at InputPos
        else if (ev.ch && InputLen < MAX_INPUT_LEN)
        {
            if (InputPos < InputLen)
            {
                memmove(Input + InputPos + 1,
                        Input + InputPos,
                        (InputLen - InputPos - 1) * sizeof(*Input));
            }
            Input[InputPos] = ev.ch;
            InputLen++;

            if (global.cursor_x == Text.X + Text.W - 1)
            {
                if (global.cursor_y == Text.Y + Text.H - 1)
                {
                    TextOffset += Text.W;
                }
                else
                {
                    global.cursor_y++; 
                }
                global.cursor_x = Text.X;
            }
            else
            {
                global.cursor_x++;
            }
        }
        else
        {
            switch (ev.key)
            {
                case TB_KEY_ARROW_UP:
                {
                    if (global.cursor_y == Text.Y)
                    {
                        if (TextOffset == 0)
                        {
                            global.cursor_x = Text.X;

                            break;
                        }

                        TextOffset -= TextSurface;
                        global.cursor_y = Text.Y + Text.H - 1;
                    }
                    else
                    {
                        global.cursor_y--;
                    }
                } break;
                case TB_KEY_ARROW_DOWN:
                {
                    if (InputPos + Text.W > InputLen) 
                    {
                        // Put the cursor on the last character
                        global.cursor_x = Text.X + (InputLen - TextOffset) % (Text.W);
                        global.cursor_y = Text.Y + (InputLen - TextOffset) / Text.W;

                        break;
                    }

                    if (global.cursor_y == Text.Y + Text.H - 1)
                    {
                        TextOffset += TextSurface;
                        global.cursor_y = Text.Y;
                    }
                    else
                    {
                        global.cursor_y++;
                    }
                } break;

                // Move character left or scroll
                case TB_KEY_ARROW_LEFT:
                {
                    if (InputPos == 0) break;

                    // If text is on the first character of the box scroll backwards
                    if (global.cursor_x == Text.X &&
                        global.cursor_y == Text.Y)
                    {

                        global.cursor_x = Text.X + Text.W - 1;
                        global.cursor_y = Text.Y + Text.H - 1;

                        // Scroll
                        TextOffset -= TextSurface;

                        break;
                    }

                    global.cursor_x--;
                    if (global.cursor_x < Text.X)
                    {
                        global.cursor_x = Text.X + Text.W - 1;
                        global.cursor_y--;
                    }

                } break;
                // Move character right or scroll
                case TB_KEY_ARROW_RIGHT:
                {
                    if (InputPos == InputLen) break;

                    // If cursor is on the last character scroll forwards
                    if (global.cursor_x == Text.X + Text.W - 1 &&
                        global.cursor_y == Text.Y + Text.H - 1)
                    {
                        global.cursor_x = Text.X;
                        global.cursor_y = Text.Y;

                        // Scroll
                        TextOffset += TextSurface;

                        break;
                    }

                    global.cursor_x++;
                    if (global.cursor_x == Text.X + Text.W)
                    {
                        global.cursor_x = Text.X;
                        global.cursor_y++;
                    }
                } break;
            }
        }

    }

    tb_shutdown();
    return 0;
}