diff --git a/src/main.rs b/src/main.rs index 5fdcd08..afaa315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,42 +13,47 @@ struct Flatpak { struct Asset; fn main() -> io::Result<()> { + loop { + clear_terminal(); - clear_terminal(); + let binding = detect_distro(); + let distro: &str = binding.as_str(); - let binding = detect_distro(); - let distro: &str = binding.as_str(); + let file_content = match distro { + "arch" => Asset::get("prompts/arch.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, + "Debian" => Asset::get("prompts/debian.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, + "fedora" => Asset::get("prompts/fedora.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, + _ => return Err(io::Error::new(io::ErrorKind::NotFound, "Unsupported distro")), + }; - let file_content = match distro { - "arch" => Asset::get("prompts/arch.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, - "Debian" => Asset::get("prompts/debian.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, - "fedora" => Asset::get("prompts/fedora.txt").ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "File not found"))?, - _ => return Err(io::Error::new(io::ErrorKind::NotFound, "Unsupported distro")), - }; + let buffer = std::str::from_utf8(&file_content.data) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - let buffer = std::str::from_utf8(&file_content.data) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + let first_choice: Vec = buffer.lines().map(|line| line.to_string()).collect(); - let first_choice: Vec = buffer.lines().map(|line| line.to_string()).collect(); + for (index, element) in first_choice.iter().enumerate() { + eprint!("{}. {}\n", index + 1, element); + } + eprint!("\nEnter 0 to exit\n"); - for(index, element) in first_choice.iter().enumerate() { - eprint!("{}. {}\n", index+1, element) + let input: i32 = handle_int_input(); + + if input == 0 { + break; + } + + match distro { + "arch" => arch_choices(input)?, // Pass the input value here + "Debian" => debian_choices(input)?, // Pass the input value here + "fedora" => fedora_choices(input)?, // Pass the input value here + _ => return Err(io::Error::new(io::ErrorKind::NotFound, "Unsupported distro")), + }; } - eprint!("\nEnter 0 to exit\n"); - - let _ = match distro { - "arch" => arch_choices(), - "Debian" => debian_choices(), - "fedora" => fedora_choices(), - _ => return Err(io::Error::new(io::ErrorKind::NotFound, "Unsupported distro")), - }; Ok(()) } -fn arch_choices() -> io::Result<()> { - - let input: i32 = handle_int_input(); +fn arch_choices(input: i32) -> io::Result<()> { let _ = match input { 1 => install_programs(), @@ -62,9 +67,7 @@ fn arch_choices() -> io::Result<()> { Ok(()) } -fn debian_choices() -> io::Result<()> { - - let input: i32 = handle_int_input(); +fn debian_choices(input: i32) -> io::Result<()> { let _ = match input { 1 => install_programs(), @@ -78,9 +81,7 @@ fn debian_choices() -> io::Result<()> { Ok(()) } -fn fedora_choices() -> io::Result<()> { - - let input: i32 = handle_int_input(); +fn fedora_choices(input: i32) -> io::Result<()> { let _ = match input { 1 => install_programs(), @@ -113,7 +114,7 @@ fn install_programs() -> Result<(), Box> { let mut chosen: Vec = vec![]; for &n in input.iter() { if input.contains(&0) { - let _ = main(); + return Ok(()); } else { chosen.push(choice[(n-1) as usize].clone()); } @@ -135,8 +136,6 @@ fn install_programs() -> Result<(), Box> { } }; - let _ = main(); - Ok(()) } @@ -163,7 +162,7 @@ fn install_flatpaks() -> Result<(), Box> { eprint!("\nInstall Flatpak first? Y/N (1/0)\n"); let input = handle_int_input(); if input == 0 { - let _ = main(); + return Ok(()); } else { match distro { "arch" => { @@ -190,7 +189,7 @@ fn install_flatpaks() -> Result<(), Box> { let input = handle_vec_input(); for &n in input.iter() { if input.contains(&0) { - let _ = main(); + return Ok(()); } else if let Some((key, _)) = hashmap.iter().nth((n-1) as usize) { let status = Command::new("flatpak").arg("install").arg("-y").arg(key).status(); match status { @@ -207,7 +206,6 @@ fn install_flatpaks() -> Result<(), Box> { eprint!("Error reading file: {}", e); } } - let _ = main(); Ok(()) } @@ -237,7 +235,6 @@ fn update_system() -> Result<(), Box> { _ => eprint!("Unsupported distro") }; - let _ = main(); Ok(()) } @@ -259,7 +256,7 @@ fn aur() -> Result<(), Box> { std::env::set_current_dir("yay")?; Command::new("makepkg").arg("-si").status()?; } else { - let _ = main(); + return Ok(()); } } @@ -272,7 +269,7 @@ fn aur() -> Result<(), Box> { let mut chosen: Vec = vec![]; for &n in input.iter() { if input.contains(&0) { - let _ = main(); + return Ok(()); } else { chosen.push(choice[(n-1) as usize].clone()); } @@ -281,8 +278,6 @@ fn aur() -> Result<(), Box> { Command::new("yay").arg("-S").arg(i).status()?; } - let _ = main(); - Ok(()) } @@ -329,7 +324,7 @@ fn third_party() -> Result<(), Box> { eprintln!("Script not found: {}", script_name); } } - let _ = main(); + Ok(()) } @@ -347,7 +342,7 @@ fn drivers() -> Result<(), Box> { let input: i32 = handle_int_input(); if input == 0 { - let _ = main(); + return Ok(()); } match input { @@ -482,8 +477,6 @@ fn rpm() { eprintln!("Failed to get Fedora version with status: {}", version.status); eprintln!("Error output:\n{}", stderr); } - - let _ = main(); } fn handle_vec_input() -> Vec {