fast-rich 0.3.2

A Rust port of Python's Rich library for beautiful terminal formatting
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
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
//! Console abstraction for terminal output.
//!
//! The `Console` type is the main entry point for rich terminal output.
//! It handles styled printing, word wrapping, and terminal capabilities.
//!
//! # Examples
//!
//! ```no_run
//! use fast_rich::Console;
//!
//! let console = Console::new();
//! console.print("Hello, [bold magenta]World[/]!");
//! ```

use crate::markup;
use crate::renderable::{Renderable, Segment};
use crate::text::{Span, Text};

use crossterm::{
    execute,
    style::{Attribute, Print, SetAttribute, SetBackgroundColor, SetForegroundColor},
    terminal,
};
use std::io::{self, Write};

/// Escape HTML special characters.
fn html_escape(s: &str) -> String {
    s.replace('&', "&")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Escape SVG special characters.
fn svg_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

/// Rendering context passed to Renderable objects.
#[derive(Debug, Clone)]
pub struct RenderContext {
    /// Available width for rendering.
    pub width: usize,
    /// Available height for rendering (optional).
    pub height: Option<usize>,
}

impl Default for RenderContext {
    fn default() -> Self {
        RenderContext {
            width: 80,
            height: None,
        }
    }
}

/// Color system capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorSystem {
    /// No color support
    NoColor,
    /// Standard 8/16 colors
    #[default]
    Standard,
    /// 256 colors
    EightBit,
    /// True color (16 million colors)
    TrueColor,
    /// Windows legacy console (mapped to Standard for ANSI output)
    Windows,
}

/// The main console type for rich terminal output.
#[derive(Debug)]
pub struct Console {
    /// Output stream (stdout or stderr)
    output: ConsoleOutput,
    /// Terminal width (cached or forced)
    width: Option<usize>,
    /// Whether to force color output
    force_color: bool,
    /// Whether color is enabled
    color_enabled: bool,
    /// The detected or forced color system
    color_system: ColorSystem,
    /// Whether to use markup parsing
    markup: bool,
    /// Whether to translate emoji shortcodes
    emoji: bool,
    /// Soft wrap text at terminal width
    soft_wrap: bool,
    /// Whether recording is enabled
    record: std::sync::Arc<std::sync::atomic::AtomicBool>,
    /// Buffer for recorded segments
    recording: std::sync::Arc<std::sync::Mutex<Vec<Segment>>>,
}

#[derive(Debug, Clone)]
enum ConsoleOutput {
    Stdout,
    Stderr,
    Buffer(std::sync::Arc<std::sync::Mutex<Vec<u8>>>),
}

struct BufferWriter {
    buffer: std::sync::Arc<std::sync::Mutex<Vec<u8>>>,
}

impl Write for BufferWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut lock = self
            .buffer
            .lock()
            .map_err(|e| io::Error::other(e.to_string()))?;
        lock.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl Default for Console {
    fn default() -> Self {
        Self::new()
    }
}

impl Console {
    /// Create a new Console writing to stdout.
    pub fn new() -> Self {
        let (color_enabled, color_system) = Self::detect_color_system();
        Console {
            output: ConsoleOutput::Stdout,
            width: None,
            force_color: false,
            color_enabled,
            color_system,
            markup: true,
            emoji: true,
            soft_wrap: true,
            record: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
            recording: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
        }
    }

    /// Create a new Console writing to stderr.
    pub fn stderr() -> Self {
        let (color_enabled, color_system) = Self::detect_color_system();
        Console {
            output: ConsoleOutput::Stderr,
            width: None,
            force_color: false,
            color_enabled,
            color_system,
            markup: true,
            emoji: true,
            soft_wrap: true,
            record: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
            recording: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
        }
    }

