move command group printing logic into function

this makes the print function a bit cleaner.
This commit is contained in:
Fries 2023-07-01 20:39:56 -07:00
parent 7f36560e25
commit e93b201d77
1 changed files with 28 additions and 18 deletions

View File

@ -5,33 +5,43 @@ use shared::{errors::Error, names::Site};
use crate::arguments::PrintGroup;
pub(crate) fn print(path: &Path, group: &PrintGroup, seperator: &Option<char>) -> Result<(), Error> {
fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) {
let mut string = String::new();
let delimiter = seperator.unwrap_or(',');
if group.url {
string += &site.url;
}
if group.name {
if !string.is_empty() {
string += &format!("{}{}", delimiter, site.name.unwrap_or("None".to_string()))
} else {
string += &site.name.unwrap_or("None".to_string());
}
}
log::info!("{}", string);
}
pub(crate) fn print(
path: &Path,
group: &PrintGroup,
seperator: &Option<char>,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let names = names::load_names(names_file)?;
Ok(for site in names {
for site in names {
if !group.url && !group.name {
log::info!("{:?}", site);
continue;
}
let mut string = String::new();
let delimiter = seperator.unwrap_or(',');
group_printing(seperator, site, group);
}
if group.url {
string += &site.url;
}
if group.name {
if !string.is_empty() {
string += &format!("{}{}", delimiter, site.name.unwrap_or("None".to_string()))
} else {
string += &site.name.unwrap_or("None".to_string());
}
}
log::info!("{}", string);
})
Ok(())
}
pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<(), Error> {