Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

4.7 KB
Raw Blame
// ©AngelaMos | 2026
// theme.rs

use lisdex_monitor::snapshot::{Phase, Severity};
use ratatui::style::{Color, Modifier, Style};

pub const INK: Color = Color::Rgb(232, 238, 252);
pub const SLATE: Color = Color::Rgb(118, 130, 164);
pub const DUSK: Color = Color::Rgb(70, 80, 112);
pub const EDGE: Color = Color::Rgb(52, 60, 96);
pub const PANEL: Color = Color::Rgb(15, 17, 30);

pub const NEON_CYAN: Color = Color::Rgb(20, 215, 235);
pub const NEON_MAGENTA: Color = Color::Rgb(255, 45, 175);
pub const NEON_VIOLET: Color = Color::Rgb(175, 85, 255);
pub const NEON_BLUE: Color = Color::Rgb(60, 150, 250);
pub const ACID_GREEN: Color = Color::Rgb(55, 250, 145);
pub const AMBER: Color = Color::Rgb(255, 190, 70);
pub const HOT_RED: Color = Color::Rgb(255, 62, 96);

pub const WORDMARK: [(char, Color); 6] = [
    ('L', NEON_MAGENTA),
    ('I', NEON_VIOLET),
    ('S', Color::Rgb(110, 110, 250)),
    ('D', NEON_BLUE),
    ('E', NEON_CYAN),
    ('X', ACID_GREEN),
];

pub const RAMP: [Color; 6] = [
    Color::Rgb(139, 92, 246),
    Color::Rgb(99, 102, 241),
    Color::Rgb(59, 130, 246),
    Color::Rgb(14, 165, 233),
    Color::Rgb(20, 220, 200),
    Color::Rgb(52, 255, 145),
];

#[must_use]
pub fn ramp_at(position: usize) -> Color {
    RAMP[position % RAMP.len()]
}

#[must_use]
pub fn panel() -> Style {
    Style::default().bg(PANEL)
}

#[must_use]
pub fn border() -> Style {
    Style::default().fg(EDGE)
}

#[must_use]
pub fn title(accent: Color) -> Style {
    Style::default().fg(accent).add_modifier(Modifier::BOLD)
}

#[must_use]
pub fn label() -> Style {
    Style::default().fg(SLATE)
}

#[must_use]
pub fn value(accent: Color) -> Style {
    Style::default().fg(accent).add_modifier(Modifier::BOLD)
}

#[must_use]
pub fn dim() -> Style {
    Style::default().fg(DUSK)
}

#[must_use]
pub fn severity_color(severity: Severity) -> Color {
    match severity {
        Severity::Exploitable => HOT_RED,
        Severity::ProbablyExploitable => AMBER,
        Severity::NotExploitable => NEON_CYAN,
        Severity::Undefined => NEON_VIOLET,
        Severity::Unclassified => SLATE,
    }
}

#[must_use]
pub fn severity_style(severity: Severity) -> Style {
    let style = Style::default().fg(severity_color(severity));
    if severity == Severity::Exploitable {
        return style.add_modifier(Modifier::BOLD);
    }
    style
}

#[must_use]
pub fn phase_color(phase: Phase) -> Color {
    match phase {
        Phase::Pending => SLATE,
        Phase::Building => AMBER,
        Phase::Fuzzing => ACID_GREEN,
        Phase::Completed => NEON_CYAN,
        Phase::Failed => HOT_RED,
    }
}

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

    #[test]
    fn the_wordmark_spells_the_tool() {
        let spelled: String = WORDMARK.iter().map(|(letter, _)| letter).collect();
        assert_eq!(spelled, "LISDEX");
    }

    #[test]
    fn every_wordmark_letter_gets_its_own_colour() {
        let mut colours: Vec<Color> = WORDMARK.iter().map(|(_, colour)| *colour).collect();
        colours.dedup();
        assert_eq!(colours.len(), WORDMARK.len());
    }

    #[test]
    fn the_ramp_wraps_rather_than_panicking_past_its_end() {
        assert_eq!(ramp_at(0), RAMP[0]);
        assert_eq!(ramp_at(RAMP.len()), RAMP[0]);
        assert_eq!(ramp_at(usize::MAX), RAMP[usize::MAX % RAMP.len()]);
    }

    #[test]
    fn no_two_severities_share_a_colour() {
        let mut colours: Vec<Color> = [
            Severity::Exploitable,
            Severity::ProbablyExploitable,
            Severity::NotExploitable,
            Severity::Undefined,
            Severity::Unclassified,
        ]
        .iter()
        .map(|severity| severity_color(*severity))
        .collect();
        colours.dedup();
        assert_eq!(
            colours.len(),
            5,
            "two severities sharing a colour would make an exploitable write look like a benign read on screen"
        );
    }

    #[test]
    fn the_worst_severity_is_the_only_one_rendered_bold() {
        assert!(
            severity_style(Severity::Exploitable)
                .add_modifier
                .contains(Modifier::BOLD)
        );
        for severity in [
            Severity::ProbablyExploitable,
            Severity::NotExploitable,
            Severity::Undefined,
            Severity::Unclassified,
        ] {
            assert!(
                !severity_style(severity)
                    .add_modifier
                    .contains(Modifier::BOLD)
            );
        }
    }

    #[test]
    fn a_failed_campaign_never_shows_the_colour_a_healthy_one_does() {
        assert_ne!(phase_color(Phase::Failed), phase_color(Phase::Fuzzing));
        assert_eq!(phase_color(Phase::Failed), HOT_RED);
        assert_eq!(phase_color(Phase::Fuzzing), ACID_GREEN);
    }
}