| 267 | } |
| 268 | |
| 269 | pub async fn read_from_session(&self, id: &str) -> Result<PtyOutput, PtyError> { |
| 270 | let sessions = self.sessions.read().await; |
| 271 | let (_, inner) = sessions |
| 272 | .get(id) |
| 273 | .ok_or_else(|| PtyError::SessionNotFound(id.to_string()))?; |
| 274 | |
| 275 | let data = { |
| 276 | let mut buf = inner.output_buffer.lock().unwrap(); |
| 277 | let bytes: Vec<u8> = buf.drain(..).collect(); |
| 278 | String::from_utf8_lossy(&bytes).into_owned() |
| 279 | }; |
| 280 | |
| 281 | Ok(PtyOutput { |
| 282 | session_id: id.to_string(), |
| 283 | data, |
| 284 | is_error: false, |
| 285 | }) |
| 286 | } |
| 287 | |
| 288 | /// Returns a snapshot of the current output buffer, the cumulative cursor |
| 289 | /// position, a broadcast receiver for live output, and a clone of the PTY |