all but main function out of main
This commit is contained in:
parent
22b9cf046c
commit
14c99e6380
3 changed files with 53 additions and 51 deletions
|
@ -31,6 +31,47 @@ pub fn parse_cli() -> Cli {
|
||||||
args
|
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 {
|
pub fn validate_config() -> Config {
|
||||||
let xdg_dirs = BaseDirectories::new().expect("Failed to get XDG directories");
|
let xdg_dirs = BaseDirectories::new().expect("Failed to get XDG directories");
|
||||||
let config_path = xdg_dirs
|
let config_path = xdg_dirs
|
||||||
|
|
54
src/main.rs
54
src/main.rs
|
@ -1,67 +1,19 @@
|
||||||
use indicatif::ProgressStyle;
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use terminal_link::Link;
|
use terminal_link::Link;
|
||||||
use tokio;
|
use tokio;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod data;
|
mod data;
|
||||||
|
mod utils;
|
||||||
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let config = config::validate_config();
|
let config = config::validate_config();
|
||||||
let args = config::parse_cli();
|
let args = config::parse_cli();
|
||||||
|
let (count, skip_amount, list) = config::collate_values(args, &config);
|
||||||
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) {
|
if let Some(values) = config.lists.get(&list) {
|
||||||
let pb = indicatif::ProgressBar::new(12);
|
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;
|
let all_items = data::run_tasks(values.to_vec(), count, skip_amount, &pb).await;
|
||||||
pb.finish_and_clear();
|
pb.finish_and_clear();
|
||||||
|
|
||||||
|
@ -69,7 +21,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
println!(
|
println!(
|
||||||
"\x1b[1m>\x1b[0m \x1b[1;32m{}\x1b[0m\n\x1b[3m\x1b[2m{}\x1b[0m\n\x1b[2m{}\x1b[0m\n",
|
"\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),
|
Link::new(&item.title, &item.link),
|
||||||
trim_chars(&item.description),
|
utils::trim_chars(&item.description),
|
||||||
item.pub_date.to_string()
|
item.pub_date.to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
9
src/utils.rs
Normal file
9
src/utils.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue