add showing group content

This commit is contained in:
timeshifter
2023-01-11 12:45:31 +01:00
parent 3f01ae5ded
commit 8f4cd91c03
3 changed files with 57 additions and 5 deletions
+6 -3
View File
@@ -29,7 +29,9 @@ impl Pacdef {
self.edit_group_files(groups).context("editing group files") self.edit_group_files(groups).context("editing group files")
} }
Some((action::GROUPS, _)) => Ok(self.show_groups()), 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::SYNC, _)) => Ok(self.install_packages()),
Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()), Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
Some((action::VERSION, _)) => Ok(self.show_version()), Some((action::VERSION, _)) => Ok(self.show_version()),
@@ -169,12 +171,13 @@ impl Pacdef {
} }
} }
fn show_packages_under_group(&self) { fn show_group_content(&self, groups: &ArgMatches) -> Result<()> {
let groups = self.args.get_many::<String>("group").unwrap(); let groups = groups.get_many::<String>("group").unwrap();
for arg_group in groups { for arg_group in groups {
let group = self.groups.iter().find(|g| g.name == *arg_group).unwrap(); let group = self.groups.iter().find(|g| g.name == *arg_group).unwrap();
println!("{group}"); println!("{group}");
} }
Ok(())
} }
} }
+13 -1
View File
@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::hash::Hash; use std::hash::Hash;
use std::path::Path; use std::path::Path;
use std::{collections::HashSet, fmt::Display};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@@ -87,3 +87,15 @@ impl Group {
Ok(Self { name, sections }) 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(())
}
}
+38 -1
View File
@@ -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}; use anyhow::{Context, Result};
@@ -48,3 +53,35 @@ impl PartialEq for Section {
self.name == other.name self.name == other.name
} }
} }
impl PartialOrd for Section {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
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(())
}
}