fn spawn_pty_if_needed(&mut self, logical_size: &Size, view_key: ViewKey) -> Result<(), Error> {
        if self.pty.is_some() {
            return Ok(());
        }

        // need to have non zero size before we can spawn
        if logical_size.width < 1.0 || logical_size.height < 1.0 {
            return Ok(());
        }

        let mut pty_wrapper = PtyWrapper::new()?;
        let pty_clone = pty_wrapper.pty.clone();

        let window_size =
            WindowSize { width: logical_size.width as u32, height: logical_size.height as u32 };

        let mut pty_receiver = pty_wrapper.take_receiver();
        let app_context = self.app_context.clone();

        fasync::spawn_local(async move {
            let mut pty = pty_clone.borrow_mut();
            pty.spawn(window_size).await.expect("failed to spawn pty");

            let fd = pty.try_clone_fd().expect("unable to clone pty read fd");
            let mut evented_fd = unsafe {
                // EventedFd::new() is unsafe because it can't guarantee the lifetime of
                // the file descriptor passed to it exceeds the lifetime of the EventedFd.
                // Since we're cloning the file when passing it in, the EventedFd
                // effectively owns that file descriptor and thus controls it's lifetime.
                fasync::net::EventedFd::new(fd).expect("failed to create evented_fd for io_loop")
            };

            drop(pty);

            let mut read_buf = [0u8; 1024];
            loop {
                let mut read_fut = evented_fd.read(&mut read_buf).fuse();
                select!(
                    result = read_fut => {
                        let bytes_read = result.unwrap_or_else(|e: std::io::Error| {
                            eprintln!(
                                "failed to read bytes from io_loop, dropping current message: {:?}",
                                e
                            );
                            0
                        });

                        if bytes_read > 0 {
                            for byte in &read_buf[0..bytes_read] {
                                app_context.queue_message(view_key, make_message(PtyIncomingMessages::ByteReceived(*byte)));
                            }
                        }
                    },
                result = pty_receiver.next().fuse() => {
                        let message = result.expect("failed to unwrap pty send event");
                        match message {
                            PtyOutgoingMessages::SendInput(string) => {
                                evented_fd.write_all(string.as_bytes()).await.unwrap_or_else(|e| {
                                    println!("failed to write character to pty: {}", e)
                                });
                            },
                            PtyOutgoingMessages::Resize(size) => {
                                let pty = pty_clone.borrow_mut();
                                let window_size = WindowSize { width: size.width as u32, height: size.height as u32 };
                                pty.resize(window_size).await.unwrap_or_else(|e: failure::Error| {
                                    eprintln!("failed to send resize message to pty: {:?}", e)
                                });
                            }
                        }
                    }
                )
            }
        });

        self.pty = Some(pty_wrapper);

        Ok(())
    }
