fundaia 0.5.0

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

/// The palette, matched to the web interface rather than invented.
///
/// A terminal has sixteen colours it can promise and a truecolour escape it
/// usually honours; these use the named ones, so the output stays legible in a
/// light terminal, over SSH, and inside a CI log that strips colour entirely.
///
/// The names are roles, not colours, for the same reason the CSS tokens are:
/// `state_style("active")` should keep working when green stops being the
/// answer.
pub fn accent() -> Style {
    Style::new().magenta()
}

pub fn muted() -> Style {
    Style::new().bright_black()
}

pub fn strong() -> Style {
    Style::new().bold()
}

pub fn success() -> Style {
    Style::new().green()
}

pub fn warning() -> Style {
    Style::new().yellow()
}

pub fn danger() -> Style {
    Style::new().red()
}

/// How a service state is coloured, and what it is called in Spanish.
pub fn state_style(state: &str) -> Style {
    match state {
        "active" => success(),
        "deploying" => accent(),
        "sleeping" => Style::new().cyan(),
        "degraded" => warning(),
        "crashed" | "failed" => danger(),
        _ => muted(),
    }
}

/// Borrowed from the argument, so an unknown state can be passed through
/// rather than replaced by a placeholder that hides what the server said.
pub fn state_word(state: &str) -> &str {
    match state {
        "draft" | "idle" => "sin desplegar",
        "deploying" => "desplegando",
        "active" => "en marcha",
        "sleeping" => "dormido",
        "degraded" => "con problemas",
        "crashed" => "ha caído",
        "failed" => "ha fallado",
        "removing" => "eliminando",
        other => other,
    }
}

pub fn deployment_status_style(status: &str) -> Style {
    match status {
        "active" => success(),
        "queued" | "building" | "deploying" => accent(),
        "failed" | "crashed" | "cancelled" => danger(),
        _ => muted(),
    }
}

/// A dot in the state's colour: the same vocabulary as the canvas.
pub fn state_dot(state: &str) -> String {
    format!("{}", "".style(state_style(state)))
}

/// A single ASCII fallback is not worth it — every terminal in use draws these.
pub const CHECK: &str = "";
pub const CROSS: &str = "";
pub const SKIP: &str = "";
pub const PENDING: &str = "";
pub const ARROW: &str = "";

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_should_translate_a_running_service() {
        assert_eq!(state_word("active"), "en marcha");
    }

    #[test]
    fn it_should_pass_an_unknown_state_through_unchanged() {
        assert_eq!(state_word("something-new"), "something-new");
    }
}