outputs.rs

  1//! # REPL Output Module
  2//!
  3//! This module provides the core functionality for handling and displaying
  4//! various types of output from Jupyter kernels.
  5//!
  6//! ## Key Components
  7//!
  8//! - `OutputContent`: An enum that encapsulates different types of output content.
  9//! - `ExecutionView`: Manages the display of outputs for a single execution.
 10//! - `ExecutionStatus`: Represents the current status of an execution.
 11//!
 12//! ## Output Types
 13//!
 14//! The module supports several output types, including:
 15//! - Plain text
 16//! - Markdown
 17//! - Images (PNG and JPEG)
 18//! - Tables
 19//! - Error messages
 20//!
 21//! ## Clipboard Support
 22//!
 23//! Most output types implement the `SupportsClipboard` trait, allowing
 24//! users to easily copy output content to the system clipboard.
 25//!
 26//! ## Rendering
 27//!
 28//! The module provides rendering capabilities for each output type,
 29//! ensuring proper display within the REPL interface.
 30//!
 31//! ## Jupyter Integration
 32//!
 33//! This module is designed to work with Jupyter message protocols,
 34//! interpreting and displaying various types of Jupyter output.
 35
 36use std::time::Duration;
 37
 38use editor::{Editor, MultiBuffer};
 39use gpui::{
 40    Animation, AnimationExt, AnyElement, ClipboardItem, Entity, Render, Transformation, WeakEntity,
 41    percentage,
 42};
 43use language::Buffer;
 44use runtimelib::{ExecutionState, JupyterMessageContent, MimeBundle, MimeType};
 45use ui::{Context, IntoElement, Styled, Tooltip, Window, div, prelude::*, v_flex};
 46
 47mod image;
 48use image::ImageView;
 49
 50mod markdown;
 51use markdown::MarkdownView;
 52
 53mod table;
 54use table::TableView;
 55
 56pub mod plain;
 57use plain::TerminalOutput;
 58
 59pub(crate) mod user_error;
 60use user_error::ErrorView;
 61use workspace::Workspace;
 62
 63/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
 64fn rank_mime_type(mimetype: &MimeType) -> usize {
 65    match mimetype {
 66        MimeType::DataTable(_) => 6,
 67        MimeType::Png(_) => 4,
 68        MimeType::Jpeg(_) => 3,
 69        MimeType::Markdown(_) => 2,
 70        MimeType::Plain(_) => 1,
 71        // All other media types are not supported in Zed at this time
 72        _ => 0,
 73    }
 74}
 75
 76pub(crate) trait OutputContent {
 77    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem>;
 78    fn has_clipboard_content(&self, _window: &Window, _cx: &App) -> bool {
 79        false
 80    }
 81    fn has_buffer_content(&self, _window: &Window, _cx: &App) -> bool {
 82        false
 83    }
 84    fn buffer_content(&mut self, _window: &mut Window, _cx: &mut App) -> Option<Entity<Buffer>> {
 85        None
 86    }
 87}
 88
 89impl<V: OutputContent + 'static> OutputContent for Entity<V> {
 90    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem> {
 91        self.read(cx).clipboard_content(window, cx)
 92    }
 93
 94    fn has_clipboard_content(&self, window: &Window, cx: &App) -> bool {
 95        self.read(cx).has_clipboard_content(window, cx)
 96    }
 97
 98    fn has_buffer_content(&self, window: &Window, cx: &App) -> bool {
 99        self.read(cx).has_buffer_content(window, cx)
100    }
101
102    fn buffer_content(&mut self, window: &mut Window, cx: &mut App) -> Option<Entity<Buffer>> {
103        self.update(cx, |item, cx| item.buffer_content(window, cx))
104    }
105}
106
107pub enum Output {
108    Plain {
109        content: Entity<TerminalOutput>,
110        display_id: Option<String>,
111    },
112    Stream {
113        content: Entity<TerminalOutput>,
114    },
115    Image {
116        content: Entity<ImageView>,
117        display_id: Option<String>,
118    },
119    ErrorOutput(ErrorView),
120    Message(String),
121    Table {
122        content: Entity<TableView>,
123        display_id: Option<String>,
124    },
125    Markdown {
126        content: Entity<MarkdownView>,
127        display_id: Option<String>,
128    },
129    ClearOutputWaitMarker,
130}
131
132impl Output {
133    fn render_output_controls<V: OutputContent + 'static>(
134        v: Entity<V>,
135        workspace: WeakEntity<Workspace>,
136        window: &mut Window,
137        cx: &mut Context<ExecutionView>,
138    ) -> Option<AnyElement> {
139        if !v.has_clipboard_content(window, cx) && !v.has_buffer_content(window, cx) {
140            return None;
141        }
142
143        Some(
144            h_flex()
145                .pl_1()
146                .when(v.has_clipboard_content(window, cx), |el| {
147                    let v = v.clone();
148                    el.child(
149                        IconButton::new(ElementId::Name("copy-output".into()), IconName::Copy)
150                            .style(ButtonStyle::Transparent)
151                            .tooltip(Tooltip::text("Copy Output"))
152                            .on_click(cx.listener(move |_, _, window, cx| {
153                                let clipboard_content = v.clipboard_content(window, cx);
154
155                                if let Some(clipboard_content) = clipboard_content.as_ref() {
156                                    cx.write_to_clipboard(clipboard_content.clone());
157                                }
158                            })),
159                    )
160                })
161                .when(v.has_buffer_content(window, cx), |el| {
162                    let v = v.clone();
163                    el.child(
164                        IconButton::new(
165                            ElementId::Name("open-in-buffer".into()),
166                            IconName::FileTextOutlined,
167                        )
168                        .style(ButtonStyle::Transparent)
169                        .tooltip(Tooltip::text("Open in Buffer"))
170                        .on_click(cx.listener({
171                            let workspace = workspace.clone();
172
173                            move |_, _, window, cx| {
174                                let buffer_content =
175                                    v.update(cx, |item, cx| item.buffer_content(window, cx));
176
177                                if let Some(buffer_content) = buffer_content.as_ref() {
178                                    let buffer = buffer_content.clone();
179                                    let editor = Box::new(cx.new(|cx| {
180                                        let multibuffer = cx.new(|cx| {
181                                            let mut multi_buffer =
182                                                MultiBuffer::singleton(buffer.clone(), cx);
183
184                                            multi_buffer.set_title("REPL Output".to_string(), cx);
185                                            multi_buffer
186                                        });
187
188                                        Editor::for_multibuffer(multibuffer, None, window, cx)
189                                    }));
190                                    workspace
191                                        .update(cx, |workspace, cx| {
192                                            workspace.add_item_to_active_pane(
193                                                editor, None, true, window, cx,
194                                            );
195                                        })
196                                        .ok();
197                                }
198                            }
199                        })),
200                    )
201                })
202                .into_any_element(),
203        )
204    }
205
206    pub fn render(
207        &self,
208        workspace: WeakEntity<Workspace>,
209        window: &mut Window,
210        cx: &mut Context<ExecutionView>,
211    ) -> impl IntoElement + use<> {
212        let content = match self {
213            Self::Plain { content, .. } => Some(content.clone().into_any_element()),
214            Self::Markdown { content, .. } => Some(content.clone().into_any_element()),
215            Self::Stream { content, .. } => Some(content.clone().into_any_element()),
216            Self::Image { content, .. } => Some(content.clone().into_any_element()),
217            Self::Message(message) => Some(div().child(message.clone()).into_any_element()),
218            Self::Table { content, .. } => Some(content.clone().into_any_element()),
219            Self::ErrorOutput(error_view) => error_view.render(window, cx),
220            Self::ClearOutputWaitMarker => None,
221        };
222
223        h_flex()
224            .id("output-content")
225            .w_full()
226            .overflow_x_scroll()
227            .items_start()
228            .child(div().flex_1().children(content))
229            .children(match self {
230                Self::Plain { content, .. } => {
231                    Self::render_output_controls(content.clone(), workspace.clone(), window, cx)
232                }
233                Self::Markdown { content, .. } => {
234                    Self::render_output_controls(content.clone(), workspace.clone(), window, cx)
235                }
236                Self::Stream { content, .. } => {
237                    Self::render_output_controls(content.clone(), workspace.clone(), window, cx)
238                }
239                Self::Image { content, .. } => {
240                    Self::render_output_controls(content.clone(), workspace.clone(), window, cx)
241                }
242                Self::ErrorOutput(err) => Self::render_output_controls(
243                    err.traceback.clone(),
244                    workspace.clone(),
245                    window,
246                    cx,
247                ),
248                Self::Message(_) => None,
249                Self::Table { content, .. } => {
250                    Self::render_output_controls(content.clone(), workspace.clone(), window, cx)
251                }
252                Self::ClearOutputWaitMarker => None,
253            })
254    }
255
256    pub fn display_id(&self) -> Option<String> {
257        match self {
258            Output::Plain { display_id, .. } => display_id.clone(),
259            Output::Stream { .. } => None,
260            Output::Image { display_id, .. } => display_id.clone(),
261            Output::ErrorOutput(_) => None,
262            Output::Message(_) => None,
263            Output::Table { display_id, .. } => display_id.clone(),
264            Output::Markdown { display_id, .. } => display_id.clone(),
265            Output::ClearOutputWaitMarker => None,
266        }
267    }
268
269    pub fn new(
270        data: &MimeBundle,
271        display_id: Option<String>,
272        window: &mut Window,
273        cx: &mut App,
274    ) -> Self {
275        match data.richest(rank_mime_type) {
276            Some(MimeType::Plain(text)) => Output::Plain {
277                content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
278                display_id,
279            },
280            Some(MimeType::Markdown(text)) => {
281                let content = cx.new(|cx| MarkdownView::from(text.clone(), cx));
282                Output::Markdown {
283                    content,
284                    display_id,
285                }
286            }
287            Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
288                Ok(view) => Output::Image {
289                    content: cx.new(|_| view),
290                    display_id,
291                },
292                Err(error) => Output::Message(format!("Failed to load image: {}", error)),
293            },
294            Some(MimeType::DataTable(data)) => Output::Table {
295                content: cx.new(|cx| TableView::new(data, window, cx)),
296                display_id,
297            },
298            // Any other media types are not supported
299            _ => Output::Message("Unsupported media type".to_string()),
300        }
301    }
302}
303
304#[derive(Default, Clone, Debug)]
305pub enum ExecutionStatus {
306    #[default]
307    Unknown,
308    ConnectingToKernel,
309    Queued,
310    Executing,
311    Finished,
312    ShuttingDown,
313    Shutdown,
314    KernelErrored(String),
315    Restarting,
316}
317
318/// An ExecutionView shows the outputs of an execution.
319/// It can hold zero or more outputs, which the user
320/// sees as "the output" for a single execution.
321pub struct ExecutionView {
322    #[allow(unused)]
323    workspace: WeakEntity<Workspace>,
324    pub outputs: Vec<Output>,
325    pub status: ExecutionStatus,
326}
327
328impl ExecutionView {
329    pub fn new(
330        status: ExecutionStatus,
331        workspace: WeakEntity<Workspace>,
332        _cx: &mut Context<Self>,
333    ) -> Self {
334        Self {
335            workspace,
336            outputs: Default::default(),
337            status,
338        }
339    }
340
341    /// Accept a Jupyter message belonging to this execution
342    pub fn push_message(
343        &mut self,
344        message: &JupyterMessageContent,
345        window: &mut Window,
346        cx: &mut Context<Self>,
347    ) {
348        let output: Output = match message {
349            JupyterMessageContent::ExecuteResult(result) => Output::new(
350                &result.data,
351                result.transient.as_ref().and_then(|t| t.display_id.clone()),
352                window,
353                cx,
354            ),
355            JupyterMessageContent::DisplayData(result) => Output::new(
356                &result.data,
357                result.transient.as_ref().and_then(|t| t.display_id.clone()),
358                window,
359                cx,
360            ),
361            JupyterMessageContent::StreamContent(result) => {
362                // Previous stream data will combine together, handling colors, carriage returns, etc
363                if let Some(new_terminal) = self.apply_terminal_text(&result.text, window, cx) {
364                    new_terminal
365                } else {
366                    return;
367                }
368            }
369            JupyterMessageContent::ErrorOutput(result) => {
370                let terminal =
371                    cx.new(|cx| TerminalOutput::from(&result.traceback.join("\n"), window, cx));
372
373                Output::ErrorOutput(ErrorView {
374                    ename: result.ename.clone(),
375                    evalue: result.evalue.clone(),
376                    traceback: terminal,
377                })
378            }
379            JupyterMessageContent::ExecuteReply(reply) => {
380                for payload in reply.payload.iter() {
381                    if let runtimelib::Payload::Page { data, .. } = payload {
382                        let output = Output::new(data, None, window, cx);
383                        self.outputs.push(output);
384                    }
385                }
386                cx.notify();
387                return;
388            }
389            JupyterMessageContent::ClearOutput(options) => {
390                if !options.wait {
391                    self.outputs.clear();
392                    cx.notify();
393                    return;
394                }
395
396                // Create a marker to clear the output after we get in a new output
397                Output::ClearOutputWaitMarker
398            }
399            JupyterMessageContent::Status(status) => {
400                match status.execution_state {
401                    ExecutionState::Busy => {
402                        self.status = ExecutionStatus::Executing;
403                    }
404                    ExecutionState::Idle => self.status = ExecutionStatus::Finished,
405                }
406                cx.notify();
407                return;
408            }
409            _msg => {
410                return;
411            }
412        };
413
414        // Check for a clear output marker as the previous output, so we can clear it out
415        if let Some(output) = self.outputs.last() {
416            if let Output::ClearOutputWaitMarker = output {
417                self.outputs.clear();
418            }
419        }
420
421        self.outputs.push(output);
422
423        cx.notify();
424    }
425
426    pub fn update_display_data(
427        &mut self,
428        data: &MimeBundle,
429        display_id: &str,
430        window: &mut Window,
431        cx: &mut Context<Self>,
432    ) {
433        let mut any = false;
434
435        self.outputs.iter_mut().for_each(|output| {
436            if let Some(other_display_id) = output.display_id().as_ref() {
437                if other_display_id == display_id {
438                    *output = Output::new(data, Some(display_id.to_owned()), window, cx);
439                    any = true;
440                }
441            }
442        });
443
444        if any {
445            cx.notify();
446        }
447    }
448
449    fn apply_terminal_text(
450        &mut self,
451        text: &str,
452        window: &mut Window,
453        cx: &mut Context<Self>,
454    ) -> Option<Output> {
455        if let Some(last_output) = self.outputs.last_mut() {
456            if let Output::Stream {
457                content: last_stream,
458            } = last_output
459            {
460                // Don't need to add a new output, we already have a terminal output
461                // and can just update the most recent terminal output
462                last_stream.update(cx, |last_stream, cx| {
463                    last_stream.append_text(text, cx);
464                    cx.notify();
465                });
466                return None;
467            }
468        }
469
470        Some(Output::Stream {
471            content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
472        })
473    }
474}
475
476impl Render for ExecutionView {
477    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
478        let status = match &self.status {
479            ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
480                .color(Color::Muted)
481                .into_any_element(),
482            ExecutionStatus::Executing => h_flex()
483                .gap_2()
484                .child(
485                    Icon::new(IconName::ArrowCircle)
486                        .size(IconSize::Small)
487                        .color(Color::Muted)
488                        .with_animation(
489                            "arrow-circle",
490                            Animation::new(Duration::from_secs(3)).repeat(),
491                            |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
492                        ),
493                )
494                .child(Label::new("Executing...").color(Color::Muted))
495                .into_any_element(),
496            ExecutionStatus::Finished => Icon::new(IconName::Check)
497                .size(IconSize::Small)
498                .into_any_element(),
499            ExecutionStatus::Unknown => Label::new("Unknown status")
500                .color(Color::Muted)
501                .into_any_element(),
502            ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
503                .color(Color::Muted)
504                .into_any_element(),
505            ExecutionStatus::Restarting => Label::new("Kernel restarting...")
506                .color(Color::Muted)
507                .into_any_element(),
508            ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
509                .color(Color::Muted)
510                .into_any_element(),
511            ExecutionStatus::Queued => Label::new("Queued...")
512                .color(Color::Muted)
513                .into_any_element(),
514            ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
515                .color(Color::Error)
516                .into_any_element(),
517        };
518
519        if self.outputs.is_empty() {
520            return v_flex()
521                .min_h(window.line_height())
522                .justify_center()
523                .child(status)
524                .into_any_element();
525        }
526
527        div()
528            .w_full()
529            .children(
530                self.outputs
531                    .iter()
532                    .map(|output| output.render(self.workspace.clone(), window, cx)),
533            )
534            .children(match self.status {
535                ExecutionStatus::Executing => vec![status],
536                ExecutionStatus::Queued => vec![status],
537                _ => vec![],
538            })
539            .into_any_element()
540    }
541}