aboutsummaryrefslogtreecommitdiff
path: root/ui.c
blob: 8d7cab48bfd438bd326ecc1caf4a2437a78ede35 (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
// 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;

// 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, u32* 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, u32* 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;
}