use portable_pty::CommandBuilder;
use ratatui_testlib::{Result, TuiTestHarness};
use std::time::Duration;
fn main() -> Result<()> {
println!("=== Basic ratatui_testlib Example ===\n");
example_1_simple_echo()?;
example_2_multiline_output()?;
example_3_wait_for_text()?;
example_4_builder_pattern()?;
example_5_cursor_tracking()?;
println!("\n=== All Examples Completed Successfully ===");
Ok(())
}
fn example_1_simple_echo() -> Result<()> {
println!("--- Example 1: Simple Echo Command ---");
let mut harness = TuiTestHarness::new(80, 24)?;
println!("Created terminal: 80 columns x 24 rows");
let mut cmd = CommandBuilder::new("echo");
cmd.arg("Hello from ratatui_testlib!");
harness.spawn(cmd)?;
println!("Spawned: echo 'Hello from ratatui_testlib!'");
std::thread::sleep(Duration::from_millis(100));
harness.update_state()?;
let contents = harness.screen_contents();
println!("\nScreen contents:");
println!("┌{:─<80}┐", "");
for line in contents.lines().take(3) {
println!("│{:<80}│", line);
}
println!("└{:─<80}┘", "");
let (row, col) = harness.cursor_position();
println!("Cursor position: row={}, col={}", row, col);
assert!(contents.contains("Hello from ratatui_testlib!"),
"Expected text not found in output");
std::thread::sleep(Duration::from_millis(100));
if !harness.is_running() {
println!("Process has exited");
}
println!();
Ok(())
}
fn example_2_multiline_output() -> Result<()> {
println!("--- Example 2: Multi-line Output ---");
let mut harness = TuiTestHarness::new(80, 24)?;
let mut cmd = CommandBuilder::new("printf");
cmd.arg("Line 1: First\\nLine 2: Second\\nLine 3: Third");
harness.spawn(cmd)?;
println!("Spawned: printf with 3 lines");
std::thread::sleep(Duration::from_millis(100));
harness.update_state()?;
let contents = harness.screen_contents();
println!("\nScreen contents:");
println!("┌{:─<80}┐", "");
for (i, line) in contents.lines().take(5).enumerate() {
println!("│{:2} {:<77}│", i, line);
}
println!("└{:─<80}┘", "");
assert!(contents.contains("Line 1: First"),
"First line not found");
assert!(contents.contains("Line 2: Second"),
"Second line not found");
assert!(contents.contains("Line 3: Third"),
"Third line not found");
println!("All three lines captured successfully");
println!();
Ok(())
}
fn example_3_wait_for_text() -> Result<()> {
println!("--- Example 3: Waiting for Text ---");
let mut harness = TuiTestHarness::new(80, 24)?;
let mut cmd = CommandBuilder::new("sh");
cmd.arg("-c");
cmd.arg("sleep 0.2 && echo 'Ready!'");
harness.spawn(cmd)?;
println!("Spawned: delayed echo command");
println!("Waiting for 'Ready!' to appear...");
harness.wait_for_text("Ready!")?;
println!("✓ Text 'Ready!' appeared on screen");
let contents = harness.screen_contents();
println!("\nFinal screen contents:");
println!("┌{:─<80}┐", "");
for line in contents.lines().take(3) {
println!("│{:<80}│", line);
}
println!("└{:─<80}┘", "");
println!();
Ok(())
}
fn example_4_builder_pattern() -> Result<()> {
println!("--- Example 4: Builder Pattern Configuration ---");
let mut harness = TuiTestHarness::builder()
.with_size(100, 30) .with_timeout(Duration::from_secs(10)) .with_poll_interval(Duration::from_millis(50)) .with_buffer_size(8192) .build()?;
println!("Created terminal with custom configuration:");
println!(" Size: 100x30");
println!(" Timeout: 10 seconds");
println!(" Poll interval: 50ms");
println!(" Buffer size: 8KB");
let mut cmd = CommandBuilder::new("echo");
cmd.arg("Custom configuration test");
harness.spawn(cmd)?;
std::thread::sleep(Duration::from_millis(100));
harness.update_state()?;
let (width, height) = harness.state().size();
println!("\nVerified terminal size: {}x{}", width, height);
assert_eq!(width, 100);
assert_eq!(height, 30);
let contents = harness.screen_contents();
assert!(contents.contains("Custom configuration test"));
println!("✓ Custom terminal configuration working correctly");
println!();
Ok(())
}
fn example_5_cursor_tracking() -> Result<()> {
println!("--- Example 5: Cursor Position Tracking ---");
let mut harness = TuiTestHarness::new(80, 24)?;
let mut cmd = CommandBuilder::new("printf");
cmd.arg("\\033[5;10HX");
harness.spawn(cmd)?;
println!("Spawned: printf with cursor movement escape sequence");
println!("Sent: ESC[5;10H (move to row 5, col 10)");
std::thread::sleep(Duration::from_millis(100));
harness.update_state()?;
let (row, col) = harness.cursor_position();
println!("\nCursor position after movement:");
println!(" Row: {} (0-based)", row);
println!(" Col: {} (0-based)", col);
println!("\nNote: Escape sequences use 1-based indexing,");
println!(" but ratatui_testlib returns 0-based positions.");
let contents = harness.screen_contents();
println!("\nScreen contents (showing rows 3-6):");
println!("┌{:─<80}┐", "");
for (i, line) in contents.lines().enumerate().skip(3).take(4) {
println!("│{:2} {:<77}│", i, line);
}
println!("└{:─<80}┘", "");
println!("✓ Cursor tracking demonstration complete");
println!();
Ok(())
}