packard/src/main.rs

82 lines
2.2 KiB
Rust
Raw Normal View History

2025-01-14 12:32:48 +00:00
use indicatif::ProgressStyle;
2025-01-13 17:34:24 +00:00
use std::error::Error;
use terminal_link::Link;
use tokio;
2025-01-14 12:32:48 +00:00
mod config;
mod data;
2025-01-13 17:34:24 +00:00
fn trim_chars(input: &str) -> String {
let trimmed: String = input.chars().take(256).collect();
if trimmed.len() < input.len() {
format!("{}...", trimmed)
} else {
trimmed
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
2025-01-14 12:32:48 +00:00
let config = config::validate_config();
let args = config::parse_cli();
2025-01-13 17:34:24 +00:00
if args.verbose {
println!("{:?}", args);
println!("Selected list: {:?}", config.selected_list);
println!("Items: {:?}", config.lists);
}
let count: u8 = if args.count.is_some() {
args.count.expect("Count flag wrong?")
} else if config.count.is_some() {
config.count.expect("Unable to use count value from config")
} else {
8
};
let skip_amount: u8 = if args.skip_amount.is_some() {
args.skip_amount.expect("Skip amount flag wrong?")
} else if config.skip_amount.is_some() {
config
.skip_amount
.expect("Unable to use skip amount value from config")
} else {
0
};
let list: String = if args.selected_list.is_some() {
args.selected_list
.expect("Error getting selected list from flag")
} else if config.selected_list.is_some() {
config
.selected_list
.expect("Need to specify a selected list")
} else {
panic!("Need to set selected list")
};
if let Some(values) = config.lists.get(&list) {
let pb = indicatif::ProgressBar::new(12);
pb.set_style(
ProgressStyle::with_template("[{elapsed}] {bar:40.green/black} {msg}").unwrap(),
);
2025-01-14 12:32:48 +00:00
let all_items = data::run_tasks(values.to_vec(), count, skip_amount, &pb).await;
2025-01-13 17:34:24 +00:00
pb.finish_and_clear();
for item in all_items {
println!(
2025-01-14 00:28:35 +00:00
"\x1b[1m>\x1b[0m \x1b[1;32m{}\x1b[0m\n\x1b[3m\x1b[2m{}\x1b[0m\n\x1b[2m{}\x1b[0m\n",
2025-01-13 17:34:24 +00:00
Link::new(&item.title, &item.link),
2025-01-14 00:28:35 +00:00
trim_chars(&item.description),
item.pub_date.to_string()
2025-01-13 17:34:24 +00:00
);
}
} else {
panic!("Have you specified your site lists and chosen one?");
}
Ok(())
}