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