little-kitty 0.0.3

A low-level interface for the Kitty Graphics Protocol
Documentation
use super::{super::utils::*, control_value::*};

use std::io::*;

//
// Controls
//

/// Vector of Kitty graphics protocol controls.
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq)]
pub struct Controls(pub Vec<(char, ControlValue)>);

impl Controls {
    /// Whether we are empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Add control.
    pub fn add<ControlT>(&mut self, key: char, value: ControlT)
    where
        ControlT: Into<ControlValue>,
    {
        self.0.push((key, value.into()));
    }

    /// Add control.
    ///
    /// Chainable.
    pub fn with<ControlT>(mut self, key: char, value: ControlT) -> Self
    where
        ControlT: Into<ControlValue>,
    {
        self.add(key, value);
        self
    }

    /// Write.
    pub fn write<WriteT>(&self, mut writer: WriteT) -> Result<()>
    where
        WriteT: Write,
    {
        let mut iterator = self.0.iter().peekable();
        while let Some((key, value)) = iterator.next() {
            writer.write_char(*key)?;
            writer.write_all(b"=")?;
            value.write(&mut writer)?;
            if iterator.peek().is_some() {
                writer.write_all(b",")?;
            }
        }
        Ok(())
    }
}