little-kitty 0.0.3

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

use std::io::*;

//
// KittySession
//

/// Kitty graphics protocol session.
///
/// In raw mode. Restores the original mode when dropped.
///
/// Optionally includes a locked stdin reader for the response.
pub struct KittySession {
    /// Reader.
    pub reader: Option<StdinLock<'static>>,

    _raw_mode: RawMode,
}

impl KittySession {
    /// Constructor.
    pub fn new(expect_response: bool) -> Result<Self> {
        let _raw_mode = RawMode::new()?; // before getting stdin
        Ok(Self { reader: if expect_response { Some(stdin().lock()) } else { None }, _raw_mode })
    }
}

impl TryFrom<KittySession> for Option<Response> {
    type Error = Error;

    fn try_from(session: KittySession) -> Result<Self> {
        session.reader.and_then(|mut reader| reader.read_response().transpose()).transpose()
    }
}