use std::path::PathBuf;
use clap::{Parser, ValueEnum};
use clap_repl::reedline::{
DefaultPrompt, DefaultPromptSegment, FileBackedHistory, Reedline, Signal,
};
use clap_repl::ClapEditor;
#[derive(Debug, Parser)]
#[command(name = "")] enum SampleCommand {
Download {
path: PathBuf,
#[arg(long)]
check_sha: bool,
},
Upload,
Login {
#[arg(short, long)]
username: Option<String>,
#[arg(short, long, value_enum, default_value_t = Mode::Secure)]
mode: Mode,
},
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Mode {
Secure,
Insecure,
}
#[tokio::main]
async fn main() {
let prompt = DefaultPrompt {
left_prompt: DefaultPromptSegment::Basic("simple-example".to_owned()),
..DefaultPrompt::default()
};
let rl = ClapEditor::<SampleCommand>::builder()
.with_prompt(Box::new(prompt))
.with_editor_hook(|reed| {
reed.with_history(Box::new(
FileBackedHistory::with_file(10000, "/tmp/clap-repl-simple-example-history".into())
.unwrap(),
))
})
.build();
rl.repl_async(async |command| {
match command {
SampleCommand::Download { path, check_sha } => {
download().await;
println!("Downloaded {path:?} with checking = {check_sha}");
}
SampleCommand::Upload => {
println!("Uploaded");
}
SampleCommand::Login { username, mode } => {
let mut rl = Reedline::create();
let username = username
.unwrap_or_else(|| read_line_with_reedline(&mut rl, "What is your username? "));
let password = read_line_with_reedline(&mut rl, "What is your password? ");
println!("Logged in with {username} and {password} in mode {mode:?}");
}
}
}).await;
}
async fn download() {
println!("Downloading...");
}
fn read_line_with_reedline(rl: &mut Reedline, prompt: &str) -> String {
let Signal::Success(x) = rl
.read_line(&DefaultPrompt::new(
DefaultPromptSegment::Basic(prompt.to_owned()),
DefaultPromptSegment::Empty,
))
.unwrap()
else {
panic!();
};
x
}