    /// Create a new Console that captures output to memory.
    ///
    /// Useful for testing output verification.
    pub fn capture() -> Self {
        Console {
            output: ConsoleOutput::Buffer(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
            width: Some(80),   // Default width for tests
            force_color: true, // Force color for tests
            color_enabled: true,
            color_system: ColorSystem::TrueColor, // Capture assumes good capabilities
            markup: true,
            emoji: true,
            soft_wrap: true,
            record: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
            recording: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
        }
    }

    /// Get the captured output as a string (if using capture mode).
    pub fn get_captured_output(&self) -> String {
        match &self.output {
            ConsoleOutput::Buffer(buf) => {
                let lock = buf.lock().unwrap();
                String::from_utf8(lock.clone()).unwrap_or_default()
            }
            _ => String::new(),
        }
    }

    /// Set a fixed terminal width.
    pub fn width(mut self, width: usize) -> Self {
        self.width = Some(width);
        self
    }

    /// Force color output even when not detected.
    pub fn force_color(mut self, force: bool) -> Self {
        self.force_color = force;
        if force {
            self.color_enabled = true;
            // If forcing color and we were previously NoColor, assume Standard
            if self.color_system == ColorSystem::NoColor {
                self.color_system = ColorSystem::Standard;
            }
        }
        self
    }

    /// Set the color system explicitly.
    pub fn color_system(mut self, system: ColorSystem) -> Self {
        self.color_system = system;
        // If explicitly setting a color system (other than NoColor), enable color
        self.color_enabled = system != ColorSystem::NoColor;
        self
    }

    /// Enable or disable markup parsing.
    pub fn markup(mut self, enabled: bool) -> Self {
        self.markup = enabled;
        self
    }

    /// Enable or disable emoji shortcode translation.
    pub fn emoji(mut self, enabled: bool) -> Self {
        self.emoji = enabled;
        self
    }

    /// Enable or disable soft word wrapping.
    pub fn soft_wrap(mut self, enabled: bool) -> Self {
        self.soft_wrap = enabled;
        self
    }

    /// Enable or disable recording of output.
    pub fn record(self, enabled: bool) -> Self {
        self.record
            .store(enabled, std::sync::atomic::Ordering::Relaxed);
        self
    }

    /// Start recording output.
    pub fn start_recording(&self) {
        self.record
            .store(true, std::sync::atomic::Ordering::Relaxed);
        if let Ok(mut lock) = self.recording.lock() {
            lock.clear();
        }
    }

    /// Stop recording output.
    pub fn stop_recording(&self) {
        self.record
            .store(false, std::sync::atomic::Ordering::Relaxed);
    }

    /// Get the current terminal width.
    pub fn get_width(&self) -> usize {
        self.width
            .unwrap_or_else(|| terminal::size().map(|(w, _)| w as usize).unwrap_or(80))
    }

    /// Detect color support and system.
    fn detect_color_system() -> (bool, ColorSystem) {
        // Check common environment variables
        if std::env::var("NO_COLOR").is_ok() {
            return (false, ColorSystem::NoColor);
        }

        if std::env::var("FORCE_COLOR").is_ok() {
            // Default to Standard if forced, can be upgraded by other checks if we were smarter,
            // but for now FORCE_COLOR just ensures we have *some* color.
            return (true, ColorSystem::Standard);
        }

        // Check COLORTERM for truecolor
        if let Ok(colorterm) = std::env::var("COLORTERM") {
            if colorterm.contains("truecolor") || colorterm.contains("24bit") {
                return (true, ColorSystem::TrueColor);
            }
        }

        // Check TERM for 256 colors
        if let Ok(term) = std::env::var("TERM") {
            if term.contains("256color") {
                return (true, ColorSystem::EightBit);
            }
        }

        // Fallback to Standard color if TTY (simplified)
        // In a real app we'd check is_tty
        (true, ColorSystem::Standard)
    }

    /// Print a string with markup support.
    pub fn print(&self, content: &str) {
        let text = if self.markup {
            markup::parse(content)
        } else {
            Text::plain(content.to_string())
        };

        self.print_renderable(&text);
    }

    /// Print any renderable object.
    pub fn print_renderable(&self, renderable: &dyn Renderable) {
        let context = RenderContext {
            width: self.get_width(),
            height: None,
        };

        let segments = renderable.render(&context);
        self.write_segments(&segments);
    }

    /// Print a line (with newline at the end).
    pub fn println(&self, content: &str) {
        self.print(content);
        self.newline();
    }

    /// Print a string without markup parsing.
    ///
    /// Use this when printing content that may contain brackets `[...]`
    /// that should NOT be interpreted as markup (e.g., debug output).
    pub fn print_raw(&self, content: &str) {
        let text = Text::plain(content.to_string());
        self.print_renderable(&text);
    }

    /// Print a line without markup parsing (with newline at the end).
    ///
    /// Use this when printing content that may contain brackets `[...]`
    /// that should NOT be interpreted as markup (e.g., debug output).
    pub fn println_raw(&self, content: &str) {
        self.print_raw(content);
        self.newline();
    }

    /// Print an empty line.
    pub fn newline(&self) {
        let _ = self.write_raw("\n");
        // Record newline segment if recording
        if self.record.load(std::sync::atomic::Ordering::Relaxed) {
            if let Ok(mut lock) = self.recording.lock() {
                lock.push(Segment::empty_line());
            }
        }
    }

    /// Write segments to the output.
    pub(crate) fn write_segments(&self, segments: &[Segment]) {
        if self.record.load(std::sync::atomic::Ordering::Relaxed) {
            if let Ok(mut lock) = self.recording.lock() {
                lock.extend_from_slice(segments);
            }
        }

        for segment in segments {
            for span in &segment.spans {
                self.write_span(span);
            }
            if segment.newline {
                let _ = self.write_raw("\n");
            }
        }
        let _ = self.flush();
    }

    /// Write a single span with styling.
    fn write_span(&self, span: &Span) {
        if !self.color_enabled || self.color_system == ColorSystem::NoColor || span.style.is_empty()
        {
            let _ = self.write_raw(&span.text);
            return;
        }

        let mut writer = self.get_writer();

        // Helper to downsample colors based on system
        let process_color = |color: crate::style::Color| -> crossterm::style::Color {
            match self.color_system {
                ColorSystem::Standard | ColorSystem::Windows => color.to_standard().to_crossterm(),
                ColorSystem::EightBit => color.to_ansi256().to_crossterm(),
                ColorSystem::TrueColor => color.to_crossterm(),
                ColorSystem::NoColor => crossterm::style::Color::Reset, // Should be handled by early return
            }
        };

        // Set foreground color
        if let Some(color) = span.style.foreground {
            if matches!(
                self.color_system,
                ColorSystem::Standard | ColorSystem::Windows
            ) {
                let std_color = color.to_standard();
                let sgr = std_color.to_sgr_fg();
                if !sgr.is_empty() {
                    let _ = self.write_raw(&sgr);
                } else {
                    let _ = execute!(writer, SetForegroundColor(std_color.to_crossterm()));
                }
            } else {
                let _ = execute!(writer, SetForegroundColor(process_color(color)));
            }
        }

        // Set background color
        if let Some(color) = span.style.background {
            if matches!(
                self.color_system,
                ColorSystem::Standard | ColorSystem::Windows
            ) {
                let std_color = color.to_standard();
                let sgr = std_color.to_sgr_bg();
                if !sgr.is_empty() {
                    let _ = self.write_raw(&sgr);
                } else {
                    let _ = execute!(writer, SetBackgroundColor(std_color.to_crossterm()));
                }
            } else {
                let _ = execute!(writer, SetBackgroundColor(process_color(color)));
            }
        }

        // Set attributes
        if span.style.bold {
            let _ = execute!(writer, SetAttribute(Attribute::Bold));
        }
        if span.style.dim {
            let _ = execute!(writer, SetAttribute(Attribute::Dim));
        }
        if span.style.italic {
            let _ = execute!(writer, SetAttribute(Attribute::Italic));
        }
        if span.style.underline {
            let _ = execute!(writer, SetAttribute(Attribute::Underlined));
        }
        if span.style.blink {
            let _ = execute!(writer, SetAttribute(Attribute::SlowBlink));
        }
        if span.style.reverse {
            let _ = execute!(writer, SetAttribute(Attribute::Reverse));
        }
        if span.style.hidden {
            let _ = execute!(writer, SetAttribute(Attribute::Hidden));
        }
        if span.style.strikethrough {
            let _ = execute!(writer, SetAttribute(Attribute::CrossedOut));
        }

        // Write the text
        let _ = execute!(writer, Print(&span.text));

        // Reset all attributes (SGR 0 includes color reset)
        let _ = execute!(writer, SetAttribute(Attribute::Reset));
    }

    /// Get the writer for this console.
    fn get_writer(&self) -> Box<dyn Write> {
        match &self.output {
            ConsoleOutput::Stdout => Box::new(io::stdout()),
            ConsoleOutput::Stderr => Box::new(io::stderr()),
            ConsoleOutput::Buffer(buf) => Box::new(BufferWriter {
                buffer: buf.clone(),
            }),
        }
    }

    /// Write raw string to output.
    fn write_raw(&self, s: &str) -> io::Result<()> {
        match &self.output {
            ConsoleOutput::Stdout => {
                let mut stdout = io::stdout();
                stdout.write_all(s.as_bytes())
            }
            ConsoleOutput::Stderr => {
                let mut stderr = io::stderr();
                stderr.write_all(s.as_bytes())
            }
            ConsoleOutput::Buffer(buf) => {
                let mut lock = buf.lock().map_err(|e| io::Error::other(e.to_string()))?;
                lock.extend_from_slice(s.as_bytes());
                Ok(())
            }
        }
    }

    /// Flush the output.
    fn flush(&self) -> io::Result<()> {
        match &self.output {
            ConsoleOutput::Stdout => io::stdout().flush(),
            ConsoleOutput::Stderr => io::stderr().flush(),
            ConsoleOutput::Buffer(_) => Ok(()),
        }
    }

    /// Clear the screen.
    pub fn clear(&self) {
        let mut writer = self.get_writer();
        let _ = execute!(
            writer,
            crossterm::terminal::Clear(crossterm::terminal::ClearType::All),
            crossterm::cursor::MoveTo(0, 0)
        );
    }

    /// Show or hide the cursor.
    pub fn show_cursor(&self, show: bool) {
        let mut writer = self.get_writer();
        if show {
            let _ = execute!(writer, crossterm::cursor::Show);
        } else {
            let _ = execute!(writer, crossterm::cursor::Hide);
        }
    }

    /// Move the cursor up by `n` lines.
    pub fn move_cursor_up(&self, n: u16) {
        if n > 0 {
            let mut writer = self.get_writer();
            let _ = execute!(writer, crossterm::cursor::MoveUp(n));
        }
    }

    /// Move the cursor down by `n` lines.
    pub fn move_cursor_down(&self, n: u16) {
        if n > 0 {
            let mut writer = self.get_writer();
            let _ = execute!(writer, crossterm::cursor::MoveDown(n));
        }
    }

    /// Clear the current line.
    pub fn clear_line(&self) {
        let mut writer = self.get_writer();
        let _ = execute!(
            writer,
            crossterm::terminal::Clear(crossterm::terminal::ClearType::CurrentLine),
            crossterm::cursor::MoveToColumn(0)
        );
    }

    /// Show a rule (horizontal line).
    pub fn rule(&self, title: &str) {
        let _width = self.get_width();
        let rule = crate::rule::Rule::new(title);
        self.print_renderable(&rule);
        self.newline();
    }

    /// Print JSON with syntax highlighting.
    ///
    /// This method prints a JSON string with automatic syntax highlighting.
    /// The input should be a valid JSON string.
    #[cfg(feature = "syntax")]
    pub fn print_json(&self, json_str: &str) {
        let syntax = crate::syntax::Syntax::new(json_str, "json");
        self.print_renderable(&syntax);
        self.newline();
    }

    /// Pretty print a debug-printable object.
    ///
    /// Uses syntax highlighting if the `syntax` feature is enabled.
    pub fn print_debug<T: std::fmt::Debug>(&self, obj: &T) {
        let content = format!("{:#?}", obj);

        #[cfg(feature = "syntax")]
        {
            let syntax = crate::syntax::Syntax::new(&content, "rust");
            self.print_renderable(&syntax);
        }

        #[cfg(not(feature = "syntax"))]
        {
            // Use Text::plain to avoid parsing brackets as markup
            let text = Text::plain(content);
            self.print_renderable(&text);
        }

        self.newline();
    }

    /// Export a renderable as plain text.
    ///
    /// Returns the plain text representation without any ANSI codes.
    pub fn export_text(&self, renderable: &dyn Renderable) -> String {
        let context = RenderContext {
            width: self.get_width(),
            height: None,
        };
        let segments = renderable.render(&context);
        self.segments_to_text(&segments)
    }

    fn segments_to_text(&self, segments: &[Segment]) -> String {
        let mut result = String::new();
        for segment in segments {
            result.push_str(&segment.plain_text());
            if segment.newline {
                result.push('\n');
            }
        }
        result
    }

    /// Export a renderable as HTML with inline styles.
    ///
    /// Returns an HTML string with styled `<span>` elements.
    pub fn export_html(&self, renderable: &dyn Renderable) -> String {
        let context = RenderContext {
            width: self.get_width(),
            height: None,
        };
        let segments = renderable.render(&context);
        self.segments_to_html(&segments)
    }

    /// Save the recorded output as HTML.
    pub fn save_html(&self, path: &str) -> io::Result<()> {
        let segments = self.recording.lock().unwrap();
        let html = self.segments_to_html(&segments);
        std::fs::write(path, html)
    }

    fn segments_to_html(&self, segments: &[Segment]) -> String {
        let mut html = String::from("<pre style=\"font-family: monospace; background: #1e1e1e; color: #d4d4d4; padding: 1em;\">\n");

        for segment in segments {
            for span in &segment.spans {
                let style_css = span.style.to_css();
                if style_css.is_empty() {
                    html.push_str(&html_escape(&span.text));
                } else {
                    html.push_str(&format!(
                        "<span style=\"{}\">{}</span>",
                        style_css,
                        html_escape(&span.text)
                    ));
                }
            }
            if segment.newline {
                html.push('\n');
            }
        }

        html.push_str("</pre>");
        html
    }

    /// Export a renderable as SVG.
    ///
    /// Returns an SVG string with text elements.
    pub fn export_svg(&self, renderable: &dyn Renderable) -> String {
        let context = RenderContext {
            width: self.get_width(),
            height: None,
        };
        let segments = renderable.render(&context);
        self.segments_to_svg(&segments)
    }

    /// Save the recorded output as SVG.
    pub fn save_svg(&self, path: &str) -> io::Result<()> {
        let segments = self.recording.lock().unwrap();
        let svg = self.segments_to_svg(&segments);
        std::fs::write(path, svg)
    }

    fn segments_to_svg(&self, segments: &[Segment]) -> String {
        let char_width = 9.6; // Approximate monospace character width
        let line_height = 20.0;
        let padding = 10.0;

        let mut lines: Vec<String> = Vec::new();
        let mut current_line = String::new();

        for segment in segments {
            for span in &segment.spans {
                current_line.push_str(&span.text);
            }
            if segment.newline {
                lines.push(std::mem::take(&mut current_line));
            }
        }
        if !current_line.is_empty() {
            lines.push(current_line);
        }

        let max_chars = lines.iter().map(|l| l.len()).max().unwrap_or(80);
        let width = (max_chars as f64 * char_width) + padding * 2.0;
        let height = (lines.len() as f64 * line_height) + padding * 2.0;

        let mut svg = format!(
            "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 {:.0} {:.0}\">\n",
            width, height
        );
        svg.push_str("  <rect width=\"100%\" height=\"100%\" fill=\"#1e1e1e\"/>\n");
        svg.push_str("  <text font-family=\"monospace\" font-size=\"14\" fill=\"#d4d4d4\">\n");

        for (i, line) in lines.iter().enumerate() {
            let y = padding + (i as f64 + 1.0) * line_height;
            svg.push_str(&format!(
                "    <tspan x=\"{}\" y=\"{:.1}\">{}</tspan>\n",
                padding,
                y,
                svg_escape(line)
            ));
        }

        svg.push_str("  </text>\n</svg>");
        svg
    }
}

/// A guard that captures output for testing.
#[derive(Debug)]
pub struct CapturedOutput {
    segments: Vec<Segment>,
}

impl CapturedOutput {
    /// Create a new capture.
    pub fn new() -> Self {
        CapturedOutput {
            segments: Vec::new(),
        }
    }

    /// Get the plain text output.
    pub fn plain_text(&self) -> String {
        let mut result = String::new();
        for segment in &self.segments {
            result.push_str(&segment.plain_text());
            if segment.newline {
                result.push('\n');
            }
        }
        result
    }
}

impl Default for CapturedOutput {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_console_default_width() {
        let console = Console::new().width(80);
        assert_eq!(console.get_width(), 80);
    }

    #[test]
    fn test_render_context_default() {
        let context = RenderContext::default();
        assert_eq!(context.width, 80);
    }

    #[test]
    fn test_force_color() {
        let console = Console::new().force_color(true);
        assert!(console.force_color);
        assert!(console.color_enabled);
    }
}