parfit 0.1.0

Paragraph fit — a codebase-aware comment reflow tool that wraps prose with optimal-fit line breaking and leaves directives alone. Inspired by par.
Documentation
use clap::Parser;
use std::io::{self, Read, Write};

use parfit::{reflow, Options};

/// Paragraph fit — a codebase-aware comment reflow tool.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
    /// Target width in columns.
    #[arg(short = 'w', long, default_value_t = 68)]
    width: usize,

    /// Add a custom regex whose matching lines pass through verbatim.
    /// Repeatable.
    #[arg(short = 's', long = "skip", value_name = "REGEX")]
    skips: Vec<String>,

    /// Turn off the built-in directive skip list (Go, Rust, shell,
    /// TypeScript, Python directives).
    #[arg(long)]
    no_default_skips: bool,

    /// Force a specific comment prefix instead of auto-detecting.
    #[arg(short = 'p', long, value_name = "STRING")]
    prefix: Option<String>,
}

fn main() -> io::Result<()> {
    let cli = Cli::parse();

    let mut input = String::new();
    io::stdin().read_to_string(&mut input)?;

    let mut opts = Options::new(cli.width);
    if cli.no_default_skips {
        opts = opts.with_default_skips(false);
    }
    for pattern in &cli.skips {
        opts = opts
            .with_skip(pattern)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?;
    }
    if let Some(prefix) = cli.prefix {
        opts = opts.with_forced_prefix(prefix);
    }

    let output = reflow(&input, &opts);
    io::stdout().write_all(output.as_bytes())?;
    Ok(())
}