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 editor::{Editor, MultiBuffer};
 37use gpui::{AnyElement, ClipboardItem, Entity, EventEmitter, Render, WeakEntity};
 38use language::Buffer;
 39use runtimelib::{ExecutionState, JupyterMessageContent, MimeBundle, MimeType};
 40use ui::{CommonAnimationExt, CopyButton, IconButton, Tooltip, prelude::*};
 41
 42mod image;
 43use image::ImageView;
 44
 45mod markdown;
 46use markdown::MarkdownView;
 47
 48mod table;
 49use table::TableView;
 50
 51pub mod plain;
 52use plain::TerminalOutput;
 53
 54pub(crate) mod user_error;
 55use user_error::ErrorView;
 56use workspace::Workspace;
 57
 58use crate::repl_settings::ReplSettings;
 59use settings::Settings;
 60
 61/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
 62fn rank_mime_type(mimetype: &MimeType) -> usize {
 63    match mimetype {
 64        MimeType::DataTable(_) => 6,
 65        MimeType::Png(_) => 4,
 66        MimeType::Jpeg(_) => 3,
 67        MimeType::Markdown(_) => 2,
 68        MimeType::Plain(_) => 1,
 69        // All other media types are not supported in Zed at this time
 70        _ => 0,
 71    }
 72}
 73
 74pub(crate) trait OutputContent {
 75    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem>;
 76    fn has_clipboard_content(&self, _window: &Window, _cx: &App) -> bool {
 77        false
 78    }
 79    fn has_buffer_content(&self, _window: &Window, _cx: &App) -> bool {
 80        false
 81    }
 82    fn buffer_content(&mut self, _window: &mut Window, _cx: &mut App) -> Option<Entity<Buffer>> {
 83        None
 84    }
 85}
 86
 87impl<V: OutputContent + 'static> OutputContent for Entity<V> {
 88    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem> {
 89        self.read(cx).clipboard_content(window, cx)
 90    }
 91
 92    fn has_clipboard_content(&self, window: &Window, cx: &App) -> bool {
 93        self.read(cx).has_clipboard_content(window, cx)
 94    }
 95
 96    fn has_buffer_content(&self, window: &Window, cx: &App) -> bool {
 97        self.read(cx).has_buffer_content(window, cx)
 98    }
 99
