all but main function out of main

This commit is contained in:
Troy 2025-01-14 15:00:12 +00:00
parent 22b9cf046c
commit 14c99e6380
Signed by: troy
GPG key ID: DFC06C02ED3B4711
3 changed files with 53 additions and 51 deletions

View file

@ -31,6 +31,47 @@ pub fn parse_cli() -> Cli {
args
}
pub fn collate_values(args: Cli, config: &Config) -> (u8, u8, String) {
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
.clone()
.expect("Error getting selected list from flag")
} else if config.selected_list.is_some() {
config
.selected_list
.clone()
.expect("Need to specify a selected list")
} else {
panic!("Need to set selected list")
};
(count, skip_amount, list)
}
pub fn validate_config() -> Config {
let xdg_dirs = BaseDirectories::new().expect("Failed to get XDG directories");
let config_path = xdg_dirs

View file

@ -1,67 +1,19 @@
use indicatif::ProgressStyle;
use std::error::Error;
use terminal_link::Link;
use tokio;
mod config;
mod data;
fn trim_chars(input: &str) -> String {
let trimmed: String = input.chars().take(256).collect();
if trimmed.len() < input.len() {
format!("{}...", trimmed)
} else {
trimmed
}
}
mod utils;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let config = config::validate_config();
let args = config::parse_cli();
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")
};
let (count, skip_amount, list) = config::collate_values(args, &config);
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(),
);
let all_items = data::run_tasks(values.to_vec(), count, skip_amount, &pb).await;
pb.finish_and_clear();
@ -69,7 +21,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!(
"\x1b[1m>\x1b[0m \x1b[1;32m{}\x1b[0m\n\x1b[3m\x1b[2m{}\x1b[0m\n\x1b[2m{}\x1b[0m\n",
Link::new(&item.title, &item.link),
trim_chars(&item.description),
utils::trim_chars(&item.description),
item.pub_date.to_string()
);
}

9
src/utils.rs Normal file
View file

@ -0,0 +1,9 @@
pub fn trim_chars(input: &str) -> String {
let trimmed: String = input.chars().take(256).collect();
if trimmed.len() < input.len() {
format!("{}...", trimmed)
} else {
trimmed
}
}