r3bl_tui 0.7.7

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
Documentation
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
// Copyright (c) 2023-2025 R3BL LLC. Licensed under Apache License, Version 2.0.

use crate::{CodeBlockLine, CodeBlockLineContent, CodeBlockLines,
            md_parser::md_parser_constants::{CODE_BLOCK_END, CODE_BLOCK_START_PARTIAL,
                                             NEW_LINE, NEWLINE_OR_NULL, NULL_CHAR},
            parse_null_padded_line::{is, trim_optional_leading_newline_and_nulls}};
use nom::{IResult, Parser,
          branch::alt,
          bytes::complete::{is_not, tag, take_until, take_while},
          combinator::{eof, map},
          multi::many0,
          sequence::{preceded, terminated}};

/// Parse fenced code blocks with language tags and content.
///
/// # Null Padding Invariant
///
/// This parser expects input where lines end with `\n` followed by zero or more `\0` characters,
/// as provided by `ZeroCopyGapBuffer::as_str()`. The parser uses `NEWLINE_OR_NULL` constant to
/// handle both `\n` and `\0` as line terminators.
///
/// # Sample inputs:
///
/// | Scenario                  | Sample input                                               |
/// |---------------------------|------------------------------------------------------------|
/// | One line                  | `"```bash\npip install foobar\n```\n"`                     |
/// | No line                   | `"```\n\n```\n"`                                           |
/// | Multi line                | `"```bash\npip install foobar\npip install foobar\n```\n"` |
/// | No language               | `"```\npip install foobar\n```\n"`                         |
/// | No language, no line      | `"```\n```\n"`                                             |
/// | No language, multi line   | `"```\npip install foobar\npip install foobar\n```\n"`     |
///
/// # Errors
///
/// Returns a nom parsing error if the input doesn't contain a valid fenced code block.
#[rustfmt::skip]
pub fn parse_fenced_code_block(input: &str) -> IResult<&str, CodeBlockLines<'_>> {
    let (remainder, (lang, code)) = (
        parse_code_block_lang_including_eol,
        parse_code_block_body_including_code_block_end,
    )
        .parse(input)?;

    // Normal case: if there is a newline, consume it along with any null padding
    // to handle the ZeroCopyGapBuffer null padding invariant.
    let remainder = trim_optional_leading_newline_and_nulls(remainder);

    let acc = split_by_new_line(code);

    Ok((remainder, convert_into_code_block_lines(lang, &acc)))
}

/// Take text until an optional EOL character is found, or end of input is reached.
/// Consumes the [`NEW_LINE`] if it exists.
#[rustfmt::skip]
fn parse_code_block_lang_including_eol(input: &str) -> IResult<&str, Option<&str>> {
    alt((
        // Either - Successfully parse both code block language & text.
        map(
            preceded(
                /* prefix - discarded */ tag(CODE_BLOCK_START_PARTIAL),
                /* output */
                terminated(
                    /* match */ is_not(NEWLINE_OR_NULL),
                    /* ends with (discarded) */
                    (tag(NEW_LINE), /* zero or more */ take_while(is(NULL_CHAR))),
                ),
            ),
            Some,
        ),
        // Or - Fail to parse language, use unknown language instead.
        map(
            (
                tag(CODE_BLOCK_START_PARTIAL),
                tag(NEW_LINE),
                /* zero or more */ take_while(is(NULL_CHAR))
            ),
            |_| None,
        ),
    )).parse(input)
}

/// Parse the body of a code block until the end of the code block is reached.
/// The end of the code block is indicated by the [`CODE_BLOCK_END`] constant.
/// Consumes the [`CODE_BLOCK_END`] if it exists.
#[rustfmt::skip]
fn parse_code_block_body_including_code_block_end(input: &str) -> IResult<&str, &str> {
    let (remainder, output) = terminated(
        take_until(CODE_BLOCK_END),
        /* end (discard) */ tag(CODE_BLOCK_END),
    ).parse(input)?;
    Ok((remainder, output))
}

