use crate::cli::read::ReadArgs;
use crate::context::AppContext;
use crate::error::AppError;
use crate::output;
use crate::services::reader::{Shelf, get, list};
pub fn run(_ctx: &AppContext, shelf: &Shelf, args: ReadArgs) -> Result<(), AppError> {
if args.list {
let names = list(shelf);
let width = names.iter().map(String::len).max().unwrap_or(0);
for name in names {
match crate::domain::docs_catalog::CATALOG.for_document(shelf.id, &name) {
Some(topic) => output::line(format!("{name:width$} {}", topic.summary)),
None => output::line(name),
}
}
return Ok(());
}
let Some(name) = args.name else {
return Err(AppError::Usage(
"a document name or --list is required".to_string(),
));
};
let Some(text) = get(shelf, &name) else {
return Err(AppError::Usage(format!("no such document: {name}")));
};
output::line(text.trim_end_matches('\n'));
Ok(())
}