/// Checks to see if the Pty has been spawned and if not it does so.
Checks to see if the Pty has been spawned and if not it does so.
Checks to see if the Pty has been spawned and if not it does so .
fn spawn_pty_if_needed(&mut self, logical_size: &Size, view_key: ViewKey) -> Result<(), Error> {
        if self.pty.is_some() {
            return Ok(());
        }
        // need to have non zero size before we can spawn
        if logical_size.width < 1.0 || logical_size.height < 1.0 {
            return Ok(());
        }
        let mut pty_wrapper = PtyWrapper::new()?;
        let pty_clone = pty_wrapper.pty.clone();
        let window_size =
            WindowSize { width: logical_size.width as u32, height: logical_size.height as u32 };
        let mut pty_receiver = pty_wrapper.take_receiver();
        let app_context = self.app_context.clone();
        fasync::spawn_local(async move {
            let mut pty = pty_clone.borrow_mut();
            pty.spawn(window_size).await.expect("failed to spawn pty");
            let fd = pty.try_clone_fd().expect("unable to clone pty read fd");
            let mut evented_fd = unsafe {
                // EventedFd::new() is unsafe because it can't guarantee the lifetime of
                // the file descriptor passed to it exceeds the lifetime of the EventedFd.
                // Since we're cloning the file when passing it in, the EventedFd
                // effectively owns that file descriptor and thus controls it's lifetime.
                fasync::net::EventedFd::new(fd).expect("failed to create evented_fd for io_loop")
            };
            drop(pty);
            let mut read_buf = [0u8; 1024];
            loop {
                let mut read_fut = evented_fd.read(&mut read_buf).fuse();
                select!(
                    result = read_fut => {
                        let bytes_read = result.unwrap_or_else(|e: std::io::Error| {
                            eprintln!(
                                "failed to read bytes from io_loop, dropping current message: {:?}",
                                e
                            );
                            0
                        });
                        if bytes_read > 0 {
                            for byte in &read_buf[0..bytes_read] {
                                app_context.queue_message(view_key, make_message(PtyIncomingMessages::ByteReceived(*byte)));
                            }
                        }
                    },
                result = pty_receiver.next().fuse() => {
                        let message = result.expect("failed to unwrap pty send event");
                        match message {
                            PtyOutgoingMessages::SendInput(string) => {
                                evented_fd.write_all(string.as_bytes()).await.unwrap_or_else(|e| {
                                    println!("failed to write character to pty: {}", e)
                                });
                            },
                            PtyOutgoingMessages::Resize(size) => {
                                let pty = pty_clone.borrow_mut();
                                let window_size = WindowSize { width: size.width as u32, height: size.height as u32 };
                                pty.resize(window_size).await.unwrap_or_else(|e: failure::Error| {
                                    eprintln!("failed to send resize message to pty: {:?}", e)
                                });
                            }
                        }
                    }
                )
            }
        });
        self.pty = Some(pty_wrapper);
        Ok(())
    }
fn spawn_pty_if_needed ( & mut self , logical_size : & Size , view_key : ViewKey ) -> Result < ( ) , Error > { if self . pty . is_some ( ) { return Ok ( ( ) ) ; } // need to have non zero size before we can spawn if logical_size . width < 1.0 || logical_size . height < 1.0 { return Ok ( ( ) ) ; } let mut pty_wrapper = PtyWrapper :: new ( ) ? ; let pty_clone = pty_wrapper . pty . clone ( ) ; let window_size = WindowSize { width : logical_size . width as u32 , height : logical_size . height as u32 } ; let mut pty_receiver = pty_wrapper . take_receiver ( ) ; let app_context = self . app_context . clone ( ) ; fasync :: spawn_local ( async move { let mut pty = pty_clone . borrow_mut ( ) ; pty . spawn ( window_size ) . await . expect ( " " ) ; let fd = pty . try_clone_fd ( ) . expect ( " " ) ; let mut evented_fd = unsafe { // EventedFd::new() is unsafe because it can't guarantee the lifetime of // the file descriptor passed to it exceeds the lifetime of the EventedFd. // Since we're cloning the file when passing it in, the EventedFd // effectively owns that file descriptor and thus controls it's lifetime. fasync :: net :: EventedFd :: new ( fd ) . expect ( " " ) } ; drop ( pty ) ; let mut read_buf = [ 0u8 ; 1024 ] ; loop { let mut read_fut = evented_fd . read ( & mut read_buf ) . fuse ( ) ; select ! ( result read_fut { let bytes_read result unwrap_or_else ( e std io Error { eprintln ( " " e ) 0 } ) if bytes_read 0 { for byte in read_buf [ 0 bytes_read ] { app_context queue_message ( view_key make_message ( PtyIncomingMessages ByteReceived ( byte ) ) ) } } } result pty_receiver next ( ) fuse ( ) { let message result expect ( " " ) match message { PtyOutgoingMessages SendInput ( string ) { evented_fd write_all ( string as_bytes ( ) ) await unwrap_or_else ( e { println ( " " e ) } ) } PtyOutgoingMessages Resize ( size ) { let pty pty_clone borrow_mut ( ) let window_size WindowSize { width size width as u32 height size height as u32 } pty resize ( window_size ) await unwrap_or_else ( e failure Error { eprintln ( " " e ) } ) } } } ) } } ) ; self . pty = Some ( pty_wrapper ) ; Ok ( ( ) ) }
Checks to see if the Pty has been spawned and if not it does so.
Checks to see if the Pty has been spawned and if not it does so .
&mut self
logical_size
&Size
view_key
ViewKey