aboutsummaryrefslogtreecommitdiff
path: root/ui.h
blob: 078a456bbe00f39b7cc3b9dbcb23370e5ffb124e (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#ifndef UI_H
#define UI_H

/* Macro's */

#include <stdint.h>
#include <stdbool.h>
#include "termbox2.h"
#include "arena.h"

/* Types */

// Format option at a position in raw text, used when iterating to know when to toggle a color
// option.
typedef struct {
    u32 Position;
    u32 Color;
} format_option;

// Array of format options and length of said array
typedef struct {
    format_option* Options;
    u32 Len;
} markdown_formatoptions;

typedef struct {
    u32* Text;
    u32 Len;
} raw_result;

// Rectangle
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;

/* Functions */

bool IsInRect(rect Rect, s32 X, s32 Y);
bool is_whitespace(u32 ch);
bool is_markdown(u32 ch);
void tb_print_wrapped(u32 X, u32 Y, u32 XLimit, u32 YLimit, u32* Text, u32 Len);
void tb_print_markdown(u32 X, u32 Y, u32 fg, u32 bg, u32* Text, u32 Len);
u32 tb_print_wrapped_with_markdown(u32 XOffset, u32 YOffset, u32 fg, u32 bg,
                                   u32* Text, u32 Len,
                                   u32 XLimit, u32 YLimit,
                                   markdown_formatoptions MDFormat);
raw_result markdown_to_raw(Arena* ScratchArena, wchar_t* Text, u32 Len);
markdown_formatoptions preprocess_markdown(Arena* ScratchArena, wchar_t* Text, u32 Len);

/* Input Box UI */

// assumes TEXTBOX_MAX_INPUT to be set
//
// #define TEXTBOX_MAX_INPUT 128

#define TEXTBOX_PADDING_X 1
#define TEXTBOX_BORDER_WIDTH 1
#define TEXTBOX_MIN_WIDTH TEXTBOX_PADDING_X * 2 + TEXTBOX_BORDER_WIDTH * 2 + 1;
#define TEXTBOXFROMBOX(Box) \
    { \
        .X = Box.X + TEXTBOX_BORDER_WIDTH + TEXTBOX_PADDING_X, \
        .Y = Box.Y + TEXTBOX_BORDER_WIDTH, \
        .W = Box.W - TEXTBOX_BORDER_WIDTH * 2 - TEXTBOX_PADDING_X * 2, \
        .H = Box.H - TEXTBOX_BORDER_WIDTH * 2 \
    }

void DrawBox(rect Rect, box_characters *Chars);
void DrawTextBox(rect TextR, wchar_t *Text, u32 TextLen);
void DrawTextBoxWrapped(rect TextR, wchar_t *Text, u32 TextLen);
void TextBoxScrollLeft(rect Text, u32 *TextOffset);
void TextBoxScrollRight(rect Text, u32 *TextOffset);
void TextBoxDelete(wchar_t* Text, u64 Pos);
void TextBoxInsert(wchar_t *Input, u32 InputPos, u32 InputLen, wchar_t ch);
u32  TextBoxKeypress(struct tb_event ev,
                     rect TextR, wchar_t *Text, u32 *TextLenPtr, u32 TextPos, u32 *TextOffsetPtr);

// Draw box along boundaries in Rect with optional Chars.
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 (s32 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 (s32 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 (s32 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);
}

// SCROLLING
// ╭──────────╮    ╭──────────╮ Going Left on the first character scrolls up.
// │ █3     4 │ => │ 1     2█ │ Cursor on end of the top line.
// │  5     6 │    │  3     4 │ 
// ╰──────────╯    ╰──────────╯
//
// ╭──────────╮    ╭──────────╮ Going Right on the last character scrolls down.
// │ 1      3 │ => │ 2     4  │ Puts cursor on start of the bottom line.
// │ 2     4█ │    │ █        │ 
// ╰──────────╯    ╰──────────╯
//
// ╭──────────╮    ╭──────────╮ Going Down on bottom line scrolls down.
// │ 1      3 │ => │ 2      4 │ Cursor stays on bottom line.
// │ 2  █   4 │    │    █     │ 
// ╰──────────╯    ╰──────────╯
//
// ╭──────────╮    ╭──────────╮ Going Up on top line scrolls up.
// │ 3  █   4 │ => │ 1  █   2 │ Cursor stays on top line.
// │ 5      6 │    │ 3      5 │ 
// ╰──────────╯    ╰──────────╯
//
// In code this translates to changing global.cursor_{x,y} and TextOffset accordingly.

// Scroll one character to the left
void
TextBoxScrollLeft(rect Text, u32 *TextOffset)
{
    // If text is on the first character of the box scroll up
    if (global.cursor_x == Text.X &&
        global.cursor_y == Text.Y)
    {
        global.cursor_x = Text.X + Text.W - 1;
        global.cursor_y = Text.Y;

        *TextOffset -= Text.W;
    }
    else
    {
        if (global.cursor_x == Text.X)
        {
            // Got to previous line
            global.cursor_x = Text.X + Text.W - 1;
            global.cursor_y--;
        }
        else
        {
            global.cursor_x--;
        }
    }
}

// Scroll one character to the right
void
TextBoxScrollRight(rect Text, u32 *TextOffset)
{
    // 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 + Text.H - 1;

        *TextOffset += Text.W;
    }
    else
    {
        global.cursor_x++;
        if (global.cursor_x == Text.X + Text.W)
        {
            global.cursor_x = Text.X;
            global.cursor_y++;
        }
    }
}


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

// Insert a ev.ch in Input at InputPos
void
TextBoxInsert(wchar_t *Input, u32 InputPos, u32 InputLen, wchar_t ch)
{
    if (InputPos < InputLen)
    {
        memmove(Input + InputPos,
                Input + InputPos - 1,
                (InputLen - InputPos + 1) * sizeof(*Input));
    }
    Input[InputPos] = ch;
}

// Handle the key event ev changing Text, TextLenPtr, and TextOffsetPtr accordingly.
// InputPos is the position in the Input relating to the cursor position.
// TextR is the bounding box for the text.
//
// Returns non-zero when a key event was handled.
//
// TODO: pass by value and return struct with updated values
u32
TextBoxKeypress(struct tb_event ev, rect TextR,
                wchar_t *Text, u32 *TextLenPtr, u32 TextPos, u32 *TextOffsetPtr)
{
    u32 Result = 1;

    u32 TextLen = *TextLenPtr;
    u32 TextOffset = *TextOffsetPtr;

    switch (ev.key)
    {

    // Delete character backwards
    case TB_KEY_CTRL_8:
    // case TB_KEY_BACKSPACE2:
    {
        if (TextPos == 0) break;

        TextBoxDelete(Text, TextPos - 1);
        TextLen--;

        TextBoxScrollLeft(TextR, &TextOffset);

    } break;

    // Delete character forwards
    case TB_KEY_CTRL_D:
    {
        if (TextPos == TextLen) break;
        TextBoxDelete(Text, TextPos);
        TextLen--;
        // Delete(Text, Position)
    } break;

    // Delete word backwards
    case TB_KEY_CTRL_W:
    {
        u32 At = TextPos;
        // Find character to stop on
        while (At && is_whitespace(Text[At - 1])) At--;
        while (At && !is_whitespace(Text[At - 1])) At--;

        s32 NDelete = TextPos - At;
        memmove(Text + At, Text + TextPos, (TextLen - TextPos) * sizeof(Text[At]));
        TextLen -= NDelete;
#ifdef DEBUG
        Text[TextLen] = 0;
#endif
        // NOTE: this could be calculated at once instead
        while(NDelete--) TextBoxScrollLeft(TextR, &TextOffset);

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

    } break;

    // Delete until start of Text
    case TB_KEY_CTRL_U:
    {
        memmove(Text, Text + TextPos, (TextLen - TextPos) * sizeof(*Text));
        TextLen -= TextPos;
#ifdef DEBUG
        Text[TextLen] = 0;
#endif
        global.cursor_x = TextR.X;
        global.cursor_y = TextR.Y;
        TextOffset = 0;
    } break;

    // Delete until end of Text
    case TB_KEY_CTRL_K:
    {
        TextLen = TextPos;
        Text[TextPos] = 0;
    } break;

    // Move to start of line
    case TB_KEY_CTRL_A: global.cursor_x = TextR.X; break;

    // Move to end of line
    case TB_KEY_CTRL_E:
    {
        if (global.cursor_x == TextR.X + TextR.W - 1) break;

        if (TextPos + TextR.W > TextLen) 
        {
            // Put the cursor on the last character
            global.cursor_x = TextR.X + (TextLen - TextOffset) % TextR.W;
        }
        else
        {
            global.cursor_x = TextR.X + TextR.W - 1;
        }
    } break;

    // Move backwards
    case TB_KEY_CTRL_B: 
    case TB_KEY_ARROW_LEFT:
    {
        // Move forward by word
        if (ev.mod == TB_MOD_CTRL)
        {
            u32 At = TextPos;
            while(At && is_whitespace(Text[At])) At--;
            while(At && !is_whitespace(Text[At])) At--;
            while(TextPos - At++) TextBoxScrollLeft(TextR, &TextOffset);
        }
        // Move forward by character
        else
        {
            if (TextPos == 0) break;
            TextBoxScrollLeft(TextR, &TextOffset);
        }
    } break;

    // Move forwards
    case TB_KEY_CTRL_F:
    case TB_KEY_ARROW_RIGHT:
    {
        // Move forward by word
        if (ev.mod == TB_MOD_CTRL)
        {
            u32 At = TextPos;
            while(At < TextLen && is_whitespace(Text[At])) At++;
            while(At < TextLen && !is_whitespace(Text[At])) At++;
            while(At-- - TextPos) TextBoxScrollRight(TextR, &TextOffset);
        }
        // Move forward by character
        else
        {
            if (TextPos == TextLen) break;
            TextBoxScrollRight(TextR, &TextOffset);
        }
    } break;

    // Move up
    case TB_KEY_CTRL_P:
    case TB_KEY_ARROW_UP:
    {
        if (global.cursor_y == TextR.Y)
        {
            if (TextOffset == 0)
            {
                global.cursor_x = TextR.X;

                break;
            }

            TextOffset -= TextR.W;
            global.cursor_y = TextR.Y;
        }
        else
        {
            global.cursor_y--;
        }
    } break;

    // Move down
    case TB_KEY_CTRL_N:
    case TB_KEY_ARROW_DOWN:
    {
        if (TextPos + TextR.W > TextLen) 
        {
            // Put the cursor on the last character
            global.cursor_x = TextR.X + (TextLen - TextOffset) % (TextR.W);
            global.cursor_y = TextR.Y + (TextLen - TextOffset) / TextR.W;

            // If cursor ended 1 line under the bottom line this means that the text
            // needs to be scrolled.
            if (global.cursor_y == TextR.Y + TextR.H)
            {
                TextOffset += TextR.W;
                global.cursor_y--;
            }

            break;
        }

        if (global.cursor_y == TextR.Y + TextR.H - 1)
        {
            TextOffset += TextR.W;
        }
        else
        {
            global.cursor_y++;
        }
    } break;
    default:
    {
        Result = 0;
    }
    }

    *TextLenPtr = TextLen;
    *TextOffsetPtr = TextOffset;

    return Result;
}

// Draws characters from Text fitting in the TextR rectangle.
// InputLen is the amount of characters in Text.
//
// NOTE: TextR is always filled, when not enough characters in Input it will uses spaces instead.
// This makes it easy to update the textbox by recalling this function.
void
DrawTextBox(rect TextR, wchar_t *Text, u32 TextLen)
{
    // Draw the text right of the cursor
    // NOTE: the cursor is assumed to be in the box
    Assert(IsInRect(TextR, global.cursor_x, global.cursor_y));
    s32 AtX = TextR.X, AtY = TextR.Y;
    u32 At = 0;
    while (AtY < TextR.Y + TextR.H)
    {
        if (At < TextLen) 
        {
            tb_printf(AtX++, AtY, 0, 0, "%lc", Text[At++]);
        }
        else
        {
            tb_printf(AtX++, AtY, 0, 0, " ");
        }

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

// NOTE: To ensure that the text looks the same even when scrolling it you must provide the whole text,
// wrap the whole text and the only show the portion that can fit in the Text Rectangle.

// When line will exceed width break the word on the next line. This is done by looking backwards
// for whitespace from TextR.W width. When a whitespace is found text is wrapped on the next line.
// TODO: this does not work yet.
void
DrawTextBoxWrapped(rect TextR, wchar_t *Text, u32 TextLen)
{
    if (TextLen <= TextR.W)
    {
        tb_printf(TextR.X, TextR.Y, 0, 0, "%ls", Text);
        tb_present();
        global.cursor_x = TextR.X + TextLen;
        global.cursor_y = TextR.Y;
        return;
    }

    u32 SearchIndex = TextR.W; 
    u32 PrevIndex = 0;
    u32 Y = TextR.Y;

    while (SearchIndex < TextLen)
    {
        while (Text[SearchIndex] != ' ')
        {
            SearchIndex--;
            if (SearchIndex == PrevIndex)
            {
                SearchIndex += TextR.W;
                break;
            }
        }

        // Wrap
        wchar_t BreakChar = Text[SearchIndex];
        Text[SearchIndex] = 0;
        tb_printf(TextR.X, Y, 0, 0, "%ls", Text + PrevIndex);
        tb_present();
        Text[SearchIndex] = BreakChar;

        if (Y + 1 == TextR.Y + TextR.H)
        {
            global.cursor_y = Y;
            global.cursor_x = TextR.X + (SearchIndex - PrevIndex);
            return;
        }
        Y++;

        if (BreakChar == L' ')
        {
            SearchIndex++;
        }

        PrevIndex = SearchIndex;
        SearchIndex += TextR.W;
    }

    // This happens when SearchIndex exceeds TextLen but there is still some 
    // text left to print.  We can assume that the text will fit because otherwise it would have
    // been wrapped a second time and the loop would have returned.
    tb_printf(TextR.X, Y, 0, 0, "%ls", Text + PrevIndex);
    // NOTE: this sets the cursor position correctly

    global.cursor_y = Y;
    global.cursor_x = TextR.X + TextLen - PrevIndex;
}

#endif // UI_H

// Check if coordinate (X,Y) is in rect boundaries
bool
IsInRect(rect Rect, s32 X, s32 Y)
{
    if ((X >= Rect.X && X <= Rect.X + Rect.W) &&
        (Y >= Rect.Y && Y <= Rect.Y + Rect.H)) return true;
    return false;
}

// Return True if ch is whitespace
bool
is_whitespace(u32 ch)
{
    if (ch == L' ')
        return true;
    return false;
}

// Return True if ch is a supported markdown markup character
// TODO: tilde
bool
is_markdown(u32 ch)
{
    if (ch == L'_' ||
        ch == L'*')
        return true;
    return false;
}

// Print `Text`, `Len` characters long with markdown
// NOTE: This function has no wrapping support
void
tb_print_markdown(u32 X, u32 Y, u32 fg, u32 bg, u32* Text, u32 Len)
{
    for (u32 ch = 0; ch < Len; ch++)
    {
        if (Text[ch] == L'_')
        {
            if (ch < Len - 1 && Text[ch + 1] == L'_')
            {
                fg ^= TB_UNDERLINE;
                ch++;
            }
            else
            {
                fg ^= TB_ITALIC;
            }
        }
        else if (Text[ch] == L'*')
        {
            if (ch < Len - 1 && Text[ch + 1] == L'*')
            {
                fg ^= TB_BOLD;
                ch++;
            }
            else
            {
                fg ^= TB_ITALIC;
            }
        }
        else
        {
            tb_printf(X, Y, fg, bg, "%lc", Text[ch]);
#ifdef DEBUG
            tb_present();
#endif
            X++;
        }
    }
}

// Print `Text`, `Len` characters long as a string wrapped at `XLimit` width and `YLimit` height.
void
tb_print_wrapped(u32 X, u32 Y, u32 XLimit, u32 YLimit, u32* Text, u32 Len)
{
    // Iterator in text
    Assert(XLimit > 0);
    Assert(YLimit > 0);
    u32 i = XLimit;

    u32 PrevI = 0;

    // For printing
    u32 t = 0;

    while(i < Len)
    {
        // Search backwards for whitespace
        while (!is_whitespace(Text[i]))
        {
            i--;

            // Failed to find whitespace, break on limit at character
            if (i == PrevI)
            {
                i += XLimit;
                break;
            }
        }

        t = Text[i];
        Text[i] = 0;
        tb_printf(X, Y++, 0, 0, "%ls", Text + PrevI);
#ifdef DEBUG
        tb_present();
#endif

        Text[i] = t;

        if (is_whitespace(Text[i])) i++;

        PrevI = i;
        i += XLimit;

        if (Y >= YLimit - 1)
        {
            break;
        }
    }
    tb_printf(X, Y++, 0, 0, "%ls", Text + PrevI);
}

// Print raw string with markdown format options in `MDFormat`, wrapped at
// `XLimit` and `YLimit`.  The string is offset by `XOffset` and `YOffset`.
// `fg` and `bg` are passed to `tb_printf`.
// `Len` is the length of the string not including a null terminator
// The wrapping algorithm searches for a whitespace backwards and if none are found it wraps at
// `XLimit`.
// This function first builds an array of positions where to wrap and then prints `Text` by
// character using the array in `MDFormat.Options` and `WrapPositions` to know when to act.
// Returns how many times wrapped
u32
tb_print_wrapped_with_markdown(u32 XOffset, u32 YOffset, u32 fg, u32 bg,
                               u32* Text, u32 Len,
                               u32 XLimit, u32 YLimit,
                               markdown_formatoptions MDFormat)
{
    XLimit -= XOffset;
    YLimit -= YOffset;
    Assert(YLimit > 0);
    Assert(XLimit > 0);

    u32 TextIndex = XLimit;
    u32 PrevTextIndex = 0;

    u32 WrapPositions[Len/XLimit + 1];
    u32 WrapPositionsLen = 0;

    // Get wrap positions
    while (TextIndex < Len)
    {
        while (!is_whitespace(Text[TextIndex]))
        {
            TextIndex--;

            if (TextIndex == PrevTextIndex)
            {
                TextIndex += XLimit;
                break;
            }
        }

        WrapPositions[WrapPositionsLen] = TextIndex;
        WrapPositionsLen++;

        PrevTextIndex = TextIndex;
        TextIndex += XLimit;
    }

    u32 MDFormatOptionsIndex = 0;
    u32 WrapPositionsIndex = 0;
    u32 X = XOffset, Y = YOffset;

    for (u32 TextIndex = 0; TextIndex < Len; TextIndex++)
    {
        if (MDFormatOptionsIndex < MDFormat.Len &&
            TextIndex == MDFormat.Options[MDFormatOptionsIndex].Position)
        {
            fg ^= MDFormat.Options[MDFormatOptionsIndex].Color;
            MDFormatOptionsIndex++;
        }
        if (WrapPositionsIndex < WrapPositionsLen &&
            TextIndex == WrapPositions[WrapPositionsIndex])
        {
            Y++;
            if (Y == YLimit) return WrapPositionsIndex + 1;
            WrapPositionsIndex++;
            X = XOffset;
            if (is_whitespace(Text[TextIndex])) continue;
        }
        tb_printf(X++, Y, fg, bg, "%lc", Text[TextIndex]);
    }
    Assert(WrapPositionsIndex == WrapPositionsLen);
    Assert(MDFormat.Len == MDFormatOptionsIndex);

    return WrapPositionsLen + 1;
}

// Return string without markdown markup characters using `is_markdown()`
// ScratchArena is used to allocate space for the raw text
// If ScratchArena is null then it will only return then length of the raw string
// Len should be characters + null terminator
// Copies the null terminator as well
raw_result
markdown_to_raw(Arena* ScratchArena, wchar_t* Text, u32 Len)
{
    raw_result Result = {0};
    if (ScratchArena)
    {
        Result.Text = ScratchArena->addr;
    }

    for (u32 i = 0; i < Len; i++)
    {
        if (!is_markdown(Text[i]))
        {
            if (ScratchArena)
            {
                u32* ch = ArenaPush(ScratchArena, sizeof(*ch));
                *ch = Text[i];
            }
            Result.Len++;
        }
    }

    return Result;
}

// Get a string with markdown in it and fill array in makrdown_formtoptions with position and colors
// Use Scratcharena to make allocations on that buffer, The Maximimum space needed is Len, eg. when
// the string is only markup characters.
markdown_formatoptions
preprocess_markdown(Arena* ScratchArena, wchar_t* Text, u32 Len)
{
    markdown_formatoptions Result = {0};
    Result.Options = (format_option*)((u8*)ScratchArena->addr + ScratchArena->pos);

    format_option* FormatOpt;

    // raw char iterator
    u32 rawch = 0;

    for (u32 i = 0; i < Len; i++)
    {
        switch (Text[i])
        {
        case L'_':
        {
            FormatOpt = ArenaPush(ScratchArena, sizeof(*FormatOpt));
            Result.Len++;

            FormatOpt->Position = rawch;
            if (i < Len - 1 && Text[i + 1] == '_')
            {
                FormatOpt->Color = TB_UNDERLINE;
                i++;
            }
            else
            {
                FormatOpt->Color = TB_ITALIC;
            }
        } break;
        case L'*':
        {
            FormatOpt = ArenaPush(ScratchArena, sizeof(*FormatOpt));
            Result.Len++;

            FormatOpt->Position = rawch;
            if (i < Len - 1 && Text[i + 1] == '*')
            {
                FormatOpt->Color = TB_BOLD;
                i++;
            }
            else
            {
                FormatOpt->Color = TB_ITALIC;
            }
        } break;
        default:
        {
            rawch++;
        } break;
        }
    }

    return Result;
}