/// Split a string by newline using nom parsers. The idea is that a line is some text
/// followed by a newline. An empty line is just a newline character.
///
/// # Examples:
/// | input          | output               |
/// |:---------------|:---------------------|
/// | "foobar\n"     | `["foobar"]`         |
/// | "\n"           | `[""] `              |
/// | ""             | `[] `                |
/// | "foo\nbar\n"   | `["foo", "bar"] `    |
/// | "\nfoo\nbar\n" | `["", "foo", "bar"]` |
#[must_use]
pub fn split_by_new_line(input: &str) -> Vec<&str> {
    // Define a parser that can handle three different line patterns.
    // This parser will be called repeatedly by many0() to consume the entire input.
    let parser = alt((
        // CASE 1: Regular line with content followed by newline
        // Example: "hello world\n" -> "hello world"
        // This handles the most common case where a line has text content.
        terminated(
            // First part: capture all characters that are NOT newline or null
            // is_not(NEWLINE_OR_NULL) matches any sequence of chars except '\n' and '\0'
            map(is_not(NEWLINE_OR_NULL), |s: &str| s),
            // Second part: consume (but don't capture) the line ending
            // This handles both the newline character and any null padding that follows.
            (
                tag(NEW_LINE),
                /* zero or more */ take_while(is(NULL_CHAR)),
            ),
        ),
        // CASE 2: Empty line (just a newline character, possibly with null padding)
        // Example: "\n" -> ""
        // This is needed because is_not() in CASE 1 would fail on empty content
        map(
            // Match a newline followed by any number of null chars.
            (
                tag(NEW_LINE),
                /* zero or more */ take_while(is(NULL_CHAR)),
            ),
            // Transform the matched pattern into an empty string.
            |_| "",
        ),
        // CASE 3: Last line without trailing newline (at end of file)
        // Example: "last line" (at EOF) -> "last line"
        // This handles the edge case where the file doesn't end with a newline.
        terminated(
            // Match any characters except newline/null
            is_not(NEWLINE_OR_NULL),
            // But only if we're at the end of the input.
            eof,
        ),
    ));

    // Apply the parser repeatedly using many0() to collect all lines
    // many0() will keep applying the parser until it fails, collecting results in a Vec
    let result: IResult<&str, Vec<&str>> = many0(parser).parse(input);

    match result {
        Ok((_, lines)) => lines,
        Err(_) => Vec::new(), // Return empty vec if parsing fails
    }
}

