fix formatting for sync, clean, unmanaged

This commit is contained in:
Dr. Matthias Ratajczak
2023-02-14 15:51:35 +01:00
parent 230b28ca9c
commit b3176662a0
2 changed files with 35 additions and 17 deletions
@@ -1,4 +1,4 @@
use std::process::ExitStatus; use std::{fmt::Write, process::ExitStatus};
use anyhow::{bail, ensure, Context, Result}; use anyhow::{bail, ensure, Context, Result};
@@ -65,22 +65,36 @@ impl ToDoPerBackend {
Ok(()) Ok(())
} }
pub(crate) fn show(&self, indentend: bool) { pub(crate) fn show(&self) -> Result<()> {
let mut parts = vec![];
for (backend, packages) in self.iter() { for (backend, packages) in self.iter() {
if packages.is_empty() { if packages.is_empty() {
continue; continue;
} }
if indentend { let mut segment = String::new();
print!(" ");
} segment.write_str(&format!("[{}]", backend.get_section()))?;
println!("[{}]", backend.get_section());
for package in packages { for package in packages {
if indentend { segment.write_str(&format!("\n{package}"))?;
print!(" "); }
}
println!(" {package}"); parts.push(segment);
}
let mut output = String::new();
let mut iter = parts.iter().peekable();
while let Some(part) = iter.next() {
output.write_str(part)?;
if iter.peek().is_some() {
output.write_str("\n\n")?;
} }
} }
println!("{output}");
Ok(())
} }
} }
+11 -7
View File
@@ -65,7 +65,7 @@ impl Pacdef {
search::search_packages(args, &self.groups).context("searching packages") search::search_packages(args, &self.groups).context("searching packages")
} }
Some((SYNC, _)) => self.install_packages(), Some((SYNC, _)) => self.install_packages(),
Some((UNMANAGED, _)) => Ok(self.show_unmanaged_packages()), Some((UNMANAGED, _)) => self.show_unmanaged_packages(),
Some((VERSION, _)) => Ok(self.show_version()), Some((VERSION, _)) => Ok(self.show_version()),
Some((_, _)) => panic!(), Some((_, _)) => panic!(),
None => { None => {
@@ -111,9 +111,10 @@ impl Pacdef {
return Ok(()); return Ok(());
} }
println!("Would install the following packages:"); println!("Would install the following packages:\n");
to_install.show(true); to_install.show().context("printing things to do")?;
println!();
if !get_user_confirmation()? { if !get_user_confirmation()? {
return Ok(()); return Ok(());
}; };
@@ -156,10 +157,12 @@ impl Pacdef {
println!("{}", get_version_string()); println!("{}", get_version_string());
} }
fn show_unmanaged_packages(mut self) { fn show_unmanaged_packages(mut self) -> Result<()> {
let unmanaged_per_backend = &self.get_unmanaged_packages(); let unmanaged_per_backend = &self.get_unmanaged_packages();
unmanaged_per_backend.show(false); unmanaged_per_backend
.show()
.context("printing things to do")
} }
fn get_unmanaged_packages(&mut self) -> ToDoPerBackend { fn get_unmanaged_packages(&mut self) -> ToDoPerBackend {
@@ -194,9 +197,10 @@ impl Pacdef {
return Ok(()); return Ok(());
} }
println!("Would remove the following packages"); println!("Would remove the following packages:\n");
to_remove.show(true); to_remove.show().context("printing things to do")?;
println!();
if !get_user_confirmation()? { if !get_user_confirmation()? {
return Ok(()); return Ok(());
}; };