use inquire::Select;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};
fn main() {
let project_path = env::args().nth(1).map(PathBuf::from).unwrap_or_else(|| {
let mut path = dirs::home_dir().expect("Could not get home dir");
path.push("projects");
path
});
if !project_path.exists() {
eprintln!("Folder doesn't exist: {}", project_path.display());
std::process::exit(1);
}
let ide = env::args()
.nth(2)
.map(PathBuf::from)
.expect("Ide cli not provided");
let project_paths: Vec<PathBuf> = fs::read_dir(&project_path)
.unwrap()
.filter_map(|entry| {
entry.ok().and_then(|e| {
let path = e.path();
if path.is_dir() { Some(path) } else { None }
})
})
.collect();
let mut rust_projects: Vec<(PathBuf, String)> = project_paths
.into_iter()
.filter(|x| {
fs::read_dir(x)
.unwrap()
.map(|x| x.unwrap())
.any(|f| f.file_name() == "Cargo.toml")
})
.map(|x| {
(
x.clone(),
x.file_name().unwrap().to_string_lossy().to_string(),
)
})
.collect();
rust_projects.sort_by(|(_, name_x), (_, name_y)| name_x.cmp(name_y));
let for_selector = rust_projects
.iter()
.map(|(_, name)| name.to_string())
.collect();
let ans: String = Select::new("Select Rust Project", for_selector)
.with_page_size(20)
.prompt()
.unwrap();
let (path, _) = rust_projects
.iter()
.find(|(path, _)| path.ends_with(ans.clone()))
.unwrap();
let status = Command::new(ide).arg(path).status();
match status {
Ok(status) if status.success() => {}
Ok(status) => eprintln!("IDE exited with non-zero code: {status}"),
Err(err) => eprintln!("Failed to launch IDE: {err}"),
}
}