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