fancy-table 0.4.1

Fancy tables with bells and whistles
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::cmp::min;

// SmallVec for more efficient ANSI code collection (for common cases)
use smallvec::SmallVec;

pub const RST_CODE: &str = "\x1b[0m";

// open SGRs, text slice, total length (excluding ANSI codes), has reset code?, needs reset?
// Using SmallVec for better performance in the common case of few ANSI codes
type AnsiSegment<'a> = (SmallVec<[&'a str; 4]>, &'a str, usize, bool, bool);

#[derive(Default)]
pub struct AnsiString<'a> {
    pub slice: &'a str,
    pub c2c: Option<String>,
    pub len: usize,
    pub needs_rst: bool,
}

#[derive(PartialEq)]
enum AnsiToken {
    Escape,
    Opening,
    Code,
}

#[derive(PartialEq)]
pub enum Overflow {
    WordWrap,
    Truncate,
}

#[derive(Debug)]
enum Segment<'a> {
    Word(&'a str, usize),
    Term(&'a str, usize),
}
#[derive(Debug, Default)]
struct CodeQueue<'a> {
    codes_to_continue: SmallVec<[&'a str; 4]>, // Most common case: few ANSI codes
    codes_to_collect: SmallVec<[&'a str; 4]>,
    reset_after_get: bool,
}

impl<'a> CodeQueue<'a> {
    pub fn codes_to_continue(&mut self) -> Option<String> {
        let code_seq = if self.codes_to_continue.is_empty() {
            None
        } else {
            let capacity = self.codes_to_continue.iter().map(|s| s.len()).sum();
            let mut codes = String::with_capacity(capacity);

            for s in &self.codes_to_continue {
                codes.push_str(s);
            }
            Some(codes)
        };
        if self.reset_after_get {
            self.codes_to_continue.clear();
            self.reset_after_get = false;
        }

        // Codes collected become codes to continue in case of line breaks
        self.codes_to_continue
            .extend_from_slice(&self.codes_to_collect);
        self.codes_to_collect.clear();

        code_seq
    }

    pub fn collect(&mut self, codes: SmallVec<[&'a str; 4]>) {
        self.codes_to_collect.extend(codes);
    }

    pub fn clear(&mut self) {
        self.reset_after_get = true;
        self.codes_to_collect.clear();
    }

    pub fn has_codes_to_continue(&self) -> bool {
        !self.codes_to_continue.is_empty()
    }
}

impl<'a> Segment<'a> {
    fn text(&self) -> &'a str {
        match self {
            Segment::Word(txt, _) | Segment::Term(txt, _) => txt,
        }
    }
    fn pos(&self) -> usize {
        match self {
            Segment::Word(_, pos) | Segment::Term(_, pos) => *pos,
        }
    }
}

impl<'a> AnsiString<'a> {
    pub fn build(c2c: Option<String>, slice: &'a str, len: usize, needs_rst: bool) -> Self {
        Self {
            slice,
            len,
            c2c,
            needs_rst,
        }
    }
}

/// Processes input text with ANSI codes into formatted lines that fit within specified dimensions.
///
/// Takes raw text containing ANSI escape sequences and breaks it into lines that respect
/// both horizontal (character) and vertical (line) constraints while preserving ANSI formatting.
///
/// # Arguments
/// * `input` - Raw input text that may contain ANSI escape sequences
/// * `hspace` - Maximum characters per line (excluding ANSI codes)
/// * `vspace` - Maximum number of lines to generate
/// * `overflow` - Strategy for handling text that exceeds horizontal space
///
/// # Returns
/// Vector of `AnsiString` objects, each representing a formatted line with:
/// - Original text slice (may include ANSI codes)
/// - Continuation codes needed to maintain formatting across line breaks
/// - Actual display length (excluding ANSI codes)
/// - Whether the line needs a reset code for proper formatting
pub fn build_string<'a>(
    input: &'a str,
    hspace: usize,
    vspace: usize,
    overflow: &Overflow,
) -> Vec<AnsiString<'a>> {
    if hspace == 0 {
        return vec![];
    }

    // Tracks whether the current line needs a reset code to properly close ANSI formatting
    let mut line_reset = false;

    // A queue keeping ANSI codes to be carried over to the next line
    let mut queue = CodeQueue::default();
    let mut result = Vec::with_capacity(vspace);
    let mut str_pos = 0;
    let mut end_pos = 0;
    let mut txt_len = 0;

    let mut segments = build_segments_iter(input, overflow).peekable();
    while let Some(seg) = segments.next() {
        let is_last_str = segments.peek().is_none();
        let is_init_str = txt_len == 0;
        let is_term_str = matches!(seg, Segment::Term(_, _));
        let eol = is_last_str || is_term_str;
        let pos = seg.pos();

        let (new_codes, txt, total_len, has_rst, needs_rst) = parse_segment(seg, hspace);
        let len = min(total_len, hspace);

        // Separator length: 0 for first segment in line, 1 otherwise.
        let sep_len = (txt_len > 0) as usize;

        if txt_len == 0 {
            str_pos = pos;
            end_pos = pos;
            line_reset = queue.has_codes_to_continue();
        }

        // If there is no more space for a segment then wrap-or-truncate the line
        if !is_init_str && txt_len + total_len + sep_len > hspace {
            result.push(AnsiString::build(
                queue.codes_to_continue(),
                &input[str_pos..end_pos],
                txt_len,
                line_reset,
            ));
            // Constitute current segment as initial in the new line
            str_pos = pos;
            end_pos = pos + txt.len();
            txt_len = len;
        } else {
            end_pos += txt.len() + sep_len;
            txt_len += len + sep_len;
        }

        // If segment contains reset code at any position wipe out
        // all ANSI codes collected up to the reset code so far...
        if has_rst {
            queue.clear();
            line_reset = needs_rst;
        } else {
            line_reset = line_reset || needs_rst;
        }

        // ...and collect all the codes coming right after the reset code
        queue.collect(new_codes);

        if result.len() < vspace && (txt_len == hspace || eol) {
            result.push(AnsiString::build(
                queue.codes_to_continue(),
                &input[str_pos..end_pos],
                txt_len,
                line_reset,
            ));
            txt_len = 0;
        }

        // Bail out early if there is no more vertical space available
        if result.len() == vspace {
            return result;
        }
    }
    result
}

