diff options
| author | A Farzat <a@farzat.xyz> | 2026-02-08 11:22:27 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-02-08 11:22:27 +0300 |
| commit | 639ddf4b2e88bdc95a9e09eadea1be606327dea5 (patch) | |
| tree | a608d3811b7d1d0e0324930140fcb8ed0ec4563a /src/display.rs | |
| parent | 9d3bff94d9dfc332a6bb05efc923135cf22a35d2 (diff) | |
| download | safaribooks-rs-639ddf4b2e88bdc95a9e09eadea1be606327dea5.tar.gz safaribooks-rs-639ddf4b2e88bdc95a9e09eadea1be606327dea5.zip | |
Add display and config modules
Diffstat (limited to 'src/display.rs')
| -rw-r--r-- | src/display.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/display.rs b/src/display.rs new file mode 100644 index 0000000..6316f95 --- /dev/null +++ b/src/display.rs @@ -0,0 +1,62 @@ +use colored::*; +use std::{fs::File, path::PathBuf}; +use tracing::{error, info}; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +pub struct Display { + pub log_file: PathBuf, + pub output_dir: Option<PathBuf>, +} + +impl Display { + pub fn new(book_id: &str) -> Self { + let log_file = std::env::current_dir() + .unwrap() + .join(format!("info_{}.log", book_id)); + + let d = Self { + log_file, + output_dir: None, + }; + + let file = File::create(&d.log_file).expect("Cannot create log file"); + + tracing_subscriber::registry() + .with(EnvFilter::from_default_env()) + .with(fmt::layer().with_writer(std::io::stdout)) + .with(fmt::layer().with_writer(file).with_ansi(false)) + .init(); + + d.intro(); + info!("** Welcome to SafariBooks (Rust) **"); + d + } + + pub fn intro(&self) { + let banner = r#" + ____ __ _ ____ _ +/ ___| __ _ / _| __ _ _ __(_) _ \ _ _ ___| |_ +\___ \ / _` | |_ / _` | '__| | |_) | | | / __| __| + ___) | (_| | _| (_| | | | | _ <| |_| \__ \ |_ +|____/ \__,_|_| \__,_|_| |_|_| \_\\__,_|___/\__| +"#; + println!("{}", banner.yellow()); + println!("{}", "~".repeat(32)); + } + + pub fn info(&self, msg: &str) { + println!("{} {}", "[*]".yellow(), msg); + info!("{msg}"); + } + + pub fn error_and_exit(&self, msg: &str) -> ! { + eprintln!("{} {}", "[!]".on_red().white(), msg); + error!("{msg}"); + std::process::exit(1); + } + + pub fn set_output_dir(&mut self, dir: PathBuf) { + self.output_dir = Some(dir.clone()); + self.info(&format!("Output directory:\n {}", dir.display())); + } +} |
