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