/// Splits input string into segments for parsing based on overflow strategy:
/// - `Overflow::Truncate`: Splits only on newlines, creating one segment per line
/// - `Overflow::WordWrap`: Splits on both newlines and spaces for word-based wrapping
fn build_segments_iter<'a>(
    input: &'a str,
    overflow: &Overflow,
) -> Box<dyn Iterator<Item = Segment<'a>> + 'a> {
    let input_ptr = input.as_ptr();
    match overflow {
        Overflow::Truncate => Box::new(
            input
                .lines()
                .map(move |s| Segment::Term(s, s.as_ptr() as usize - input_ptr as usize)),
        ),
        Overflow::WordWrap => Box::new(input.lines().flat_map(move |s| {
            let mut iter = s.split(' ').peekable();
            std::iter::from_fn(move || {
                iter.next().map(|slice| {
                    let pos = slice.as_ptr() as usize - input_ptr as usize;
                    if iter.peek().is_none() {
                        Segment::Term(slice, pos)
                    } else {
                        Segment::Word(slice, pos)
                    }
                })
            })
        })),
    }
}

/// Parses a single text segment, extracting ANSI codes and enforcing character limits.
///
/// Returns a tuple containing:
/// - SmallVec of ANSI escape sequences found in the segment (optimized for common case of few codes)
/// - Text slice (including ANSI codes) truncated to fit the character limit
/// - Actual text length (excluding ANSI codes)  
/// - Whether a reset code was found in the segment
/// - Whether the segment needs a reset code (has unclosed ANSI styling)
fn parse_segment<'a>(segment: Segment<'a>, len: usize) -> AnsiSegment<'a> {
    let mut codes = SmallVec::new();
    let mut expected = AnsiToken::Escape;
    let mut current_code_start = 0;

    let mut txt_len: usize = 0;
    let mut end_pos = None;
    let mut has_rst = false;
    let mut needs_rst = false;
    let mut stop_collecting = false;

    let input = segment.text();

    // Fast path: if input is empty or very short, avoid expensive processing
    if input.is_empty() {
        return (codes, input, 0, false, false);
    }

    for (pos, ch) in input.char_indices() {
        match ch {
            '\x1b' if expected == AnsiToken::Escape => {
                expected = AnsiToken::Opening;
                current_code_start = pos;
            }
            '[' if expected == AnsiToken::Opening => expected = AnsiToken::Code,
            'm' if expected == AnsiToken::Code => {
                // Valid SGR sequence terminator
                let sequence = &input[current_code_start..pos + 1];
                let seq_rst = sequence == RST_CODE;

                has_rst = seq_rst;

                if seq_rst {
                    codes.clear();
                } else {
                    codes.push(sequence);
                }
                if !stop_collecting {
                    needs_rst = !has_rst;
                    if end_pos.is_some() {
                        end_pos = Some(pos + 1);
                    }
                }
                expected = AnsiToken::Escape
            }
            '0'..='9' | ';' | ':' if expected == AnsiToken::Code => {
                continue;
            }
            _ if end_pos.is_none() => {
                txt_len += 1;

                if txt_len == len {
                    end_pos = Some(pos + ch.len_utf8());
                }
                expected = AnsiToken::Escape;
            }
            _ => {
                stop_collecting = true;
                expected = AnsiToken::Escape;
                // consume, do nothing
            }
        }
    }
    let slice = &input[0..end_pos.unwrap_or(input.len())];
    (codes, slice, txt_len, has_rst, needs_rst)
}

