use std::time::Duration;
use rust_expect::auto_config::{
PromptConfig, ShellConfig, ShellType, default_shell, detect_from_path, detect_locale,
detect_shell, is_utf8_environment,
};
use rust_expect::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
println!("rust-expect Zero Configuration Example");
println!("======================================\n");
println!("1. Detecting default shell...");
let shell = default_shell();
println!(" Default shell: {shell}");
println!("\n2. Detecting shell type from path...");
let shell_types = [
("/bin/bash", ShellType::Bash),
("/bin/zsh", ShellType::Zsh),
("/bin/sh", ShellType::Sh),
("/usr/bin/fish", ShellType::Fish),
];
for (path, _expected) in shell_types {
let detected = detect_from_path(path);
println!(" {path} -> {detected:?}");
}
println!("\n3. Detecting shell from environment...");
let env_shell = detect_shell();
println!(" Current shell: {env_shell:?}");
println!("\n4. Detecting locale...");
let locale = detect_locale();
println!(" Detected locale: {locale}");
println!(" UTF-8 environment: {}", is_utf8_environment());
let locale_vars = ["LANG", "LC_ALL", "LC_CTYPE"];
for var in locale_vars {
if let Ok(value) = std::env::var(var) {
println!(" {var}: {value}");
}
}
println!("\n5. Shell configuration...");
let _config = ShellConfig::default();
println!(" Default shell config created");
println!(" Useful for setting up sessions with sensible defaults");
println!("\n6. Common prompt patterns...");
let prompts = [
("$ ", "POSIX shell"),
("# ", "Root shell"),
("> ", "Generic prompt"),
(">>> ", "Python REPL"),
("mysql> ", "MySQL client"),
];
for (prompt, name) in prompts {
println!(" '{prompt}' - {name}");
}
println!("\n7. Automatic session setup...");
let mut session = Session::spawn(&default_shell(), &[]).await?;
let prompt_pattern = Pattern::shell_prompt();
session
.expect_timeout(prompt_pattern.clone(), Duration::from_secs(5))
.await?;
println!(" Session started with auto-detected shell");
println!(" Shell: {}", default_shell());
println!("\n8. Environment-aware automation...");
if is_utf8_environment() {
println!(" UTF-8 environment detected - safe to use Unicode");
session.send_line("echo 'Unicode: ✓ 日本語'").await?;
} else {
println!(" Non-UTF-8 environment - using ASCII only");
session.send_line("echo 'ASCII only'").await?;
}
session
.expect_timeout(prompt_pattern.clone(), Duration::from_secs(2))
.await?;
println!("\n9. Cross-platform detection...");
#[cfg(unix)]
println!(" Platform: Unix/Linux/macOS");
#[cfg(windows)]
println!(" Platform: Windows");
let line_ending = if cfg!(windows) { "CRLF" } else { "LF" };
println!(" Expected line ending: {line_ending}");
println!("\n10. Prompt configuration...");
let _prompt_config = PromptConfig::default();
println!(" Prompt config created");
println!(" Can be used to customize prompt detection behavior");
println!("\n11. Zero-config session workflow...");
println!(" 1. Detect shell: {}", default_shell());
println!(" 2. Detect locale: {}", detect_locale());
println!(" 3. Detect UTF-8: {}", is_utf8_environment());
println!(" 4. Configure session automatically");
println!(" 5. Start automation with sensible defaults");
session.send_line("echo 'Zero-config success!'").await?;
let m = session.expect("Zero-config success!").await?;
println!("\n Result: {}", m.matched.trim());
session.send_line("exit").await?;
session.wait().await?;
println!("\nZero-configuration examples completed successfully!");
Ok(())
}