/// Convert language and lines into `CodeBlockLines` structure.
/// This function was previously in `md_parser_ng` but is now implemented locally.
fn convert_into_code_block_lines<'a>(
    language: Option<&'a str>,
    lines: &[&'a str],
) -> CodeBlockLines<'a> {
    let mut result = CodeBlockLines::new();

    // Add start tag
    result.push(CodeBlockLine {
        language,
        content: CodeBlockLineContent::StartTag,
    });

    // Add text lines
    for line in lines {
        result.push(CodeBlockLine {
            language,
            content: CodeBlockLineContent::Text(line),
        });
    }

    // Add end tag
    result.push(CodeBlockLine {
        language,
        content: CodeBlockLineContent::EndTag,
    });

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assert_eq2;

    #[test]
    fn test_parse_codeblock_split_by_eol() {
        assert_eq2!(split_by_new_line("foobar\n"), vec!["foobar"]);
        assert_eq2!(split_by_new_line("\n"), vec![""]);
        assert_eq2!(split_by_new_line(""), Vec::<&str>::new());
        assert_eq2!(split_by_new_line("foo\nbar\n"), vec!["foo", "bar"]);
        assert_eq2!(split_by_new_line("\nfoo\nbar\n"), vec!["", "foo", "bar"]);
    }

    #[test]
    fn test_parse_codeblock_trailing_extra() {
        let input = "```bash\npip install foobar\n````";
        let lang = "bash";
        let code_lines = vec!["pip install foobar"];
        let (remainder, code_block_lines) = parse_fenced_code_block(input).unwrap();
        assert_eq2!(remainder, "`");
        assert_eq2!(
            code_block_lines,
            convert_into_code_block_lines(Some(lang), &code_lines)
        );
    }

    #[test]
    fn test_convert_from_code_block_into_lines() {
        // no line. "```rs\n```\n".
        {
            let language = Some("rs");
            let lines = vec![];
            let expected = [
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::StartTag,
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::EndTag,
                },
            ]
            .into();
            let output = convert_into_code_block_lines(language, &lines);
            assert_eq2!(output, expected);
        }

        // 1 empty line. "```rs\n\n```\n".
        {
            let language = Some("rs");
            let lines = vec![""];
            let expected = [
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::StartTag,
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::Text(""),
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::EndTag,
                },
            ]
            .into();
            let output = convert_into_code_block_lines(language, &lines);
            assert_eq2!(output, expected);
        }

        // 1 line. "```rs\nlet x = 1;\n```\n".
        {
            let language = Some("rs");
            let lines = vec!["let x = 1;"];
            let expected = [
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::StartTag,
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::Text("let x = 1;"),
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::EndTag,
                },
            ]
            .into();
            let output = convert_into_code_block_lines(language, &lines);
            assert_eq2!(output, expected);
        }

        // 2 lines. "```rs\nlet x = 1;\nlet y = 2;\n```\n".
        {
            let language = Some("rs");
            let lines = vec!["let x = 1;", "let y = 2;"];
            let expected = [
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::StartTag,
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::Text("let x = 1;"),
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::Text("let y = 2;"),
                },
                CodeBlockLine {
                    language: Some("rs"),
                    content: CodeBlockLineContent::EndTag,
                },
            ]
            .into();
            let output = convert_into_code_block_lines(language, &lines);
            assert_eq2!(output, expected);
        }
    }

    #[test]
    fn test_parse_codeblock() {
        // One line: "```bash\npip install foobar\n```\n"
        {
            let lang = "bash";
            let code_lines = vec!["pip install foobar"];
            let input = ["```bash", "pip install foobar", "```", ""].join("\n");
            println!("{:#?}", &input);
            let (remainder, code_block_lines) = parse_fenced_code_block(&input).unwrap();
            assert_eq2!(remainder, "");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }

        // No line: "```bash\n```\n"
        {
            let lang = "bash";
            let code_lines = vec![];
            let input = ["```bash", "```", ""].join("\n");
            let (remainder, code_block_lines) = parse_fenced_code_block(&input).unwrap();
            assert_eq2!(remainder, "");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }

        // 1 empty line: "```bash\n\n```\n"
        {
            let lang = "bash";
            let code_lines = vec![""];
            let input = ["```bash", "", "```", ""].join("\n");
            let (remainder, code_block_lines) = parse_fenced_code_block(&input).unwrap();
            assert_eq2!(remainder, "");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }

        // Multiple lines.
        // "import foobar\n\nfoobar.pluralize('word') # returns
        // 'words'\nfoobar.pluralize('goose') # returns
        // 'geese'\nfoobar.singularize('phenomena') # returns 'phenomenon'\n```"
        {
            let lang = "python";
            let code_lines = vec![
                "import foobar",
                "",
                "foobar.pluralize('word') # returns 'words'",
                "foobar.pluralize('goose') # returns 'geese'",
                "foobar.singularize('phenomena') # returns 'phenomenon'",
            ];
            let input = [
                "```python",
                "import foobar",
                "",
                "foobar.pluralize('word') # returns 'words'",
                "foobar.pluralize('goose') # returns 'geese'",
                "foobar.singularize('phenomena') # returns 'phenomenon'",
                "```",
                "",
            ]
            .join("\n");
            let (remainder, code_block_lines) = parse_fenced_code_block(&input).unwrap();
            assert_eq2!(remainder, "");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }
    }

    #[test]
    fn test_parse_codeblock_no_language() {
        let lang = None;
        let code_lines = vec!["pip install foobar"];
        let input = ["```", "pip install foobar", "```", ""].join("\n");
        let (remainder, code_block_lines) = parse_fenced_code_block(&input).unwrap();
        assert_eq2!(remainder, "");
        assert_eq2!(
            code_block_lines,
            convert_into_code_block_lines(lang, &code_lines)
        );
    }

    #[test]
    fn test_parse_codeblock_with_null_padding() {
        // Code block followed by null padding.
        {
            let lang = "python";
            let code_lines = vec!["import foo", "bar()"];
            let input = "```python\nimport foo\nbar()\n```\n\0\0\0";
            let (remainder, code_block_lines) = parse_fenced_code_block(input).unwrap();
            assert_eq2!(remainder, "");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }

        // Code block with null padding and more content after.
        {
            let lang = "bash";
            let code_lines = vec!["pip install foobar"];
            let input = "```bash\npip install foobar\n```\0\0\0\nNext line";
            let (remainder, code_block_lines) = parse_fenced_code_block(input).unwrap();
            assert_eq2!(remainder, "\0\0\0\nNext line");
            assert_eq2!(
                code_block_lines,
                convert_into_code_block_lines(Some(lang), &code_lines)
            );
        }
    }
}