#[macro_export]
macro_rules! assert_ansi_string {
        ($string:expr, $hspace:expr, $vspace:expr, $overflow:expr, []) => {
            let str = format!($string);
            let segments = $crate::ansi::build_string(&str, $hspace, $vspace, &$overflow);

            assert!(segments.is_empty());
        };
        ($string:expr, $hspace:expr, $vspace:expr, $overflow:expr, [$($segment:tt),*]) => {
            {
                let str = format!($string);
                let segments = $crate::ansi::build_string(&str, $hspace, $vspace, &$overflow);
                let mut segment_index = 0;

                $(
                    assert_ansi_string!(@verify_segment segments[segment_index], $segment);
                    segment_index += 1;
                )+
                assert_eq!(segments.len(), segment_index, "Expected {} segments, found {}", segment_index, segments.len());
            }
        };
        (@verify_segment $seg:expr, { $($field:ident => $value:tt),* }) => {
            let seg = &$seg;
            $(
                assert_ansi_string!(@check_field seg, $field, $value);
            )*
        };
        (@check_field $seg:expr, len, $expected:expr) => {
            assert_eq!($seg.len, $expected);
        };
        (@check_field $seg:expr, txt, $expected:literal) => {
            let formatted = format!($expected);
            assert_eq!($seg.slice.as_ref(), formatted);
        };
        (@check_field $seg:expr, rst, $expected:expr) => {
            assert_eq!($seg.needs_rst, $expected);
        };
    }

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

    #[test]
    fn test_codes_queue_clear() {
        let mut queue = CodeQueue::default();
        queue.collect(smallvec!["\x1b[31m", "\x1b[1m"]);

        // First call moves collected to continue
        assert_eq!(queue.codes_to_continue(), None);
        assert!(queue.has_codes_to_continue());

        // Clear should mark for reset
        queue.clear();

        // Next call should clear and return existing codes
        assert_eq!(
            queue.codes_to_continue(),
            Some(String::from("\x1b[31m\x1b[1m"))
        );
        assert!(!queue.has_codes_to_continue());
    }

    #[test]
    fn test_codes_queue_multiple_codes_collected() {
        let mut queue = CodeQueue::default();

        // First collected code.
        // Nothing to be applied at the beginning of current line.
        queue.collect(smallvec!["\x1b[31m"]);
        assert_eq!(queue.codes_to_continue(), None);

        // Second collected code should append to queue of codes to continue
        // but current line should be prepended with previously collected code.
        queue.collect(smallvec!["\x1b[1m"]);
        assert_eq!(queue.codes_to_continue(), Some(String::from("\x1b[31m")));

        // Finally, next call of `codes_to_continue` should generate a sequence
        // of all codes collected so far.
        assert_eq!(
            queue.codes_to_continue(),
            Some(String::from("\x1b[31m\x1b[1m"))
        );
    }

    #[test]
    fn test_codes_queue_clear_with_new_codes() {
        let mut queue = CodeQueue::default();

        // Set up some continuing codes
        queue.collect(smallvec!["\x1b[31m", "\x1b[1m"]);
        queue.codes_to_continue();

        // Collect new codes then clear
        queue.collect(smallvec!["\x1b[32m"]);
        queue.clear();

        // Should get the old continuing codes (before clear) and new codes should be cleared
        assert_eq!(
            queue.codes_to_continue(),
            Some(String::from("\x1b[31m\x1b[1m"))
        );
        assert!(!queue.has_codes_to_continue());
    }

    #[test]
    fn test_codes_queue_empty_states() {
        let mut queue = CodeQueue::default();

        // Empty queue
        assert!(!queue.has_codes_to_continue());
        assert_eq!(queue.codes_to_continue(), None);

        // Empty collection
        queue.collect(smallvec![]);
        assert_eq!(queue.codes_to_continue(), None);
        assert!(!queue.has_codes_to_continue());

        // Clear empty queue
        queue.clear();
        assert_eq!(queue.codes_to_continue(), None);
        assert!(!queue.has_codes_to_continue());
    }
}