use chrono::Datelike;
use clap::Parser;
use compact_calendar_cli::models::{
CalendarOptions, ColorMode, MonthFilter, PastDateDisplay, WeekStart, WeekendDisplay,
};
use compact_calendar_cli::rendering::CalendarRenderer;
use std::path::PathBuf;
fn restore_sigpipe_default() {
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
}
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
#[arg(short, long)]
year: Option<i32>,
#[arg(short, long, default_value = "calendar.toml")]
config: PathBuf,
#[arg(short, long)]
sunday: bool,
#[arg(long)]
no_dim_weekends: bool,
#[arg(short, long)]
work: bool,
#[arg(long)]
no_strikethrough_past: bool,
#[arg(short = 'm', long)]
month: Option<String>,
#[arg(short = 'f', long)]
following_months: Option<u32>,
}
fn main() {
restore_sigpipe_default();
let args = Args::parse();
let year = args.year.unwrap_or_else(|| chrono::Local::now().year());
let config = compact_calendar_cli::load_config(&args.config);
let options = CalendarOptions {
week_start: WeekStart::from_sunday_flag(args.sunday),
weekend_display: WeekendDisplay::from_no_dim_flag(args.no_dim_weekends),
color_mode: ColorMode::from_work_flag(args.work),
past_date_display: PastDateDisplay::from_no_strikethrough_flag(args.no_strikethrough_past),
month_filter: MonthFilter::from_cli_args(args.month.as_deref(), args.following_months)
.unwrap_or_else(|e| {
eprintln!("Error: {}", e);
std::process::exit(1);
}),
};
let calendar = compact_calendar_cli::build_calendar(year, options, config);
let renderer = CalendarRenderer::new(&calendar);
renderer.render();
}