papr-pdf 0.1.0

A fast, keyboard-first CLI tool to compile Markdown, LaTeX, and plain text files into publication-grade PDFs.
//! # papr
//!
//! A fast, keyboard-first CLI tool to compile Markdown, LaTeX, and plain text files
//! into publication-grade PDFs.

mod cli;
mod engine;
mod theme;
mod wizard;

use clap::Parser;
use cli::Cli;
use colored::*;
use std::process;

fn main() {
    let args = Cli::parse();

    let (input_path, format) = match args.input {
        Some(path) => {
            if !path.exists() {
                eprintln!(
                    "{} File not found: {}",
                    "Error:".bright_red().bold(),
                    path.display()
                );
                process::exit(1);
            }
            let fmt = args.format.unwrap_or_else(|| engine::detect_format_from_path(&path));
            (path, fmt)
        }
        None => {
            // No argument supplied: launch interactive wizard!
            match wizard::run_wizard() {
                Ok(result) => (result.input_path, result.format),
                Err(err) => {
                    eprintln!("{} {}", "Cancelled:".bright_yellow().bold(), err);
                    process::exit(1);
                }
            }
        }
    };

    let output_path = engine::determine_output_path(&input_path, args.output);

    if let Err(err) = engine::compile(&input_path, &output_path, format, args.theme) {
        eprintln!("\n{} {:#}", "Error:".bright_red().bold(), err);
        process::exit(1);
    }

    if args.open {
        println!("{}", "Opening PDF in default viewer...".dimmed());
        let _ = open::that(&output_path);
    }
}