navy-nvim-rs 0.0.18

A library for writing neovim rpc clients
//! Some manually implemented API functions
use std::io::{self, Error, ErrorKind};

use rmpv::{Value, ValueRef};

use crate::{error::TypeError, neovim::Neovim, rpc::handler::Handler, uioptions::UiAttachOptions};

impl<H: Handler> Neovim<H> {
    /// Register as a remote UI.
    ///
    /// After this method is called, the client will receive redraw notifications.
    pub async fn ui_attach(
        &self,
        width: i64,
        height: i64,
        opts: UiAttachOptions<'_>,
    ) -> io::Result<()> {
        let opts = opts.into_value();
        let args = (width, height, &opts);

        self.request("nvim_ui_attach", args).await?;
        Ok(())
    }

    pub async fn get_api_info(&self) -> io::Result<Vec<Value>> {
        match self.request("nvim_get_api_info", ()).await? {
            Value::Array(values) => Ok(values),
            value => Err(Error::new(
                ErrorKind::InvalidData,
                TypeError::new("nvim_get_api_info", "array", value),
            )),
        }
    }

    pub async fn request_input(&self, keys: &str) -> io::Result<i64> {
        let value = self.request("nvim_input", &ValueRef::from(keys)).await?;
        i64::try_from(value).map_err(|value| {
            Error::new(
                ErrorKind::InvalidData,
                TypeError::new("nvim_input", "integer", value),
            )
        })
    }

    #[inline]
    pub async fn notify_input(&self, keys: &str) -> io::Result<()> {
        self.notify("nvim_input", keys).await
    }

    #[inline]
    pub async fn out_write(&self, str: &str) -> io::Result<()> {
        self.notify("nvim_out_write", str).await
    }

    #[inline]
    pub async fn err_write(&self, str: &str) -> io::Result<()> {
        self.notify("nvim_err_write", str).await
    }

    #[inline]
    pub async fn err_writeln(&self, str: &str) -> io::Result<()> {
        self.notify("nvim_err_writeln", str).await
    }

    #[inline]
    pub async fn ui_set_focus(&self, gained: bool) -> io::Result<()> {
        self.notify("nvim_ui_set_focus", gained).await
    }

    #[inline]
    pub async fn ui_try_resize(&self, width: i64, height: i64) -> io::Result<()> {
        let args = (width, height);
        self.notify("nvim_ui_try_resize", args).await
    }

    pub async fn cmd(&self, cmd: ValueRef<'_>, opts: ValueRef<'_>) -> io::Result<String> {
        let value = self.request("nvim_cmd", (&cmd, &opts)).await?;
        String::try_from(value).map_err(|value| {
            Error::new(
                ErrorKind::InvalidData,
                TypeError::new("nvim_cmd", "string", value),
            )
        })
    }
}