1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::stdio::TerminalOutput;
5use anyhow::Result;
6use base64::prelude::*;
7use gpui::{
8 img, percentage, Animation, AnimationExt, AnyElement, FontWeight, ImageData, Render, TextRun,
9 Transformation,
10};
11use runtimelib::datatable::TableSchema;
12use runtimelib::media::datatable::TabularDataResource;
13use runtimelib::{ExecutionState, JupyterMessageContent, MimeBundle, MimeType};
14use serde_json::Value;
15use settings::Settings;
16use theme::ThemeSettings;
17use ui::{div, prelude::*, v_flex, IntoElement, Styled, ViewContext};
18
19/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
20fn rank_mime_type(mimetype: &MimeType) -> usize {
21 match mimetype {
22 MimeType::DataTable(_) => 6,
23 MimeType::Png(_) => 4,
24 MimeType::Jpeg(_) => 3,
25 MimeType::Markdown(_) => 2,
26 MimeType::Plain(_) => 1,
27 // All other media types are not supported in Zed at this time
28 _ => 0,
29 }
30}
31
32/// ImageView renders an image inline in an editor, adapting to the line height to fit the image.
33pub struct ImageView {
34 height: u32,
35 width: u32,
36 image: Arc<ImageData>,
37}
38
39impl ImageView {
40 fn render(&self, cx: &ViewContext<ExecutionView>) -> AnyElement {
41 let line_height = cx.line_height();
42
43 let (height, width) = if self.height as f32 / line_height.0 == u8::MAX as f32 {
44 let height = u8::MAX as f32 * line_height.0;
45 let width = self.width as f32 * height / self.height as f32;
46 (height, width)
47 } else {
48 (self.height as f32, self.width as f32)
49 };
50
51 let image = self.image.clone();
52
53 div()
54 .h(Pixels(height))
55 .w(Pixels(width))
56 .child(img(image))
57 .into_any_element()
58 }
59
60 fn from(base64_encoded_data: &str) -> Result<Self> {
61 let bytes = BASE64_STANDARD.decode(base64_encoded_data)?;
62
63 let format = image::guess_format(&bytes)?;
64 let mut data = image::load_from_memory_with_format(&bytes, format)?.into_rgba8();
65
66 // Convert from RGBA to BGRA.
67 for pixel in data.chunks_exact_mut(4) {
68 pixel.swap(0, 2);
69 }
70
71 let height = data.height();
72 let width = data.width();
73
74 let gpui_image_data = ImageData::new(vec![image::Frame::new(data)]);
75
76 return Ok(ImageView {
77 height,
78 width,
79 image: Arc::new(gpui_image_data),
80 });
81 }
82}
83
84/// TableView renders a static table inline in a buffer.
85/// It uses the https://specs.frictionlessdata.io/tabular-data-resource/ specification for data interchange.
86pub struct TableView {
87 pub table: TabularDataResource,
88 pub widths: Vec<Pixels>,
89}
90
91fn cell_content(row: &Value, field: &str) -> String {
92 match row.get(&field) {
93 Some(Value::String(s)) => s.clone(),
94 Some(Value::Number(n)) => n.to_string(),
95 Some(Value::Bool(b)) => b.to_string(),
96 Some(Value::Array(arr)) => format!("{:?}", arr),
97 Some(Value::Object(obj)) => format!("{:?}", obj),
98 Some(Value::Null) | None => String::new(),
99 }
100}
101
102// Declare constant for the padding multiple on the line height
103const TABLE_Y_PADDING_MULTIPLE: f32 = 0.5;
104
105impl TableView {
106 pub fn new(table: TabularDataResource, cx: &mut WindowContext) -> Self {
107 let mut widths = Vec::with_capacity(table.schema.fields.len());
108
109 let text_system = cx.text_system();
110 let text_style = cx.text_style();
111 let text_font = ThemeSettings::get_global(cx).buffer_font.clone();
112 let font_size = ThemeSettings::get_global(cx).buffer_font_size;
113 let mut runs = [TextRun {
114 len: 0,
115 font: text_font,
116 color: text_style.color,
117 background_color: None,
118 underline: None,
119 strikethrough: None,
120 }];
121
122 for field in table.schema.fields.iter() {
123 runs[0].len = field.name.len();
124 let mut width = text_system
125 .layout_line(&field.name, font_size, &runs)
126 .map(|layout| layout.width)
127 .unwrap_or(px(0.));
128
129 let Some(data) = table.data.as_ref() else {
130 widths.push(width);
131 continue;
132 };
133
134 for row in data {
135 let content = cell_content(&row, &field.name);
136 runs[0].len = content.len();
137 let cell_width = cx
138 .text_system()
139 .layout_line(&content, font_size, &runs)
140 .map(|layout| layout.width)
141 .unwrap_or(px(0.));
142
143 width = width.max(cell_width)
144 }
145
146 widths.push(width)
147 }
148
149 Self { table, widths }
150 }
151
152 pub fn render(&self, cx: &ViewContext<ExecutionView>) -> AnyElement {
153 let data = match &self.table.data {
154 Some(data) => data,
155 None => return div().into_any_element(),
156 };
157
158 let mut headings = serde_json::Map::new();
159 for field in &self.table.schema.fields {
160 headings.insert(field.name.clone(), Value::String(field.name.clone()));
161 }
162 let header = self.render_row(&self.table.schema, true, &Value::Object(headings), cx);
163
164 let body = data
165 .iter()
166 .map(|row| self.render_row(&self.table.schema, false, &row, cx));
167
168 v_flex()
169 .id("table")
170 .overflow_x_scroll()
171 .w_full()
172 .child(header)
173 .children(body)
174 .into_any_element()
175 }
176
177 pub fn render_row(
178 &self,
179 schema: &TableSchema,
180 is_header: bool,
181 row: &Value,
182 cx: &ViewContext<ExecutionView>,
183 ) -> AnyElement {
184 let theme = cx.theme();
185
186 let line_height = cx.line_height();
187
188 let row_cells = schema
189 .fields
190 .iter()
191 .zip(self.widths.iter())
192 .map(|(field, width)| {
193 let container = match field.field_type {
194 runtimelib::datatable::FieldType::String => div(),
195
196 runtimelib::datatable::FieldType::Number
197 | runtimelib::datatable::FieldType::Integer
198 | runtimelib::datatable::FieldType::Date
199 | runtimelib::datatable::FieldType::Time
200 | runtimelib::datatable::FieldType::Datetime
201 | runtimelib::datatable::FieldType::Year
202 | runtimelib::datatable::FieldType::Duration
203 | runtimelib::datatable::FieldType::Yearmonth => v_flex().items_end(),
204
205 _ => div(),
206 };
207
208 let value = cell_content(row, &field.name);
209
210 let mut cell = container
211 .min_w(*width + px(22.))
212 .w(*width + px(22.))
213 .child(value)
214 .px_2()
215 .py((TABLE_Y_PADDING_MULTIPLE / 2.0) * line_height)
216 .border_color(theme.colors().border);
217
218 if is_header {
219 cell = cell.border_1().bg(theme.colors().border_focused)
220 } else {
221 cell = cell.border_1()
222 }
223 cell
224 })
225 .collect::<Vec<_>>();
226
227 let mut total_width = px(0.);
228 for width in self.widths.iter() {
229 // Width fudge factor: border + 2 (heading), padding
230 total_width += *width + px(22.);
231 }
232
233 h_flex()
234 .w(total_width)
235 .children(row_cells)
236 .into_any_element()
237 }
238}
239
240/// Userspace error from the kernel
241pub struct ErrorView {
242 pub ename: String,
243 pub evalue: String,
244 pub traceback: TerminalOutput,
245}
246
247impl ErrorView {
248 fn render(&self, cx: &ViewContext<ExecutionView>) -> Option<AnyElement> {
249 let theme = cx.theme();
250
251 let padding = cx.line_height() / 2.;
252
253 Some(
254 v_flex()
255 .w_full()
256 .px(padding)
257 .py(padding)
258 .border_1()
259 .border_color(theme.status().error_border)
260 .child(
261 h_flex()
262 .font_weight(FontWeight::BOLD)
263 .child(format!("{}: {}", self.ename, self.evalue)),
264 )
265 .child(self.traceback.render(cx))
266 .into_any_element(),
267 )
268 }
269}
270
271pub struct Output {
272 content: OutputContent,
273 display_id: Option<String>,
274}
275
276impl Output {
277 pub fn new(data: &MimeBundle, display_id: Option<String>, cx: &mut WindowContext) -> Self {
278 Self {
279 content: OutputContent::new(data, cx),
280 display_id,
281 }
282 }
283
284 pub fn from(content: OutputContent) -> Self {
285 Self {
286 content,
287 display_id: None,
288 }
289 }
290}
291
292pub enum OutputContent {
293 Plain(TerminalOutput),
294 Stream(TerminalOutput),
295 Image(ImageView),
296 ErrorOutput(ErrorView),
297 Message(String),
298 Table(TableView),
299 ClearOutputWaitMarker,
300}
301
302impl std::fmt::Debug for OutputContent {
303 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
304 match self {
305 OutputContent::Plain(_) => f.debug_struct("OutputContent(Plain)"),
306 OutputContent::Stream(_) => f.debug_struct("OutputContent(Stream)"),
307 OutputContent::Image(_) => f.debug_struct("OutputContent(Image)"),
308 OutputContent::ErrorOutput(_) => f.debug_struct("OutputContent(ErrorOutput)"),
309 OutputContent::Message(_) => f.debug_struct("OutputContent(Message)"),
310 OutputContent::Table(_) => f.debug_struct("OutputContent(Table)"),
311 OutputContent::ClearOutputWaitMarker => {
312 f.debug_struct("OutputContent(ClearOutputWaitMarker)")
313 }
314 }
315 .finish()
316 }
317}
318
319impl OutputContent {
320 fn render(&self, cx: &ViewContext<ExecutionView>) -> Option<AnyElement> {
321 let el = match self {
322 // Note: in typical frontends we would show the execute_result.execution_count
323 // Here we can just handle either
324 Self::Plain(stdio) => Some(stdio.render(cx)),
325 // Self::Markdown(markdown) => Some(markdown.render(theme)),
326 Self::Stream(stdio) => Some(stdio.render(cx)),
327 Self::Image(image) => Some(image.render(cx)),
328 Self::Message(message) => Some(div().child(message.clone()).into_any_element()),
329 Self::Table(table) => Some(table.render(cx)),
330 Self::ErrorOutput(error_view) => error_view.render(cx),
331 Self::ClearOutputWaitMarker => None,
332 };
333
334 el
335 }
336
337 pub fn new(data: &MimeBundle, cx: &mut WindowContext) -> Self {
338 match data.richest(rank_mime_type) {
339 Some(MimeType::Plain(text)) => OutputContent::Plain(TerminalOutput::from(text, cx)),
340 Some(MimeType::Markdown(text)) => OutputContent::Plain(TerminalOutput::from(text, cx)),
341 Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
342 Ok(view) => OutputContent::Image(view),
343 Err(error) => OutputContent::Message(format!("Failed to load image: {}", error)),
344 },
345 Some(MimeType::DataTable(data)) => {
346 OutputContent::Table(TableView::new(data.clone(), cx))
347 }
348 // Any other media types are not supported
349 _ => OutputContent::Message("Unsupported media type".to_string()),
350 }
351 }
352}
353
354#[derive(Default, Clone, Debug)]
355pub enum ExecutionStatus {
356 #[default]
357 Unknown,
358 ConnectingToKernel,
359 Queued,
360 Executing,
361 Finished,
362 ShuttingDown,
363 Shutdown,
364 KernelErrored(String),
365}
366
367pub struct ExecutionView {
368 pub outputs: Vec<Output>,
369 pub status: ExecutionStatus,
370}
371
372impl ExecutionView {
373 pub fn new(status: ExecutionStatus, _cx: &mut ViewContext<Self>) -> Self {
374 Self {
375 outputs: Default::default(),
376 status,
377 }
378 }
379
380 /// Accept a Jupyter message belonging to this execution
381 pub fn push_message(&mut self, message: &JupyterMessageContent, cx: &mut ViewContext<Self>) {
382 let output: Output = match message {
383 JupyterMessageContent::ExecuteResult(result) => Output::new(
384 &result.data,
385 result.transient.as_ref().and_then(|t| t.display_id.clone()),
386 cx,
387 ),
388 JupyterMessageContent::DisplayData(result) => {
389 Output::new(&result.data, result.transient.display_id.clone(), cx)
390 }
391 JupyterMessageContent::StreamContent(result) => {
392 // Previous stream data will combine together, handling colors, carriage returns, etc
393 if let Some(new_terminal) = self.apply_terminal_text(&result.text, cx) {
394 Output::from(new_terminal)
395 } else {
396 return;
397 }
398 }
399 JupyterMessageContent::ErrorOutput(result) => {
400 let mut terminal = TerminalOutput::new(cx);
401 terminal.append_text(&result.traceback.join("\n"));
402
403 Output::from(OutputContent::ErrorOutput(ErrorView {
404 ename: result.ename.clone(),
405 evalue: result.evalue.clone(),
406 traceback: terminal,
407 }))
408 }
409 JupyterMessageContent::ExecuteReply(reply) => {
410 for payload in reply.payload.iter() {
411 match payload {
412 // Pager data comes in via `?` at the end of a statement in Python, used for showing documentation.
413 // Some UI will show this as a popup. For ease of implementation, it's included as an output here.
414 runtimelib::Payload::Page { data, .. } => {
415 let output = Output::new(data, None, cx);
416 self.outputs.push(output);
417 }
418
419 // There are other payloads that could be handled here, such as updating the input.
420 // Below are the other payloads that _could_ be handled, but are not required for Zed.
421
422 // Set next input adds text to the next cell. Not required to support.
423 // However, this could be implemented by adding text to the buffer.
424 // Trigger in python using `get_ipython().set_next_input("text")`
425 //
426 // runtimelib::Payload::SetNextInput { text, replace } => {},
427
428 // Not likely to be used in the context of Zed, where someone could just open the buffer themselves
429 // Python users can trigger this with the `%edit` magic command
430 // runtimelib::Payload::EditMagic { filename, line_number } => {},
431
432 // Ask the user if they want to exit the kernel. Not required to support.
433 // runtimelib::Payload::AskExit { keepkernel } => {},
434 _ => {}
435 }
436 }
437 cx.notify();
438 return;
439 }
440 JupyterMessageContent::ClearOutput(options) => {
441 if !options.wait {
442 self.outputs.clear();
443 cx.notify();
444 return;
445 }
446
447 // Create a marker to clear the output after we get in a new output
448 Output::from(OutputContent::ClearOutputWaitMarker)
449 }
450 JupyterMessageContent::Status(status) => {
451 match status.execution_state {
452 ExecutionState::Busy => {
453 self.status = ExecutionStatus::Executing;
454 }
455 ExecutionState::Idle => self.status = ExecutionStatus::Finished,
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 if let OutputContent::ClearOutputWaitMarker = output.content {
468 self.outputs.clear();
469 }
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 cx: &mut ViewContext<Self>,
482 ) {
483 let mut any = false;
484
485 self.outputs.iter_mut().for_each(|output| {
486 if let Some(other_display_id) = output.display_id.as_ref() {
487 if other_display_id == display_id {
488 output.content = OutputContent::new(data, cx);
489 any = true;
490 }
491 }
492 });
493
494 if any {
495 cx.notify();
496 }
497 }
498
499 fn apply_terminal_text(
500 &mut self,
501 text: &str,
502 cx: &mut ViewContext<Self>,
503 ) -> Option<OutputContent> {
504 if let Some(last_output) = self.outputs.last_mut() {
505 match &mut last_output.content {
506 OutputContent::Stream(last_stream) => {
507 last_stream.append_text(text);
508 // Don't need to add a new output, we already have a terminal output
509 cx.notify();
510 return None;
511 }
512 // Edge case note: a clear output marker
513 OutputContent::ClearOutputWaitMarker => {
514 // Edge case note: a clear output marker is handled by the caller
515 // since we will return a new output at the end here as a new terminal output
516 }
517 // A different output type is "in the way", so we need to create a new output,
518 // which is the same as having no prior output
519 _ => {}
520 }
521 }
522
523 let mut new_terminal = TerminalOutput::new(cx);
524 new_terminal.append_text(text);
525 Some(OutputContent::Stream(new_terminal))
526 }
527}
528
529impl Render for ExecutionView {
530 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
531 let status = match &self.status {
532 ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
533 .color(Color::Muted)
534 .into_any_element(),
535 ExecutionStatus::Executing => h_flex()
536 .gap_2()
537 .child(
538 Icon::new(IconName::ArrowCircle)
539 .size(IconSize::Small)
540 .color(Color::Muted)
541 .with_animation(
542 "arrow-circle",
543 Animation::new(Duration::from_secs(3)).repeat(),
544 |icon, delta| icon.transform(Transformation::rotate(percentage(delta))),
545 ),
546 )
547 .child(Label::new("Executing...").color(Color::Muted))
548 .into_any_element(),
549 ExecutionStatus::Finished => Icon::new(IconName::Check)
550 .size(IconSize::Small)
551 .into_any_element(),
552 ExecutionStatus::Unknown => Label::new("Unknown status")
553 .color(Color::Muted)
554 .into_any_element(),
555 ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
556 .color(Color::Muted)
557 .into_any_element(),
558 ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
559 .color(Color::Muted)
560 .into_any_element(),
561 ExecutionStatus::Queued => Label::new("Queued...")
562 .color(Color::Muted)
563 .into_any_element(),
564 ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
565 .color(Color::Error)
566 .into_any_element(),
567 };
568
569 if self.outputs.len() == 0 {
570 return v_flex()
571 .min_h(cx.line_height())
572 .justify_center()
573 .child(status)
574 .into_any_element();
575 }
576
577 div()
578 .w_full()
579 .children(
580 self.outputs
581 .iter()
582 .filter_map(|output| output.content.render(cx)),
583 )
584 .children(match self.status {
585 ExecutionStatus::Executing => vec![status],
586 ExecutionStatus::Queued => vec![status],
587 _ => vec![],
588 })
589 .into_any_element()
590 }
591}