100    fn buffer_content(&mut self, window: &mut Window, cx: &mut App) -> Option<Entity<Buffer>> {
101        self.update(cx, |item, cx| item.buffer_content(window, cx))
102    }
103}
104
105pub enum Output {
106    Plain {
107        content: Entity<TerminalOutput>,
108        display_id: Option<String>,
109    },
110    Stream {
111        content: Entity<TerminalOutput>,
112    },
113    Image {
114        content: Entity<ImageView>,
115        display_id: Option<String>,
116    },
117    ErrorOutput(ErrorView),
118    Message(String),
119    Table {
120        content: Entity<TableView>,
121        display_id: Option<String>,
122    },
123    Markdown {
124        content: Entity<MarkdownView>,
125        display_id: Option<String>,
126    },
127    ClearOutputWaitMarker,
128}
129
130impl Output {
131    fn render_output_controls<V: OutputContent + 'static>(
132        v: Entity<V>,
133        workspace: WeakEntity<Workspace>,
134        window: &mut Window,
135        cx: &mut Context<ExecutionView>,
136    ) -> Option<AnyElement> {
137        if !v.has_clipboard_content(window, cx) && !v.has_buffer_content(window, cx) {
138            return None;
139        }
140
141        Some(
142            h_flex()
143                .pl_1()
144                .when(v.has_clipboard_content(window, cx), |el| {
145                    let v = v.clone();
146                    el.child(
147                        IconButton::new(ElementId::Name("copy-output".into()), IconName::Copy)
148                            .style(ButtonStyle::Transparent)
149                            .tooltip(Tooltip::text("Copy Output"))
150                            .on_click(move |_, window, cx| {
151                                let clipboard_content = v.clipboard_content(window, cx);
152
153                                if let Some(clipboard_content) = clipboard_content.as_ref() {
154                                    cx.write_to_clipboard(clipboard_content.clone());
155                                }
156                            }),
157                    )
158                })
159                .when(v.has_buffer_content(window, cx), |el| {
160                    let v = v.clone();
161                    el.child(
162                        IconButton::new(
163                            ElementId::Name("open-in-buffer".into()),
164                            IconName::FileTextOutlined,
165                        )
166                        .style(ButtonStyle::Transparent)
167                        .tooltip(Tooltip::text("Open in Buffer"))
168                        .on_click({
169                            let workspace = workspace.clone();
170                            move |_, window, cx| {
171                                let buffer_content =
172                                    v.update(cx, |item, cx| item.buffer_content(window, cx));
173
174                                if let Some(buffer_content) = buffer_content.as_ref() {
175                                    let buffer = buffer_content.clone();
176                                    let editor = Box::new(cx.new(|cx| {
177                                        let multibuffer = cx.new(|cx| {
178                                            let mut multi_buffer =
179                                                MultiBuffer::singleton(buffer.clone(), cx);
180
181                                            multi_buffer.set_title("REPL Output".to_string(), cx);
182                                            multi_buffer
183                                        });
184
185                                        Editor::for_multibuffer(multibuffer, None, window, cx)
186                                    }));
187                                    workspace
188                                        .update(cx, |workspace, cx| {
189                                            workspace.add_item_to_active_pane(
190                                                editor, None, true, window, cx,
191                                            );
192                                        })
193                                        .ok();
194                                }
195                            }
196                        }),
197                    )
198                })
199                .into_any_element(),
200        )
201    }
202
203    pub fn render(
204        &self,
205        workspace: WeakEntity<Workspace>,
206        window: &mut Window,
207        cx: &mut Context<ExecutionView>,
208    ) -> impl IntoElement + use<> {
209        let content = match self {
210            Self::Plain { content, .. } => Some(content.clone().into_any_element()),
211            Self::Markdown { content, .. } => Some(content.clone().into_any_element()),
212            Self::Stream { content, .. } => Some(content.clone().into_any_element()),
213            Self::Image { content, .. } => Some(content.clone().into_any_element()),
214            Self::Message(message) => Some(div().child(message.clone()).into_any_element()),
215            Self::Table { content, .. } => Some(content.clone().into_any_element()),
216            Self::ErrorOutput(error_view) => error_view.render(window, cx),
217            Self::ClearOutputWaitMarker => None,
218        };
219
220        h_flex()
221            .id("output-content")
222            .w_full()
223            .overflow_x_scroll()
224            .items_start()
225            .child(div().flex_1().children(content))
226            .children(match self {
227                Self::Plain { content, .. } => {
228                    Self::render_output_controls(content.clone(), workspace, window, cx)
229                }
230                Self::Markdown { content, .. } => {
231                    Self::render_output_controls(content.clone(), workspace, window, cx)
232                }
233                Self::Stream { content, .. } => {
234                    Self::render_output_controls(content.clone(), workspace, window, cx)
235                }
236                Self::Image { content, .. } => {
237                    Self::render_output_controls(content.clone(), workspace, window, cx)
238                }
239                Self::ErrorOutput(err) => Some(
240                    h_flex()
241                        .pl_1()
242                        .child({
243                            let ename = err.ename.clone();
244                            let evalue = err.evalue.clone();
245                            let traceback = err.traceback.clone();
246                            let traceback_text = traceback.read(cx).full_text();
247                            let full_error = format!("{}: {}\n{}", ename, evalue, traceback_text);
248
249                            CopyButton::new(full_error).tooltip_label("Copy Full Error")
250                        })
251                        .child(
252                            IconButton::new(
253                                ElementId::Name("open-full-error-in-buffer-traceback".into()),
254                                IconName::FileTextOutlined,
255                            )
256                            .style(ButtonStyle::Transparent)
257                            .tooltip(Tooltip::text("Open Full Error in Buffer"))
258                            .on_click({
259                                let ename = err.ename.clone();
260                                let evalue = err.evalue.clone();
261                                let traceback = err.traceback.clone();
262                                move |_, window, cx| {
263                                    if let Some(workspace) = workspace.upgrade() {
264                                        let traceback_text = traceback.read(cx).full_text();
265                                        let full_error =
266                                            format!("{}: {}\n{}", ename, evalue, traceback_text);
267                                        let buffer = cx.new(|cx| {
268                                            let mut buffer = Buffer::local(full_error, cx)
269                                                .with_language(language::PLAIN_TEXT.clone(), cx);
270                                            buffer
271                                                .set_capability(language::Capability::ReadOnly, cx);
272                                            buffer
273                                        });
274                                        let editor = Box::new(cx.new(|cx| {
275                                            let multibuffer = cx.new(|cx| {
276                                                let mut multi_buffer =
277                                                    MultiBuffer::singleton(buffer.clone(), cx);
278                                                multi_buffer
279                                                    .set_title("Full Error".to_string(), cx);
280                                                multi_buffer
281                                            });
282                                            Editor::for_multibuffer(multibuffer, None, window, cx)
283                                        }));
284                                        workspace.update(cx, |workspace, cx| {
285                                            workspace.add_item_to_active_pane(
286                                                editor, None, true, window, cx,
287                                            );
288                                        });
289                                    }
290                                }
291                            }),
292                        )
293                        .into_any_element(),
294                ),
295                Self::Message(_) => None,
296                Self::Table { content, .. } => {
297                    Self::render_output_controls(content.clone(), workspace, window, cx)
298                }
299                Self::ClearOutputWaitMarker => None,
300            })
301    }
302
303    pub fn display_id(&self) -> Option<String> {
304        match self {
305            Output::Plain { display_id, .. } => display_id.clone(),
306            Output::Stream { .. } => None,
307            Output::Image { display_id, .. } => display_id.clone(),
308            Output::ErrorOutput(_) => None,
309            Output::Message(_) => None,
310            Output::Table { display_id, .. } => display_id.clone(),
311            Output::Markdown { display_id, .. } => display_id.clone(),
312            Output::ClearOutputWaitMarker => None,
313        }
314    }
315
316    pub fn new(
317        data: &MimeBundle,
318        display_id: Option<String>,
319        window: &mut Window,
320        cx: &mut App,
321    ) -> Self {
322        match data.richest(rank_mime_type) {
323            Some(MimeType::Plain(text)) => Output::Plain {
324                content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
325                display_id,
326            },
327            Some(MimeType::Markdown(text)) => {
328                let content = cx.new(|cx| MarkdownView::from(text.clone(), cx));
329                Output::Markdown {
330                    content,
331                    display_id,
332                }
333            }
334            Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
335                Ok(view) => Output::Image {
336                    content: cx.new(|_| view),
337                    display_id,
338                },
339                Err(error) => Output::Message(format!("Failed to load image: {}", error)),
340            },
341            Some(MimeType::DataTable(data)) => Output::Table {
342                content: cx.new(|cx| TableView::new(data, window, cx)),
343                display_id,
344            },
345            // Any other media types are not supported
346            _ => Output::Message("Unsupported media type".to_string()),
347        }
348    }
349}
350
351#[derive(Default, Clone, Debug)]
352pub enum ExecutionStatus {
353    #[default]
354    Unknown,
355    ConnectingToKernel,
356    Queued,
357    Executing,
358    Finished,
359    ShuttingDown,
360    Shutdown,
361    KernelErrored(String),
362    Restarting,
363}
364
365pub struct ExecutionViewFinishedEmpty;
366pub struct ExecutionViewFinishedSmall(pub String);
367
368/// An ExecutionView shows the outputs of an execution.
369/// It can hold zero or more outputs, which the user
370/// sees as "the output" for a single execution.
371pub struct ExecutionView {
372    #[allow(unused)]
373    workspace: WeakEntity<Workspace>,
374    pub outputs: Vec<Output>,
375    pub status: ExecutionStatus,
376}
377
378impl EventEmitter<ExecutionViewFinishedEmpty> for ExecutionView {}
379impl EventEmitter<ExecutionViewFinishedSmall> for ExecutionView {}
380
381impl ExecutionView {
382    pub fn new(
383        status: ExecutionStatus,
384        workspace: WeakEntity<Workspace>,
385        _cx: &mut Context<Self>,
386    ) -> Self {
387        Self {
388            workspace,
389            outputs: Default::default(),
390            status,
391        }
392    }
393
394    /// Accept a Jupyter message belonging to this execution
395    pub fn push_message(
396        &mut self,
397        message: &JupyterMessageContent,
398        window: &mut Window,
399        cx: &mut Context<Self>,
400    ) {
401        let output: Output = match message {
402            JupyterMessageContent::ExecuteResult(result) => Output::new(
403                &result.data,
404                result.transient.as_ref().and_then(|t| t.display_id.clone()),
405                window,
406                cx,
407            ),
408            JupyterMessageContent::DisplayData(result) => Output::new(
409                &result.data,
410                result.transient.as_ref().and_then(|t| t.display_id.clone()),
411                window,
412                cx,
413            ),
414            JupyterMessageContent::StreamContent(result) => {
415                // Previous stream data will combine together, handling colors, carriage returns, etc
416                if let Some(new_terminal) = self.apply_terminal_text(&result.text, window, cx) {
417                    new_terminal
418                } else {
419                    return;
420                }
421            }
422            JupyterMessageContent::ErrorOutput(result) => {
423                let terminal =
424                    cx.new(|cx| TerminalOutput::from(&result.traceback.join("\n"), window, cx));
425
426                Output::ErrorOutput(ErrorView {
427                    ename: result.ename.clone(),
428                    evalue: result.evalue.clone(),
429                    traceback: terminal,
430                })
431            }
432            JupyterMessageContent::ExecuteReply(reply) => {
433                for payload in reply.payload.iter() {
434                    if let runtimelib::Payload::Page { data, .. } = payload {
435                        let output = Output::new(data, None, window, cx);
436                        self.outputs.push(output);
437                    }
438                }
439                cx.notify();
440                return;
441            }
442            JupyterMessageContent::ClearOutput(options) => {
443                if !options.wait {
444                    self.outputs.clear();
445                    cx.notify();
446                    return;
447                }
448
449                // Create a marker to clear the output after we get in a new output
450                Output::ClearOutputWaitMarker
451            }
452            JupyterMessageContent::Status(status) => {
453                match status.execution_state {
454                    ExecutionState::Busy => {
455                        self.status = ExecutionStatus::Executing;
456                    }
457                    ExecutionState::Idle => {
458                        self.status = ExecutionStatus::Finished;
459                        if self.outputs.is_empty() {
460                            cx.emit(ExecutionViewFinishedEmpty);
461                        } else if ReplSettings::get_global(cx).inline_output {
462                            if let Some(small_text) = self.get_small_inline_output(cx) {
463                                cx.emit(ExecutionViewFinishedSmall(small_text));
464                            }
465                        }
466                    }
467                    ExecutionState::Unknown => self.status = ExecutionStatus::Unknown,
468                    ExecutionState::Starting => self.status = ExecutionStatus::ConnectingToKernel,
469                    ExecutionState::Restarting => self.status = ExecutionStatus::Restarting,
470                    ExecutionState::Terminating => self.status = ExecutionStatus::ShuttingDown,
471                    ExecutionState::AutoRestarting => self.status = ExecutionStatus::Restarting,
472                    ExecutionState::Dead => self.status = ExecutionStatus::Shutdown,
473                    ExecutionState::Other(_) => self.status = ExecutionStatus::Unknown,
474                }
475                cx.notify();
476                return;
477            }
478            _msg => {
479                return;
480            }
481        };
482
483        // Check for a clear output marker as the previous output, so we can clear it out
484        if let Some(output) = self.outputs.last()
485            && let Output::ClearOutputWaitMarker = output
486        {
487            self.outputs.clear();
488        }
489
490        self.outputs.push(output);
491
492        cx.notify();
493    }
494
495    pub fn update_display_data(
496        &mut self,
497        data: &MimeBundle,
498        display_id: &str,
499        window: &mut Window,
500        cx: &mut Context<Self>,
501    ) {
502        let mut any = false;
503
504        self.outputs.iter_mut().for_each(|output| {
505            if let Some(other_display_id) = output.display_id().as_ref()
506                && other_display_id == display_id
507            {
508                *output = Output::new(data, Some(display_id.to_owned()), window, cx);
509                any = true;
510            }
511        });
512
513        if any {
514            cx.notify();
515        }
516    }
517
518    /// Check if the output is a single small plain text that can be shown inline.
519    /// Returns the text if it's suitable for inline display (single line, short enough).
520    fn get_small_inline_output(&self, cx: &App) -> Option<String> {
521        // Only consider single outputs
522        if self.outputs.len() != 1 {
523            return None;
524        }
525
526        let output = self.outputs.first()?;
527
528        // Only Plain outputs can be inlined
529        let content = match output {
530            Output::Plain { content, .. } => content,
531            _ => return None,
532        };
533
534        let text = content.read(cx).full_text();
535        let trimmed = text.trim();
536
537        let max_length = ReplSettings::get_global(cx).inline_output_max_length;
538
539        // Must be a single line and within the configured max length
540        if trimmed.contains('\n') || trimmed.len() > max_length {
541            return None;
542        }
543
544        Some(trimmed.to_string())
545    }
546
547    fn apply_terminal_text(
548        &mut self,
549        text: &str,
550        window: &mut Window,
551        cx: &mut Context<Self>,
552    ) -> Option<Output> {
553        if let Some(last_output) = self.outputs.last_mut()
554            && let Output::Stream {
555                content: last_stream,
556            } = last_output
557        {
558            // Don't need to add a new output, we already have a terminal output
559            // and can just update the most recent terminal output
560            last_stream.update(cx, |last_stream, cx| {
561                last_stream.append_text(text, cx);
562                cx.notify();
563            });
564            return None;
565        }
566
567        Some(Output::Stream {
568            content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
569        })
570    }
571}
572
573impl Render for ExecutionView {
574    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
575        let status = match &self.status {
576            ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
577                .color(Color::Muted)
578                .into_any_element(),
579            ExecutionStatus::Executing => h_flex()
580                .gap_2()
581                .child(
582                    Icon::new(IconName::ArrowCircle)
583                        .size(IconSize::Small)
584                        .color(Color::Muted)
585                        .with_rotate_animation(3),
586                )
587                .child(Label::new("Executing...").color(Color::Muted))
588                .into_any_element(),
589            ExecutionStatus::Finished => Icon::new(IconName::Check)
590                .size(IconSize::Small)
591                .into_any_element(),
592            ExecutionStatus::Unknown => Label::new("Unknown status")
593                .color(Color::Muted)
594                .into_any_element(),
595            ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
596                .color(Color::Muted)
597                .into_any_element(),
598            ExecutionStatus::Restarting => Label::new("Kernel restarting...")
599                .color(Color::Muted)
600                .into_any_element(),
601            ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
602                .color(Color::Muted)
603                .into_any_element(),
604            ExecutionStatus::Queued => Label::new("Queued...")
605                .color(Color::Muted)
606                .into_any_element(),
607            ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
608                .color(Color::Error)
609                .into_any_element(),
610        };
611
612        if self.outputs.is_empty() {
613            return v_flex()
614                .min_h(window.line_height())
615                .justify_center()
616                .child(status)
617                .into_any_element();
618        }
619
620        div()
621            .w_full()
622            .children(
623                self.outputs
624                    .iter()
625                    .map(|output| output.render(self.workspace.clone(), window, cx)),
626            )
627            .children(match self.status {
628                ExecutionStatus::Executing => vec![status],
629                ExecutionStatus::Queued => vec![status],
630                _ => vec![],
631            })
632            .into_any_element()
633    }
634}