supercode-frontend-tui 0.4.5

Attachable terminal frontend primitives for Supercode SDK runtimes.
Documentation
// Derived from OpenAI Codex: codex-rs/tui/src/notifications/mod.rs
// Pinned source: 8604689ec5e3437eb79802d8d72249b7722fbf5b
// Copyright 2025 OpenAI
// Licensed under the Apache License, Version 2.0.
// Modified by the Supercode contributors; see docs/legal/codex-frontend-extraction.toml.

//! Desktop notification selection from explicit terminal capabilities.

mod bel;
mod osc9;

use std::io;

use bel::BelBackend;
use osc9::Osc9Backend;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NotificationMethod {
    Auto,
    Osc9,
    Bell,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct NotificationCapabilities {
    pub osc9: bool,
    pub tmux_passthrough: bool,
}

#[derive(Debug)]
pub enum DesktopNotificationBackend {
    Osc9(Osc9Backend),
    Bell(BelBackend),
}

impl DesktopNotificationBackend {
    pub fn new(method: NotificationMethod, capabilities: NotificationCapabilities) -> Self {
        match method {
            NotificationMethod::Auto if capabilities.osc9 => {
                Self::Osc9(Osc9Backend::new(capabilities.tmux_passthrough))
            }
            NotificationMethod::Osc9 => Self::Osc9(Osc9Backend::new(capabilities.tmux_passthrough)),
            NotificationMethod::Auto | NotificationMethod::Bell => Self::Bell(BelBackend),
        }
    }

    pub fn method(&self) -> NotificationMethod {
        match self {
            Self::Osc9(_) => NotificationMethod::Osc9,
            Self::Bell(_) => NotificationMethod::Bell,
        }
    }

    pub fn notify(&mut self, message: &str) -> io::Result<()> {
        match self {
            Self::Osc9(backend) => backend.notify(message),
            Self::Bell(backend) => backend.notify(message),
        }
    }
}

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

    #[test]
    fn auto_uses_osc9_only_when_the_runtime_reports_support() {
        let osc9 = DesktopNotificationBackend::new(
            NotificationMethod::Auto,
            NotificationCapabilities {
                osc9: true,
                tmux_passthrough: true,
            },
        );
        assert_eq!(osc9.method(), NotificationMethod::Osc9);

        let bell = DesktopNotificationBackend::new(
            NotificationMethod::Auto,
            NotificationCapabilities::default(),
        );
        assert_eq!(bell.method(), NotificationMethod::Bell);
    }

    #[test]
    fn explicit_methods_do_not_depend_on_terminal_detection() {
        assert_eq!(
            DesktopNotificationBackend::new(
                NotificationMethod::Osc9,
                NotificationCapabilities::default(),
            )
            .method(),
            NotificationMethod::Osc9
        );
        assert_eq!(
            DesktopNotificationBackend::new(
                NotificationMethod::Bell,
                NotificationCapabilities {
                    osc9: true,
                    tmux_passthrough: true,
                },
            )
            .method(),
            NotificationMethod::Bell
        );
    }
}