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, Render, WeakEntity};
 38use language::Buffer;
 39use runtimelib::{ExecutionState, JupyterMessageContent, MimeBundle, MimeType};
 40use ui::{
 41    ButtonStyle, CommonAnimationExt, Context, IconButton, IconName, IntoElement, Styled, Tooltip,
 42    Window, div, h_flex, prelude::*, v_flex,
 43};
 44
 45mod image;
 46use image::ImageView;
 47
 48mod markdown;
 49use markdown::MarkdownView;
 50
 51mod table;
 52use table::TableView;
 53
 54pub mod plain;
 55use plain::TerminalOutput;
 56
 57pub(crate) mod user_error;
 58use user_error::ErrorView;
 59use workspace::Workspace;
 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) => {
240                    // Add buttons for the traceback section
241                    Some(
242                        h_flex()
243                            .pl_1()
244                            .child(
245                                IconButton::new(
246                                    ElementId::Name("copy-full-error-traceback".into()),
247                                    IconName::Copy,
248                                )
249                                .style(ButtonStyle::Transparent)
250                                .tooltip(Tooltip::text("Copy Full Error"))
251                                .on_click({
252                                    let ename = err.ename.clone();
253                                    let evalue = err.evalue.clone();
254                                    let traceback = err.traceback.clone();
255                                    move |_, _window, cx| {
256                                        let traceback_text = traceback.read(cx).full_text();
257                                        let full_error =
258                                            format!("{}: {}\n{}", ename, evalue, traceback_text);
259                                        let clipboard_content =
260                                            ClipboardItem::new_string(full_error);
261                                        cx.write_to_clipboard(clipboard_content);
262                                    }
263                                }),
264                            )
265                            .child(
266                                IconButton::new(
267                                    ElementId::Name("open-full-error-in-buffer-traceback".into()),
268                                    IconName::FileTextOutlined,
269                                )
270                                .style(ButtonStyle::Transparent)
271                                .tooltip(Tooltip::text("Open Full Error in Buffer"))
272                                .on_click({
273                                    let ename = err.ename.clone();
274                                    let evalue = err.evalue.clone();
275                                    let traceback = err.traceback.clone();
276                                    move |_, window, cx| {
277                                        if let Some(workspace) = workspace.upgrade() {
278                                            let traceback_text = traceback.read(cx).full_text();
279                                            let full_error = format!(
280                                                "{}: {}\n{}",
281                                                ename, evalue, traceback_text
282                                            );
283                                            let buffer = cx.new(|cx| {
284                                                let mut buffer = Buffer::local(full_error, cx)
285                                                    .with_language(
286                                                        language::PLAIN_TEXT.clone(),
287                                                        cx,
288                                                    );
289                                                buffer.set_capability(
290                                                    language::Capability::ReadOnly,
291                                                    cx,
292                                                );
293                                                buffer
294                                            });
295                                            let editor = Box::new(cx.new(|cx| {
296                                                let multibuffer = cx.new(|cx| {
297                                                    let mut multi_buffer =
298                                                        MultiBuffer::singleton(buffer.clone(), cx);
299                                                    multi_buffer
300                                                        .set_title("Full Error".to_string(), cx);
301                                                    multi_buffer
302                                                });
303                                                Editor::for_multibuffer(
304                                                    multibuffer,
305                                                    None,
306                                                    window,
307                                                    cx,
308                                                )
309                                            }));
310                                            workspace.update(cx, |workspace, cx| {
311                                                workspace.add_item_to_active_pane(
312                                                    editor, None, true, window, cx,
313                                                );
314                                            });
315                                        }
316                                    }
317                                }),
318                            )
319                            .into_any_element(),
320                    )
321                }
322                Self::Message(_) => None,
323                Self::Table { content, .. } => {
324                    Self::render_output_controls(content.clone(), workspace, window, cx)
325                }
326                Self::ClearOutputWaitMarker => None,
327            })
328    }
329
330    pub fn display_id(&self) -> Option<String> {
331        match self {
332            Output::Plain { display_id, .. } => display_id.clone(),
333            Output::Stream { .. } => None,
334            Output::Image { display_id, .. } => display_id.clone(),
335            Output::ErrorOutput(_) => None,
336            Output::Message(_) => None,
337            Output::Table { display_id, .. } => display_id.clone(),
338            Output::Markdown { display_id, .. } => display_id.clone(),
339            Output::ClearOutputWaitMarker => None,
340        }
341    }
342
343    pub fn new(
344        data: &MimeBundle,
345        display_id: Option<String>,
346        window: &mut Window,
347        cx: &mut App,
348    ) -> Self {
349        match data.richest(rank_mime_type) {
350            Some(MimeType::Plain(text)) => Output::Plain {
351                content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
352                display_id,
353            },
354            Some(MimeType::Markdown(text)) => {
355                let content = cx.new(|cx| MarkdownView::from(text.clone(), cx));
356                Output::Markdown {
357                    content,
358                    display_id,
359                }
360            }
361            Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
362                Ok(view) => Output::Image {
363                    content: cx.new(|_| view),
364                    display_id,
365                },
366                Err(error) => Output::Message(format!("Failed to load image: {}", error)),
367            },
368            Some(MimeType::DataTable(data)) => Output::Table {
369                content: cx.new(|cx| TableView::new(data, window, cx)),
370                display_id,
371            },
372            // Any other media types are not supported
373            _ => Output::Message("Unsupported media type".to_string()),
374        }
375    }
376}
377
378#[derive(Default, Clone, Debug)]
379pub enum ExecutionStatus {
380    #[default]
381    Unknown,
382    ConnectingToKernel,
383    Queued,
384    Executing,
385    Finished,
386    ShuttingDown,
387    Shutdown,
388    KernelErrored(String),
389    Restarting,
390}
391
392/// An ExecutionView shows the outputs of an execution.
393/// It can hold zero or more outputs, which the user
394/// sees as "the output" for a single execution.
395pub struct ExecutionView {
396    #[allow(unused)]
397    workspace: WeakEntity<Workspace>,
398    pub outputs: Vec<Output>,
399    pub status: ExecutionStatus,
400}
401
402impl ExecutionView {
403    pub fn new(
404        status: ExecutionStatus,
405        workspace: WeakEntity<Workspace>,
406        _cx: &mut Context<Self>,
407    ) -> Self {
408        Self {
409            workspace,
410            outputs: Default::default(),
411            status,
412        }
413    }
414
415    /// Accept a Jupyter message belonging to this execution
416    pub fn push_message(
417        &mut self,
418        message: &JupyterMessageContent,
419        window: &mut Window,
420        cx: &mut Context<Self>,
421    ) {
422        let output: Output = match message {
423            JupyterMessageContent::ExecuteResult(result) => Output::new(
424                &result.data,
425                result.transient.as_ref().and_then(|t| t.display_id.clone()),
426                window,
427                cx,
428            ),
429            JupyterMessageContent::DisplayData(result) => Output::new(
430                &result.data,
431                result.transient.as_ref().and_then(|t| t.display_id.clone()),
432                window,
433                cx,
434            ),
435            JupyterMessageContent::StreamContent(result) => {
436                // Previous stream data will combine together, handling colors, carriage returns, etc
437                if let Some(new_terminal) = self.apply_terminal_text(&result.text, window, cx) {
438                    new_terminal
439                } else {
440                    return;
441                }
442            }
443            JupyterMessageContent::ErrorOutput(result) => {
444                let terminal =
445                    cx.new(|cx| TerminalOutput::from(&result.traceback.join("\n"), window, cx));
446
447                Output::ErrorOutput(ErrorView {
448                    ename: result.ename.clone(),
449                    evalue: result.evalue.clone(),
450                    traceback: terminal,
451                })
452            }
453            JupyterMessageContent::ExecuteReply(reply) => {
454                for payload in reply.payload.iter() {
455                    if let runtimelib::Payload::Page { data, .. } = payload {
456                        let output = Output::new(data, None, window, cx);
457                        self.outputs.push(output);
458                    }
459                }
460                cx.notify();
461                return;
462            }
463            JupyterMessageContent::ClearOutput(options) => {
464                if !options.wait {
465                    self.outputs.clear();
466                    cx.notify();
467                    return;
468                }
469
470                // Create a marker to clear the output after we get in a new output
471                Output::ClearOutputWaitMarker
472            }
473            JupyterMessageContent::Status(status) => {
474                match status.execution_state {
475                    ExecutionState::Busy => {
476                        self.status = ExecutionStatus::Executing;
477                    }
478                    ExecutionState::Idle => self.status = ExecutionStatus::Finished,
479                    ExecutionState::Unknown => self.status = ExecutionStatus::Unknown,
480                    ExecutionState::Starting => self.status = ExecutionStatus::ConnectingToKernel,
481                    ExecutionState::Restarting => self.status = ExecutionStatus::Restarting,
482                    ExecutionState::Terminating => self.status = ExecutionStatus::ShuttingDown,
483                    ExecutionState::AutoRestarting => self.status = ExecutionStatus::Restarting,
484                    ExecutionState::Dead => self.status = ExecutionStatus::Shutdown,
485                    ExecutionState::Other(_) => self.status = ExecutionStatus::Unknown,
486                }
487                cx.notify();
488                return;
489            }
490            _msg => {
491                return;
492            }
493        };
494
495        // Check for a clear output marker as the previous output, so we can clear it out
496        if let Some(output) = self.outputs.last()
497            && let Output::ClearOutputWaitMarker = output
498        {
499            self.outputs.clear();
500        }
501
502        self.outputs.push(output);
503
504        cx.notify();
505    }
506
507    pub fn update_display_data(
508        &mut self,
509        data: &MimeBundle,
510        display_id: &str,
511        window: &mut Window,
512        cx: &mut Context<Self>,
513    ) {
514        let mut any = false;
515
516        self.outputs.iter_mut().for_each(|output| {
517            if let Some(other_display_id) = output.display_id().as_ref()
518                && other_display_id == display_id
519            {
520                *output = Output::new(data, Some(display_id.to_owned()), window, cx);
521                any = true;
522            }
523        });
524
525        if any {
526            cx.notify();
527        }
528    }
529
530    fn apply_terminal_text(
531        &mut self,
532        text: &str,
533        window: &mut Window,
534        cx: &mut Context<Self>,
535    ) -> Option<Output> {
536        if let Some(last_output) = self.outputs.last_mut()
537            && let Output::Stream {
538                content: last_stream,
539            } = last_output
540        {
541            // Don't need to add a new output, we already have a terminal output
542            // and can just update the most recent terminal output
543            last_stream.update(cx, |last_stream, cx| {
544                last_stream.append_text(text, cx);
545                cx.notify();
546            });
547            return None;
548        }
549
550        Some(Output::Stream {
551            content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
552        })
553    }
554}
555
556impl Render for ExecutionView {
557    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
558        let status = match &self.status {
559            ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
560                .color(Color::Muted)
561                .into_any_element(),
562            ExecutionStatus::Executing => h_flex()
563                .gap_2()
564                .child(
565                    Icon::new(IconName::ArrowCircle)
566                        .size(IconSize::Small)
567                        .color(Color::Muted)
568                        .with_rotate_animation(3),
569                )
570                .child(Label::new("Executing...").color(Color::Muted))
571                .into_any_element(),
572            ExecutionStatus::Finished => Icon::new(IconName::Check)
573                .size(IconSize::Small)
574                .into_any_element(),
575            ExecutionStatus::Unknown => Label::new("Unknown status")
576                .color(Color::Muted)
577                .into_any_element(),
578            ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
579                .color(Color::Muted)
580                .into_any_element(),
581            ExecutionStatus::Restarting => Label::new("Kernel restarting...")
582                .color(Color::Muted)
583                .into_any_element(),
584            ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
585                .color(Color::Muted)
586                .into_any_element(),
587            ExecutionStatus::Queued => Label::new("Queued...")
588                .color(Color::Muted)
589                .into_any_element(),
590            ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
591                .color(Color::Error)
592                .into_any_element(),
593        };
594
595        if self.outputs.is_empty() {
596            return v_flex()
597                .min_h(window.line_height())
598                .justify_center()
599                .child(status)
600                .into_any_element();
601        }
602
603        div()
604            .w_full()
605            .children(
606                self.outputs
607                    .iter()
608                    .map(|output| output.render(self.workspace.clone(), window, cx)),
609            )
610            .children(match self.status {
611                ExecutionStatus::Executing => vec![status],
612                ExecutionStatus::Queued => vec![status],
613                _ => vec![],
614            })
615            .into_any_element()
616    }
617}