tests and docs
This commit is contained in:
parent
f7762c10da
commit
78583d4f06
|
|
@ -28,7 +28,10 @@ use colored::Color;
|
||||||
author = "r0r-5chach",
|
author = "r0r-5chach",
|
||||||
version = "1.0",
|
version = "1.0",
|
||||||
about = "A program that generates ASCII art terminal headers.
|
about = "A program that generates ASCII art terminal headers.
|
||||||
color can cause unexpected behaviour when used with terminal themes.",
|
color can cause unexpected behaviour when used with terminal themes.
|
||||||
|
|
||||||
|
Available colors: black, red, green, yellow, blue, magenta, cyan, white.
|
||||||
|
All colors also have a bright variant that can be accessed by using the 'bright' prefix (e.g. brightwhite)",
|
||||||
long_about = None)]
|
long_about = None)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// The path to the ASCII text files directory
|
/// The path to the ASCII text files directory
|
||||||
|
|
@ -53,7 +56,7 @@ pub struct Args {
|
||||||
/// - `title_file`: The contents of the randomly selected ASCII text file.
|
/// - `title_file`: The contents of the randomly selected ASCII text file.
|
||||||
/// - `art_file`: The contents of the randomly selected ASCII image file.
|
/// - `art_file`: The contents of the randomly selected ASCII image file.
|
||||||
/// - `banner_file`: The contents of the randomly selected ASCII banner file.
|
/// - `banner_file`: The contents of the randomly selected ASCII banner file.
|
||||||
///
|
/// - `color`: The color to output the ASCII to terminal in.
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub title_file: String,
|
pub title_file: String,
|
||||||
pub art_file: String,
|
pub art_file: String,
|
||||||
|
|
@ -61,8 +64,41 @@ pub struct Config {
|
||||||
pub color: Option<Color>,
|
pub color: Option<Color>,
|
||||||
}
|
}
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Returns a new configuration parsed from the [Args] parsed from the command line.
|
/// Returns a new configuration generated from the [Args] parsed from the command line.
|
||||||
pub fn new(args: Args) -> Self { //TODO: Add examples using tempdir to create a dir
|
///
|
||||||
|
/// # Examples
|
||||||
|
/// ```
|
||||||
|
/// //TODO: This Example returns a Permission Error
|
||||||
|
/// use clap::Parser;
|
||||||
|
/// use std::{fs::File, io::Write};
|
||||||
|
/// use tempfile::{NamedTempFile, TempDir};
|
||||||
|
/// use term_header::config;
|
||||||
|
///
|
||||||
|
/// let titles = TempDir::new().unwrap();
|
||||||
|
/// File::create(titles.path().join("/1.txt")).unwrap()
|
||||||
|
/// .write_all(b"Title Test");
|
||||||
|
///
|
||||||
|
/// let art = TempDir::new().unwrap();
|
||||||
|
/// File::create(art.path().join("/1.txt")).unwrap()
|
||||||
|
/// .write_all(b"Art Test");
|
||||||
|
///
|
||||||
|
/// let mut banner = NamedTempFile::new().unwrap();
|
||||||
|
/// banner.write_all(b"Banner Test");
|
||||||
|
///
|
||||||
|
/// let args = config::Args {
|
||||||
|
/// titles_path: titles.path().to_string_lossy().to_string(),
|
||||||
|
/// art_path: art.path().to_string_lossy().to_string(),
|
||||||
|
/// banner_path: banner.path().to_string_lossy().to_string(),
|
||||||
|
/// color: None,
|
||||||
|
/// };
|
||||||
|
/// let config = config::Config::new(args);
|
||||||
|
///
|
||||||
|
/// assert_eq!(config.title_file, titles.path().join("1.txt").to_string_lossy());
|
||||||
|
/// assert_eq!(config.art_file, art.path().join("1.txt").to_string_lossy());
|
||||||
|
/// assert_eq!(config.banner_file, banner.path().to_string_lossy());
|
||||||
|
/// assert_eq!(config.color, None);
|
||||||
|
/// ```
|
||||||
|
pub fn new(args: Args) -> Self {
|
||||||
let title_file = rand_file_from_path(args.titles_path);
|
let title_file = rand_file_from_path(args.titles_path);
|
||||||
let art_file = rand_file_from_path(args.art_path);
|
let art_file = rand_file_from_path(args.art_path);
|
||||||
let banner_file = args.banner_path;
|
let banner_file = args.banner_path;
|
||||||
|
|
@ -76,18 +112,23 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Returns the [usize] (number) of the files contained in the directory at [str] (dir).
|
||||||
|
/// If path does not exist, program exits.
|
||||||
fn get_file_count(dir: &str) -> usize {
|
fn get_file_count(dir: &str) -> usize {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
let files = match fs::read_dir(&dir) {
|
let files = match fs::read_dir(&dir) {
|
||||||
Ok(files) => files,
|
Ok(files) => files,
|
||||||
Err(_) => path_not_found_error(&dir),
|
Err(_) => {
|
||||||
|
eprintln!("The path {} does not exist", dir);
|
||||||
|
|
||||||
|
std::process::exit(1);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
files.count()
|
files.count()
|
||||||
}
|
}
|
||||||
|
/// Returns the [String] (file path) of a randomly selected file in the supplied [String] (path).
|
||||||
fn rand_file_from_path(path: String) -> String {
|
fn rand_file_from_path(path: String) -> String {
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
|
@ -104,8 +145,3 @@ fn rand_file_from_path(path: String) -> String {
|
||||||
|
|
||||||
file.to_string()
|
file.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_not_found_error(path: &str) -> ! {
|
|
||||||
eprintln!("This path does not exist: {}", path);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ pub fn run() {
|
||||||
/// //Checks that the output took longer to complete than the length of the string in milliseconds
|
/// //Checks that the output took longer to complete than the length of the string in milliseconds
|
||||||
/// assert!(time >= Duration::from_millis(str.len() as u64));
|
/// assert!(time >= Duration::from_millis(str.len() as u64));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn type_writer_print(str: &str, color: Option<Color>) { //TODO: Add an example for with a color
|
pub fn type_writer_print(str: &str, color: Option<Color>) {
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use std::{io, thread, time::Duration};
|
use std::{io, thread, time::Duration};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
@ -67,6 +67,7 @@ pub fn type_writer_print(str: &str, color: Option<Color>) { //TODO: Add an examp
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/// Returns a [str] containing the contents of the file at [str] (path).
|
/// Returns a [str] containing the contents of the file at [str] (path).
|
||||||
|
/// If path does not exist, program exits.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
|
|
@ -88,7 +89,7 @@ pub fn parse_file(path: &str) -> String {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Failed to read path: {path}");
|
eprintln!("Failed to read path: {path}");
|
||||||
|
|
||||||
"".to_string()
|
std::process::exit(1);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue