From 2a3fc57014d5580c0126ed9f47488ea70e380618 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 14 Feb 2023 15:51:35 +0100 Subject: [PATCH] fix formatting for sync, clean, unmanaged --- .../src/backend/todo_per_backend.rs | 34 +++++++++++++------ crates/pacdef_core/src/core.rs | 18 ++++++---- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/crates/pacdef_core/src/backend/todo_per_backend.rs b/crates/pacdef_core/src/backend/todo_per_backend.rs index bae2108..592b5e0 100644 --- a/crates/pacdef_core/src/backend/todo_per_backend.rs +++ b/crates/pacdef_core/src/backend/todo_per_backend.rs @@ -1,4 +1,4 @@ -use std::process::ExitStatus; +use std::{fmt::Write, process::ExitStatus}; use anyhow::{bail, ensure, Context, Result}; @@ -65,22 +65,36 @@ impl ToDoPerBackend { Ok(()) } - pub(crate) fn show(&self, indentend: bool) { + pub(crate) fn show(&self) -> Result<()> { + let mut parts = vec![]; + for (backend, packages) in self.iter() { if packages.is_empty() { continue; } - if indentend { - print!(" "); - } - println!("[{}]", backend.get_section()); + let mut segment = String::new(); + + segment.write_str(&format!("[{}]", backend.get_section()))?; for package in packages { - if indentend { - print!(" "); - } - println!(" {package}"); + segment.write_str(&format!("\n{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(()) } } diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index c8b7a7a..73b4285 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -65,7 +65,7 @@ impl Pacdef { search::search_packages(args, &self.groups).context("searching 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((_, _)) => panic!(), None => { @@ -111,9 +111,10 @@ impl Pacdef { return Ok(()); } - println!("Would install the following packages:"); - to_install.show(true); + println!("Would install the following packages:\n"); + to_install.show().context("printing things to do")?; + println!(); if !get_user_confirmation()? { return Ok(()); }; @@ -156,10 +157,12 @@ impl Pacdef { 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(); - unmanaged_per_backend.show(false); + unmanaged_per_backend + .show() + .context("printing things to do") } fn get_unmanaged_packages(&mut self) -> ToDoPerBackend { @@ -194,9 +197,10 @@ impl Pacdef { return Ok(()); } - println!("Would remove the following packages"); - to_remove.show(true); + println!("Would remove the following packages:\n"); + to_remove.show().context("printing things to do")?; + println!(); if !get_user_confirmation()? { return Ok(()); };