outputs.rs

  1use std::time::Duration;
  2
  3use gpui::{
  4    percentage, Animation, AnimationExt, AnyElement, ClipboardItem, Render, Transformation, View,
  5};
  6use runtimelib::{ExecutionState, JupyterMessageContent, MimeBundle, MimeType};
  7use ui::{div, prelude::*, v_flex, IntoElement, Styled, Tooltip, ViewContext};
  8
  9mod image;
 10use image::ImageView;
 11
 12mod markdown;
 13use markdown::MarkdownView;
 14
 15mod table;
 16use table::TableView;
 17
 18pub mod plain;
 19use plain::TerminalOutput;
 20
 21mod user_error;
 22use user_error::ErrorView;
 23
 24/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
 25fn rank_mime_type(mimetype: &MimeType) -> usize {
 26    match mimetype {
 27        MimeType::DataTable(_) => 6,
 28        MimeType::Png(_) => 4,
 29        MimeType::Jpeg(_) => 3,
 30        MimeType::Markdown(_) => 2,
 31        MimeType::Plain(_) => 1,
 32        // All other media types are not supported in Zed at this time
 33        _ => 0,
 34    }
 35}
 36
 37pub(crate) trait SupportsClipboard {
 38    fn clipboard_content(&self, cx: &WindowContext) -> Option<ClipboardItem>;
 39    fn has_clipboard_content(&self, cx: &WindowContext) -> bool;
 40}
 41
 42pub struct Output {
 43    content: OutputContent,
 44    display_id: Option<String>,
 45}
 46
 47impl Output {
 48    pub fn new(data: &MimeBundle, display_id: Option<String>, cx: &mut WindowContext) -> Self {
 49        Self {
 50            content: OutputContent::new(data, cx),
 51            display_id,
 52        }
 53    }
 54
 55    pub fn from(content: OutputContent) -> Self {
 56        Self {
 57            content,
 58            display_id: None,
 59        }
 60    }
 61}
 62
 63impl SupportsClipboard for Output {
 64    fn clipboard_content(&self, cx: &WindowContext) -> Option<ClipboardItem> {
 65        match &self.content {
 66            OutputContent::Plain(terminal) => terminal.clipboard_content(cx),
 67            OutputContent::Stream(terminal) => terminal.clipboard_content(cx),
 68            OutputContent::Image(image) => image.clipboard_content(cx),
 69            OutputContent::ErrorOutput(error) => error.traceback.clipboard_content(cx),
 70            OutputContent::Message(_) => None,
 71            OutputContent::Table(table) => table.clipboard_content(cx),
 72            OutputContent::Markdown(markdown) => markdown.read(cx).clipboard_content(cx),
 73            OutputContent::ClearOutputWaitMarker => None,
 74        }
 75    }
 76
 77    fn has_clipboard_content(&self, cx: &WindowContext) -> bool {
 78        match &self.content {
 79            OutputContent::Plain(terminal) => terminal.has_clipboard_content(cx),
 80            OutputContent::Stream(terminal) => terminal.has_clipboard_content(cx),
 81            OutputContent::Image(image) => image.has_clipboard_content(cx),
 82            OutputContent::ErrorOutput(error) => error.traceback.has_clipboard_content(cx),
 83            OutputContent::Message(_) => false,
 84            OutputContent::Table(table) => table.has_clipboard_content(cx),
 85            OutputContent::Markdown(markdown) => markdown.read(cx).has_clipboard_content(cx),
 86            OutputContent::ClearOutputWaitMarker => false,
 87        }
 88    }
 89}
 90
 91pub enum OutputContent {
 92    Plain(TerminalOutput),
 93    Stream(TerminalOutput),
 94    Image(ImageView),
 95    ErrorOutput(ErrorView),
 96    Message(String),
 97    Table(TableView),
 98    Markdown(View<MarkdownView>),
 99    ClearOutputWaitMarker,
100}
101
102impl OutputContent {
103    fn render(&self, cx: &mut ViewContext<ExecutionView>) -> Option<AnyElement> {
104        let el = match self {
105            // Note: in typical frontends we would show the execute_result.execution_count
106            // Here we can just handle either
107            Self::Plain(stdio) => Some(stdio.render(cx)),
108            Self::Markdown(markdown) => Some(markdown.clone().into_any_element()),
109            Self::Stream(stdio) => Some(stdio.render(cx)),
110            Self::Image(image) => Some(image.render(cx)),
111            Self::Message(message) => Some(div().child(message.clone()).into_any_element()),
112            Self::Table(table) => Some(table.render(cx)),
113            Self::ErrorOutput(error_view) => error_view.render(cx),
114            Self::ClearOutputWaitMarker => None,
115        };
116
117        el
118    }
119
120    pub fn new(data: &MimeBundle, cx: &mut WindowContext) -> Self {
121        match data.richest(rank_mime_type) {
122            Some(MimeType::Plain(text)) => OutputContent::Plain(TerminalOutput::from(text, cx)),
123            Some(MimeType::Markdown(text)) => {
124                let view = cx.new_view(|cx| MarkdownView::from(text.clone(), cx));
125                OutputContent::Markdown(view)
126            }
127            Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
128                Ok(view) => OutputContent::Image(view),
129                Err(error) => OutputContent::Message(format!("Failed to load image: {}", error)),
130            },
131            Some(MimeType::DataTable(data)) => {
132                OutputContent::Table(TableView::new(data.clone(), cx))
133            }
134            // Any other media types are not supported
135            _ => OutputContent::Message("Unsupported media type".to_string()),
136        }
137    }
138}
139
140#[derive(Default, Clone, Debug)]
141pub enum ExecutionStatus {
142    #[default]
143    Unknown,
144    ConnectingToKernel,
145    Queued,
146    Executing,
147    Finished,
148    ShuttingDown,
149    Shutdown,
150    KernelErrored(String),
151    Restarting,
152}
153
154/// An ExecutionView shows the outputs of an execution.
155/// It can hold zero or more outputs, which the user
156/// sees as "the output" for a single execution.
157pub struct ExecutionView {
158    pub outputs: Vec<Output>,
159    pub status: ExecutionStatus,
160}
161
162impl ExecutionView {
163    pub fn new(status: ExecutionStatus, _cx: &mut ViewContext<Self>) -> Self {
164        Self {
165            outputs: Default::default(),
166            status,
167        }
168    }
169
170    /// Accept a Jupyter message belonging to this execution
171    pub fn push_message(&mut self, message: &JupyterMessageContent, cx: &mut ViewContext<Self>) {
172        let output: Output = match message {
173            JupyterMessageContent::ExecuteResult(result) => Output::new(
174                &result.data,
175                result.transient.as_ref().and_then(|t| t.display_id.clone()),
176                cx,
177            ),
178            JupyterMessageContent::DisplayData(result) => {
179                Output::new(&result.data, result.transient.display_id.clone(), cx)
180            }
181            JupyterMessageContent::StreamContent(result) => {
182                // Previous stream data will combine together, handling colors, carriage returns, etc
183                if let Some(new_terminal) = self.apply_terminal_text(&result.text, cx) {
184                    Output::from(new_terminal)
185                } else {
186                    return;
187                }
188            }
189            JupyterMessageContent::ErrorOutput(result) => {
190                let mut terminal = TerminalOutput::new(cx);
191                terminal.append_text(&result.traceback.join("\n"));
192
193                Output::from(OutputContent::ErrorOutput(ErrorView {
194                    ename: result.ename.clone(),
195                    evalue: result.evalue.clone(),
196                    traceback: terminal,
197                }))
198            }
199            JupyterMessageContent::ExecuteReply(reply) => {
200                for payload in reply.payload.iter() {
201                    match payload {
202                        // Pager data comes in via `?` at the end of a statement in Python, used for showing documentation.
203                        // Some UI will show this as a popup. For ease of implementation, it's included as an output here.
204                        runtimelib::Payload::Page { data, .. } => {
205                            let output = Output::new(data, None, cx);
206                            self.outputs.push(output);
207                        }
208
209                        // There are other payloads that could be handled here, such as updating the input.
210                        // Below are the other payloads that _could_ be handled, but are not required for Zed.
211
212                        // Set next input adds text to the next cell. Not required to support.
213                        // However, this could be implemented by adding text to the buffer.
214                        // Trigger in python using `get_ipython().set_next_input("text")`
215                        //
216                        // runtimelib::Payload::SetNextInput { text, replace } => {},
217
218                        // Not likely to be used in the context of Zed, where someone could just open the buffer themselves
219                        // Python users can trigger this with the `%edit` magic command
220                        // runtimelib::Payload::EditMagic { filename, line_number } => {},
221
222                        // Ask the user if they want to exit the kernel. Not required to support.
223                        // runtimelib::Payload::AskExit { keepkernel } => {},
224                        _ => {}
225                    }
226                }
227                cx.notify();
228                return;
229            }
230            JupyterMessageContent::ClearOutput(options) => {
231                if !options.wait {
232                    self.outputs.clear();
233                    cx.notify();
234                    return;
235                }
236
237                // Create a marker to clear the output after we get in a new output
238                Output::from(OutputContent::ClearOutputWaitMarker)
239            }
240            JupyterMessageContent::Status(status) => {
241                match status.execution_state {
242                    ExecutionState::Busy => {
243                        self.status = ExecutionStatus::Executing;
244                    }
245                    ExecutionState::Idle => self.status = ExecutionStatus::Finished,
246                }
247                cx.notify();
248                return;
249            }
250            _msg => {
251                return;
252            }
253        };
254
255        // Check for a clear output marker as the previous output, so we can clear it out
256        if let Some(output) = self.outputs.last() {
257            if let OutputContent::ClearOutputWaitMarker = output.content {
258                self.outputs.clear();
259            }
260        }
261
262        self.outputs.push(output);
263
264        cx.notify();
265    }
266
267    pub fn update_display_data(
268        &mut self,
269        data: &MimeBundle,
270        display_id: &str,
271        cx: &mut ViewContext<Self>,
272    ) {
273        let mut any = false;
274
275        self.outputs.iter_mut().for_each(|output| {
276            if let Some(other_display_id) = output.display_id.as_ref() {
277                if other_display_id == display_id {
278                    output.content = OutputContent::new(data, cx);
279                    any = true;
280                }
281            }
282        });
283
284        if any {
285            cx.notify();
286        }
287    }
288
289    fn apply_terminal_text(
290        &mut self,
291        text: &str,
292        cx: &mut ViewContext<Self>,
293    ) -> Option<OutputContent> {
294        if let Some(last_output) = self.outputs.last_mut() {
295            match &mut last_output.content {
296                OutputContent::Stream(last_stream) => {
297                    last_stream.append_text(text);
298                    // Don't need to add a new output, we already have a terminal output
299                    cx.notify();
300                    return None;
301                }
302                // Edge case note: a clear output marker
303                OutputContent::ClearOutputWaitMarker => {
304                    // Edge case note: a clear output marker is handled by the caller
305                    // since we will return a new output at the end here as a new terminal output
306                }
307                // A different output type is "in the way", so we need to create a new output,
308                // which is the same as having no prior output
309                _ => {}
310            }
311        }
312
313        let mut new_terminal = TerminalOutput::new(cx);
314        new_terminal.append_text(text);
315        Some(OutputContent::Stream(new_terminal))
316    }
317}
318
319impl Render for ExecutionView {
320    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
321        let status = match &self.status {
322            ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
323                .color(Color::Muted)
324                .into_any_element(),
325            ExecutionStatus::Executing => h_flex()
326                .gap_2()
327                .child(
328                    Icon::new(IconName::ArrowCircle)
329                        .size(IconSize::Small)
330                        .color(Color::Muted)
331                        .with_animation(
332                            "arrow-circle",
333                            Animation::new(Duration::from_secs(3)).repeat(),
334                            |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
335                        ),
336                )
337                .child(Label::new("Executing...").color(Color::Muted))
338                .into_any_element(),
339            ExecutionStatus::Finished => Icon::new(IconName::Check)
340                .size(IconSize::Small)
341                .into_any_element(),
342            ExecutionStatus::Unknown => Label::new("Unknown status")
343                .color(Color::Muted)
344                .into_any_element(),
345            ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
346                .color(Color::Muted)
347                .into_any_element(),
348            ExecutionStatus::Restarting => Label::new("Kernel restarting...")
349                .color(Color::Muted)
350                .into_any_element(),
351            ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
352                .color(Color::Muted)
353                .into_any_element(),
354            ExecutionStatus::Queued => Label::new("Queued...")
355                .color(Color::Muted)
356                .into_any_element(),
357            ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
358                .color(Color::Error)
359                .into_any_element(),
360        };
361
362        if self.outputs.len() == 0 {
363            return v_flex()
364                .min_h(cx.line_height())
365                .justify_center()
366                .child(status)
367                .into_any_element();
368        }
369
370        div()
371            .w_full()
372            .children(self.outputs.iter().enumerate().map(|(index, output)| {
373                h_flex()
374                    .w_full()
375                    .items_start()
376                    .child(
377                        div().flex_1().child(
378                            output
379                                .content
380                                .render(cx)
381                                .unwrap_or_else(|| div().into_any_element()),
382                        ),
383                    )
384                    .when(output.has_clipboard_content(cx), |el| {
385                        let clipboard_content = output.clipboard_content(cx);
386
387                        el.child(
388                            div().pl_1().child(
389                                IconButton::new(
390                                    ElementId::Name(format!("copy-output-{}", index).into()),
391                                    IconName::Copy,
392                                )
393                                .style(ButtonStyle::Transparent)
394                                .tooltip(move |cx| Tooltip::text("Copy Output", cx))
395                                .on_click(cx.listener(
396                                    move |_, _, cx| {
397                                        if let Some(clipboard_content) = clipboard_content.as_ref()
398                                        {
399                                            cx.write_to_clipboard(clipboard_content.clone());
400                                            // todo!(): let the user know that the content was copied
401                                        }
402                                    },
403                                )),
404                            ),
405                        )
406                    })
407            }))
408            .children(match self.status {
409                ExecutionStatus::Executing => vec![status],
410                ExecutionStatus::Queued => vec![status],
411                _ => vec![],
412            })
413            .into_any_element()
414    }
415}