fundaia 0.55.0

Command line for the Fundaia deployment platform: projects, services, variables, deployments, logs and metrics
use owo_colors::OwoColorize;

use crate::api::models::CommitFile;
use crate::render::{sanitise, theme};

pub fn status_word(status: &str) -> &str {
    match status {
        "added" => "nuevo",
        "removed" => "borrado",
        "modified" => "modificado",
        "renamed" => "renombrado",
        "copied" => "copiado",
        "unchanged" => "sin cambios",
        other => other,
    }
}

pub fn proportion(additions: u32, deletions: u32) -> String {
    let width = 5u32;
    let total = additions + deletions;
    if total == 0 {
        return "─".repeat(width as usize);
    }

    let pluses = (((additions * width) + total / 2) / total)
        .max(u32::from(additions > 0))
        .min(width);
    let minuses = width - pluses;

    format!(
        "{}{}",
        "+".repeat(pluses as usize).style(theme::success()),
        "-".repeat(minuses as usize).style(theme::danger())
    )
}

pub fn print_file(file: &CommitFile) {
    println!();
    println!(
        "{} {}  {}",
        "▸".style(theme::muted()),
        heading(file).style(theme::strong()),
        format!(
            "{} {} · +{} −{}",
            status_word(&file.status),
            proportion(file.additions, file.deletions),
            file.additions,
            file.deletions
        )
        .style(theme::muted())
    );

    match &file.patch {
        None if file.additions == 0 && file.deletions == 0 => {
            println!("  {}", "fichero binario".style(theme::muted()))
        }
        None => println!(
            "  {}",
            "GitHub no envía el diff de un fichero tan grande".style(theme::muted())
        ),
        Some(patch) => print_patch(patch),
    }
}

fn heading(file: &CommitFile) -> String {
    match &file.previous_path {
        Some(previous) => format!("{previous} → {}", file.path),
        None => file.path.clone(),
    }
}

fn print_patch(patch: &str) {
    for line in sanitise::plain(patch).lines() {
        if line.starts_with("@@") {
            println!("  {}", line.style(theme::accent()));
        } else if line.starts_with("+++") || line.starts_with("---") {
            println!("  {}", line.style(theme::muted()));
        } else if line.starts_with('+') {
            println!("  {}", line.style(theme::success()));
        } else if line.starts_with('-') {
            println!("  {}", line.style(theme::danger()));
        } else if line.starts_with('\\') {
            println!("  {}", line.style(theme::muted()));
        } else {
            println!("  {line}");
        }
    }
}