devela 0.28.0

A development substrate of coherence.
Documentation
// devela::sys::os::term::cap::field
//
//! Defines [`TermCaps`].
//

use crate::{ColorDepth, TermCap};

crate::bitfield! {
    #[must_use]
    #[doc = crate::_tags!(term runtime set bit)]
    /// Terminal capability bits.
    #[doc = crate::_doc_meta!{
        location("sys/os/term"),
        test_size_of(TermCaps = 4|32),
    }]
    ///
    /// Stores independent terminal feature flags plus the maximum known color depth.
    ///
    /// See also [`TermCap`].
    pub struct TermCaps(u32) {
        /* input */
        /// Keyboard input.
        KEYBOARD = 0;
        /// Mouse press/release.
        MOUSE = 1;
        /// Mouse drag.
        MOUSE_DRAG = 2;
        /// Mouse motion.
        MOUSE_MOTION = 3;
        /// SGR mouse cell-coordinate mode.
        MOUSE_SGR = 4;
        /// SGR mouse pixel-coordinate mode.
        MOUSE_SGR_PIXELS = 5;
        /// Focus-in/focus-out.
        FOCUS = 6;
        /// Bracketed paste.
        BRACKETED_PASTE = 7;
        /// Terminal resize.
        RESIZE = 8;

        /* output */
        /// ANSI/VT escape sequences.
        ANSI = 9;
        /// Cursor movement and visibility control.
        CURSOR = 10;
        /// SGR styling, such as bold, reset, and colors.
        STYLE = 11;
        /// Alternate screen buffer.
        ALT_SCREEN = 12;
        /// Synchronized output updates.
        SYNC_UPDATE = 13;

        /* image */
        /// Sixel image output.
        SIXEL = 14;
        /// Kitty graphics protocol image output.
        KITTY_IMAGE = 15;
        /// iTerm2 inline image output.
        ITERM_IMAGE = 16;

        /* query replies */
        /// Device-attributes query replies.
        QUERY_DEVICE_ATTRS = 17;
        /// Cursor-position query replies.
        QUERY_CURSOR_POS = 18;
        /// Color query replies.
        QUERY_COLOR = 19;

        /// Terminal color depth.
        COLOR_DEPTH = 20..=22; // 3-bit
    }
    impl {
        /// Returns an empty terminal capability set.
        pub const EMPTY: Self = Self::new();

        /// A conservative ANSI terminal baseline.
        pub const ANSI_BASE: Self = Self::new()
            .with_keyboard(1)
            .with_ansi(1)
            .with_cursor(1)
            .with_style(1)
            .with_color_depth_enum(ColorDepth::Ansi4);

        /// Returns the maximum known terminal color depth.
        #[must_use]
        pub const fn color_depth(self) -> ColorDepth {
            ColorDepth::from_bits_trunc(self.get_color_depth() as u8)
        }
        /// Returns a copy with the given color depth.
        pub const fn with_color_depth_enum(self, depth: ColorDepth) -> Self {
            self.with_color_depth(depth as u32)
        }
        /// Sets the maximum known terminal color depth.
        pub const fn set_color_depth_enum(&mut self, depth: ColorDepth) {
            self.set_color_depth(depth as u32);
        }

        /// Returns whether `cap` is present.
        #[must_use]
        pub const fn has(self, cap: TermCap) -> bool {
            self.bits() & cap.bit() != 0
        }
        /// Returns a copy with `cap` enabled.
        pub const fn set_cap(&mut self, cap: TermCap) {
            *self = self.with_cap(cap);
        }
        /// Returns a copy with `cap` enabled.
        pub const fn unset_cap(&mut self, cap: TermCap) {
            *self = self.without_cap(cap);
        }
        /// Returns a copy with `cap` enabled.
        pub const fn with_cap(self, cap: TermCap) -> Self {
            Self::from_bits(self.bits() | cap.bit())
        }
        /// Returns a copy with `cap` disabled.
        pub const fn without_cap(self, cap: TermCap) -> Self {
            Self::from_bits(self.bits() & !cap.bit())
        }
    }
}