fastmash 0.6.0

Fast command-line statistics on text data, compatible with GNU datamash
//! One terminal palette and destination policy for human-readable output.
#![forbid(unsafe_code)]

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum ColorMode {
    #[default]
    Auto,
    Always,
    Never,
}

#[derive(Clone, Copy)]
pub(super) enum Destination {
    Stdout,
    Stderr,
}

#[derive(Clone, Copy, Default)]
pub(super) struct Detection {
    pub stdout_terminal: bool,
    pub stderr_terminal: bool,
    pub suppressed: bool,
}

impl Detection {
    pub(super) fn from_env() -> Self {
        Self {
            stdout_terminal: nix::unistd::isatty(rustix::stdio::stdout()).unwrap_or(false),
            stderr_terminal: nix::unistd::isatty(rustix::stdio::stderr()).unwrap_or(false),
            suppressed: environment_suppresses(),
        }
    }

    pub(super) fn style(self, mode: ColorMode, destination: Destination) -> Style {
        let terminal = match destination {
            Destination::Stdout => self.stdout_terminal,
            Destination::Stderr => self.stderr_terminal,
        };
        Style(match mode {
            ColorMode::Auto => terminal && !self.suppressed,
            ColorMode::Always => true,
            ColorMode::Never => false,
        })
    }
}

fn environment_suppresses() -> bool {
    std::env::var_os("TERM").is_some_and(|term| term == "dumb")
        || std::env::var_os("NO_COLOR").is_some_and(|value| !value.is_empty())
}

#[derive(Clone, Copy, Debug)]
pub(super) enum Role {
    Heading,
    Syntax,
    Advisory,
    Failure,
    Value,
    Success,
    Muted,
}

#[derive(Clone, Copy, Debug, Default)]
pub(super) struct Style(bool);

impl Style {
    pub(super) fn enabled(self) -> bool {
        self.0
    }

    pub(super) fn start(self, role: Role) -> &'static [u8] {
        if !self.0 {
            return b"";
        }
        match role {
            Role::Heading => b"\x1b[1;38;2;189;147;249m",
            Role::Syntax => b"\x1b[38;2;92;184;255m",
            Role::Value => b"\x1b[1;38;2;92;184;255m",
            Role::Advisory => b"\x1b[1;38;2;243;156;18m",
            Role::Failure => b"\x1b[1;38;2;255;92;87m",
            Role::Success => b"\x1b[1;38;2;90;247;142m",
            Role::Muted => b"\x1b[38;2;112;120;140m",
        }
    }

    pub(super) fn reset(self) -> &'static [u8] {
        if self.0 { b"\x1b[0m" } else { b"" }
    }
}

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

    #[test]
    fn destination_detection_and_explicit_overrides_are_independent() {
        for stdout_terminal in [false, true] {
            for stderr_terminal in [false, true] {
                for suppressed in [false, true] {
                    let detection = Detection {
                        stdout_terminal,
                        stderr_terminal,
                        suppressed,
                    };
                    for (destination, terminal) in [
                        (Destination::Stdout, stdout_terminal),
                        (Destination::Stderr, stderr_terminal),
                    ] {
                        for (mode, enabled) in [
                            (ColorMode::Auto, terminal && !suppressed),
                            (ColorMode::Always, true),
                            (ColorMode::Never, false),
                        ] {
                            let style = detection.style(mode, destination);
                            for (role, sequence) in [
                                (Role::Heading, b"\x1b[1;38;2;189;147;249m".as_slice()),
                                (Role::Syntax, b"\x1b[38;2;92;184;255m"),
                                (Role::Advisory, b"\x1b[1;38;2;243;156;18m"),
                                (Role::Failure, b"\x1b[1;38;2;255;92;87m"),
                            ] {
                                assert_eq!(style.start(role), if enabled { sequence } else { b"" });
                            }
                            assert_eq!(
                                style.reset(),
                                if enabled { b"\x1b[0m".as_slice() } else { b"" }
                            );
                        }
                    }
                }
            }
        }
    }
}