fast_rich/
lib.rs

1//! # fast-rich
2//!
3//! A Rust port of Python's [Rich](https://github.com/Textualize/rich) library
4//! for beautiful terminal formatting.
5//!
6//! ## Features
7//!
8//! - **Rich text** with colors, styles, and markup
9//! - **Tables** with Unicode borders and auto-sizing
10//! - **Progress bars** with multiple tasks, spinners, and customizable columns
11//! - **Live Display** for flicker-free auto-updating content
12//! - **Logging** handler for colorful structured logs
13//! - **Tree views** for hierarchical data
14//! - **Panels** and **Rules** for visual organization
15//! - **Markdown** rendering (optional)
16//! - **Syntax highlighting** (optional)
17//! - **Pretty tracebacks** for better error display
18//!
19//! ## Quick Start
20//!
21//! ```no_run
22//! use fast_rich::prelude::*;
23//!
24//! let console = Console::new();
25//!
26//! // Simple styled output
27//! console.print("Hello, [bold magenta]World[/]!");
28//!
29//! // Tables
30//! let mut table = Table::new();
31//! table.add_column("Name");
32//! table.add_column("Age");
33//! table.add_row_strs(&["Alice", "30"]);
34//! console.print_renderable(&table);
35//! ```
36
37// Core modules
38pub mod align;
39pub mod bar;
40pub mod box_drawing;
41pub mod console;
42pub mod emoji;
43pub mod group;
44pub mod highlighter;
45pub mod markup;
46pub mod measure;
47pub mod nested_progress;
48pub mod padding;
49pub mod pager;
50pub mod renderable;
51pub mod screen;
52pub mod style;
53pub mod text;
54pub mod theme;
55
56// Renderables
57pub mod columns;
58pub mod filesize;
59pub mod layout;
60pub mod live;
61pub mod log;
62pub mod panel;
63pub mod rule;
64pub mod table;
65pub mod tree;
66
67// Progress
68pub mod progress;
69
70// Utilities
71pub mod inspect;
72pub mod prompt;
73pub mod traceback;
74
75// Optional feature-gated modules
76#[cfg(feature = "markdown")]
77pub mod markdown;
78
79#[cfg(feature = "syntax")]
80pub mod syntax;
81
82// Re-exports for convenience
83pub use console::Console;
84pub use layout::Layout;
85pub use live::Live;
86pub use panel::{BorderStyle, Panel};
87pub use renderable::Renderable;
88pub use rule::Rule;
89pub use style::{Color, Style};
90pub use table::{Column, ColumnAlign, Table};
91pub use text::{Alignment, Text};
92pub use tree::{Tree, TreeNode};
93
94/// Prelude module for convenient imports.
95pub mod prelude {
96    pub use crate::columns::Columns;
97    pub use crate::console::Console;
98    pub use crate::inspect::{inspect, InspectConfig};
99    pub use crate::log::ConsoleLog;
100    pub use crate::panel::{BorderStyle, Panel};
101    pub use crate::progress::{track, Progress, ProgressBar, Spinner, SpinnerStyle, Status};
102    pub use crate::renderable::Renderable;
103    pub use crate::rule::Rule;
104    pub use crate::style::{Color, Style};
105    pub use crate::table::{Column, ColumnAlign, Table};
106    pub use crate::text::{Alignment, Text};
107    pub use crate::traceback::install_panic_hook;
108    pub use crate::tree::{GuideStyle, Tree, TreeNode};
109}
110
111/// Print text with markup to stdout.
112///
113/// This is a convenience function for quick output.
114///
115/// # Example
116///
117/// ```no_run
118/// fast_rich::print("[bold green]Success![/]");
119/// ```
120pub fn print(content: &str) {
121    Console::new().print(content);
122}
123
124/// Print text with markup to stdout, followed by a newline.
125pub fn println(content: &str) {
126    Console::new().println(content);
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn test_console_creation() {
135        let console = Console::new();
136        assert!(console.get_width() > 0);
137    }
138
139    #[test]
140    fn test_style_builder() {
141        let style = Style::new().foreground(Color::Red).bold().underline();
142
143        assert!(style.bold);
144        assert!(style.underline);
145    }
146
147    #[test]
148    fn test_text_creation() {
149        let text = Text::plain("Hello, World!");
150        assert_eq!(text.plain_text(), "Hello, World!");
151    }
152
153    #[test]
154    fn test_table_creation() {
155        let mut table = Table::new();
156        table.add_column("Col1");
157        table.add_column("Col2");
158        table.add_row_strs(&["a", "b"]);
159
160        // Table should have columns and rows
161        assert!(!table
162            .render(&console::RenderContext {
163                width: 40,
164                height: None
165            })
166            .is_empty());
167    }
168}