From 8f4cd91c039a4e3df4a3a7da7e84ba9a9fd406bf Mon Sep 17 00:00:00 2001 From: timeshifter Date: Wed, 11 Jan 2023 12:45:31 +0100 Subject: [PATCH] add showing group content --- src/core.rs | 9 ++++++--- src/group.rs | 14 +++++++++++++- src/section.rs | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/core.rs b/src/core.rs index 70dfe73..5350e1c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -29,7 +29,9 @@ impl Pacdef { self.edit_group_files(groups).context("editing group files") } Some((action::GROUPS, _)) => Ok(self.show_groups()), - Some((action::SHOW, _)) => Ok(self.show_packages_under_group()), + Some((action::SHOW, groups)) => { + self.show_group_content(groups).context("showing groups") + } Some((action::SYNC, _)) => Ok(self.install_packages()), Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()), Some((action::VERSION, _)) => Ok(self.show_version()), @@ -169,12 +171,13 @@ impl Pacdef { } } - fn show_packages_under_group(&self) { - let groups = self.args.get_many::("group").unwrap(); + fn show_group_content(&self, groups: &ArgMatches) -> Result<()> { + let groups = groups.get_many::("group").unwrap(); for arg_group in groups { let group = self.groups.iter().find(|g| g.name == *arg_group).unwrap(); println!("{group}"); } + Ok(()) } } diff --git a/src/group.rs b/src/group.rs index ad97dff..668f099 100644 --- a/src/group.rs +++ b/src/group.rs @@ -1,7 +1,7 @@ -use std::collections::HashSet; use std::fs::read_to_string; use std::hash::Hash; use std::path::Path; +use std::{collections::HashSet, fmt::Display}; use anyhow::{Context, Result}; @@ -87,3 +87,15 @@ impl Group { Ok(Self { name, sections }) } } + +impl Display for Group { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut sections: Vec<_> = self.sections.iter().collect(); + sections.sort_unstable(); + + for section in sections { + section.fmt(f)?; + } + Ok(()) + } +} diff --git a/src/section.rs b/src/section.rs index f3d6613..b7ac8fa 100644 --- a/src/section.rs +++ b/src/section.rs @@ -1,4 +1,9 @@ -use std::{collections::HashSet, hash::Hash, iter::Peekable}; +use std::{ + collections::HashSet, + fmt::{Display, Write}, + hash::Hash, + iter::Peekable, +}; use anyhow::{Context, Result}; @@ -48,3 +53,35 @@ impl PartialEq for Section { self.name == other.name } } + +impl PartialOrd for Section { + fn partial_cmp(&self, other: &Self) -> Option { + self.name.partial_cmp(&other.name) + } +} + +impl Ord for Section { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + todo!() + } +} + +impl Display for Section { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("[{}]\n", &self.name))?; + + let mut packages: Vec<_> = self.packages.iter().collect(); + packages.sort_unstable(); + + let mut iter = packages.iter().peekable(); + + while let Some(package) = iter.next() { + package.fmt(f)?; + if iter.peek().is_some() { + f.write_char('\n')?; + } + } + + Ok(()) + } +}