Merge pull request #1344 from khjorth/neovide_channel_from_api_info

Get channel from nvim.get_api_info().
macos-click-through
multisn8 3 years ago committed by GitHub
commit 7a3bf522ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -276,48 +276,6 @@ pub enum RedrawEvent {
},
}
#[derive(Debug)]
pub enum ChannelStreamType {
Stdio,
Stderr,
Socket,
Job,
}
impl Default for ChannelStreamType {
fn default() -> Self {
Self::Stdio
}
}
#[derive(Debug)]
pub enum ChannelMode {
Bytes,
Terminal,
Rpc,
}
impl Default for ChannelMode {
fn default() -> Self {
Self::Bytes
}
}
#[derive(Debug, Default)]
pub struct ClientInfo {
pub name: String,
}
#[derive(Debug, Default)]
pub struct ChannelInfo {
pub id: u64,
pub stream: ChannelStreamType,
pub mode: ChannelMode,
pub pty: Option<String>,
pub buffer: Option<String>,
pub client: Option<ClientInfo>,
}
fn unpack_color(packed_color: u64) -> Color4f {
let packed_color = packed_color as u32;
let r = ((packed_color & 0x00ff_0000) >> 16) as f32;
@ -919,74 +877,3 @@ pub fn parse_redraw_event(event_value: Value) -> Result<Vec<RedrawEvent>> {
Ok(parsed_events)
}
pub fn parse_channel_stream_type(channel_stream_value: Value) -> Result<ChannelStreamType> {
match parse_string(channel_stream_value)?.as_ref() {
"stdio" => Ok(ChannelStreamType::Stdio),
"stderr" => Ok(ChannelStreamType::Stderr),
"socket" => Ok(ChannelStreamType::Socket),
"job" => Ok(ChannelStreamType::Job),
stream_type => Err(ParseError::Format(format!("{:?}", stream_type))),
}
}
pub fn parse_channel_mode(channel_mode_value: Value) -> Result<ChannelMode> {
match parse_string(channel_mode_value)?.as_ref() {
"bytes" => Ok(ChannelMode::Bytes),
"terminal" => Ok(ChannelMode::Terminal),
"rpc" => Ok(ChannelMode::Rpc),
channel_mode => Err(ParseError::Format(format!("{:?}", channel_mode))),
}
}
pub fn parse_client_info(client_info_value: Value) -> Result<ClientInfo> {
let client_info_map = parse_map(client_info_value)?;
let mut client_info = ClientInfo::default();
for info_property in client_info_map {
if let (Value::String(name), value) = info_property {
match (name.as_str().unwrap(), value) {
("name", name) => client_info.name = parse_string(name)?,
_ => debug!("Ignored client type property: {}", name),
}
} else {
debug!("Invalid client info format");
}
}
Ok(client_info)
}
pub fn parse_channel_info(channel_value: Value) -> Result<ChannelInfo> {
let channel_map = parse_map(channel_value)?;
let mut channel_info = ChannelInfo::default();
for channel_property in channel_map {
if let (Value::String(name), value) = channel_property {
match (name.as_str().unwrap(), value) {
("id", channel_id) => channel_info.id = parse_u64(channel_id)?,
("stream", stream) => channel_info.stream = parse_channel_stream_type(stream)?,
("mode", mode) => channel_info.mode = parse_channel_mode(mode)?,
("pty", pty) => channel_info.pty = Some(parse_string(pty)?),
("buffer", buffer) => channel_info.buffer = Some(parse_string(buffer)?),
("client", client_info) => {
channel_info.client = Some(parse_client_info(client_info)?)
}
_ => debug!("Ignored channel info property: {}", name),
}
} else {
debug!("Invalid channel info format");
}
}
Ok(channel_info)
}
pub fn parse_channel_list(channel_infos: Vec<Value>) -> Result<Vec<ChannelInfo>> {
channel_infos
.into_iter()
.map(parse_channel_info)
.collect::<Result<Vec<ChannelInfo>>>()
}

@ -2,10 +2,7 @@ use log::{info, warn};
use nvim_rs::Neovim;
use rmpv::Value;
use crate::{
bridge::{events::*, TxWrapper},
error_handling::ResultPanicExplanation,
};
use crate::{bridge::TxWrapper, error_handling::ResultPanicExplanation};
pub async fn setup_neovide_remote_clipboard(nvim: &Neovim<TxWrapper>, neovide_channel: u64) {
// users can opt-out with
@ -84,20 +81,10 @@ pub async fn setup_neovide_specific_state(nvim: &Neovim<TxWrapper>, is_remote: b
// Retrieve the channel number for communicating with neovide
let neovide_channel: Option<u64> = nvim
.list_chans()
.get_api_info()
.await
.ok()
.and_then(|channel_values| parse_channel_list(channel_values).ok())
.and_then(|channel_list| {
channel_list.iter().find_map(|channel| match channel {
ChannelInfo {
id,
client: Some(ClientInfo { name, .. }),
..
} if name == "neovide" => Some(*id),
_ => None,
})
});
.and_then(|info| info[0].as_u64());
if let Some(neovide_channel) = neovide_channel {
// Record the channel to the log

Loading…
Cancel
Save