nc.rs

 1use anyhow::Result;
 2
 3#[cfg(windows)]
 4pub fn main(_socket: &str) -> Result<()> {
 5    // It looks like we can't get an async stdio stream on Windows from smol.
 6    //
 7    // We decided to merge this with a panic on Windows since this is only used
 8    // by the experimental Claude Code Agent Server.
 9    //
10    // We're tracking this internally, and we will address it before shipping the integration.
11    panic!("--nc isn't yet supported on Windows");
12}
13
14/// The main function for when Zed is running in netcat mode
15#[cfg(not(windows))]
16pub fn main(socket: &str) -> Result<()> {
17    use futures::{AsyncReadExt as _, AsyncWriteExt as _, FutureExt as _, io::BufReader, select};
18    use net::async_net::UnixStream;
19    use smol::{Async, io::AsyncBufReadExt};
20
21    smol::block_on(async {
22        let socket_stream = UnixStream::connect(socket).await?;
23        let (socket_read, mut socket_write) = socket_stream.split();
24        let mut socket_reader = BufReader::new(socket_read);
25
26        let mut stdout = Async::new(std::io::stdout())?;
27        let stdin = Async::new(std::io::stdin())?;
28        let mut stdin_reader = BufReader::new(stdin);
29
30        let mut socket_line = Vec::new();
31        let mut stdin_line = Vec::new();
32
33        loop {
34            select! {
35                bytes_read = socket_reader.read_until(b'\n', &mut socket_line).fuse() => {
36                    if bytes_read? == 0 {
37                        break
38                    }
39                    stdout.write_all(&socket_line).await?;
40                    stdout.flush().await?;
41                    socket_line.clear();
42                }
43                bytes_read = stdin_reader.read_until(b'\n', &mut stdin_line).fuse() => {
44                    if bytes_read? == 0 {
45                        break
46                    }
47                    socket_write.write_all(&stdin_line).await?;
48                    socket_write.flush().await?;
49                    stdin_line.clear();
50                }
51            }
52        }
53
54        anyhow::Ok(())
55    })
56}