term-header/src/config.rs

60 lines
1.3 KiB
Rust
Raw Normal View History

2024-01-05 18:29:50 +00:00
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long, default_value = "titles/")]
titles_path: String,
#[arg(short, long, default_value = "art/")]
art_path: String,
#[arg(short, long, default_value = "banner.txt")]
banner_path: String,
}
pub struct Config {
pub title_file: String,
pub art_file: String,
pub banner_file: String,
}
impl Config {
pub fn new(args: Args) -> Self {
let title_file = rand_file_from_path(args.titles_path);
let art_file = rand_file_from_path(args.art_path);
let banner_file = args.banner_path;
Self {
title_file,
art_file,
banner_file,
}
}
}
fn get_file_count(dir: &str) -> usize {
use std::fs;
let files = match fs::read_dir(&dir) {
Ok(files) => files,
Err(_) => path_not_found_error(&dir),
};
files.count()
}
fn rand_file_from_path(path: String) -> String {
use rand::Rng;
let mut rng = rand::thread_rng();
let mut file = path.clone();
file.push_str(&rng.gen_range(1..get_file_count(&path)).to_string());
file.push_str(".txt");
file.to_string()
}
fn path_not_found_error(path: &str) -> ! {
eprintln!("This path does not exist: {}", path);
std::process::exit(1);
}