outputs.rs

   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 menu;
  40use runtimelib::{ExecutionState, JupyterMessage, JupyterMessageContent, MimeBundle, MimeType};
  41use ui::{CommonAnimationExt, CopyButton, IconButton, Tooltip, prelude::*};
  42
  43mod image;
  44use image::ImageView;
  45
  46mod markdown;
  47use markdown::MarkdownView;
  48
  49mod table;
  50use table::TableView;
  51
  52mod json;
  53use json::JsonView;
  54
  55mod html;
  56
  57pub mod plain;
  58use plain::TerminalOutput;
  59
  60pub(crate) mod user_error;
  61use user_error::ErrorView;
  62use workspace::Workspace;
  63
  64use crate::repl_settings::ReplSettings;
  65use settings::Settings;
  66
  67/// When deciding what to render from a collection of mediatypes, we need to rank them in order of importance
  68fn rank_mime_type(mimetype: &MimeType) -> usize {
  69    match mimetype {
  70        MimeType::DataTable(_) => 7,
  71        MimeType::Html(_) => 6,
  72        MimeType::Json(_) => 5,
  73        MimeType::Png(_) => 4,
  74        MimeType::Jpeg(_) => 3,
  75        MimeType::Markdown(_) => 2,
  76        MimeType::Plain(_) => 1,
  77        // All other media types are not supported in Zed at this time
  78        _ => 0,
  79    }
  80}
  81
  82pub(crate) trait OutputContent {
  83    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem>;
  84    fn has_clipboard_content(&self, _window: &Window, _cx: &App) -> bool {
  85        false
  86    }
  87    fn has_buffer_content(&self, _window: &Window, _cx: &App) -> bool {
  88        false
  89    }
  90    fn buffer_content(&mut self, _window: &mut Window, _cx: &mut App) -> Option<Entity<Buffer>> {
  91        None
  92    }
  93}
  94
  95impl<V: OutputContent + 'static> OutputContent for Entity<V> {
  96    fn clipboard_content(&self, window: &Window, cx: &App) -> Option<ClipboardItem> {
  97        self.read(cx).clipboard_content(window, cx)
  98    }
  99
 100    fn has_clipboard_content(&self, window: &Window, cx: &App) -> bool {
 101        self.read(cx).has_clipboard_content(window, cx)
 102    }
 103
 104    fn has_buffer_content(&self, window: &Window, cx: &App) -> bool {
 105        self.read(cx).has_buffer_content(window, cx)
 106    }
 107
 108    fn buffer_content(&mut self, window: &mut Window, cx: &mut App) -> Option<Entity<Buffer>> {
 109        self.update(cx, |item, cx| item.buffer_content(window, cx))
 110    }
 111}
 112
 113pub enum Output {
 114    Plain {
 115        content: Entity<TerminalOutput>,
 116        display_id: Option<String>,
 117    },
 118    Stream {
 119        content: Entity<TerminalOutput>,
 120    },
 121    Image {
 122        content: Entity<ImageView>,
 123        display_id: Option<String>,
 124    },
 125    ErrorOutput(ErrorView),
 126    Message(String),
 127    Table {
 128        content: Entity<TableView>,
 129        display_id: Option<String>,
 130    },
 131    Markdown {
 132        content: Entity<MarkdownView>,
 133        display_id: Option<String>,
 134    },
 135    Json {
 136        content: Entity<JsonView>,
 137        display_id: Option<String>,
 138    },
 139    ClearOutputWaitMarker,
 140}
 141
 142impl Output {
 143    pub fn to_nbformat(&self, cx: &App) -> Option<nbformat::v4::Output> {
 144        match self {
 145            Output::Stream { content } => {
 146                let text = content.read(cx).full_text();
 147                Some(nbformat::v4::Output::Stream {
 148                    name: "stdout".to_string(),
 149                    text: nbformat::v4::MultilineString(text),
 150                })
 151            }
 152            Output::Plain { content, .. } => {
 153                let text = content.read(cx).full_text();
 154                let mut data = jupyter_protocol::media::Media::default();
 155                data.content.push(jupyter_protocol::MediaType::Plain(text));
 156                Some(nbformat::v4::Output::DisplayData(
 157                    nbformat::v4::DisplayData {
 158                        data,
 159                        metadata: serde_json::Map::new(),
 160                    },
 161                ))
 162            }
 163            Output::ErrorOutput(error_view) => {
 164                let traceback_text = error_view.traceback.read(cx).full_text();
 165                let traceback_lines: Vec<String> =
 166                    traceback_text.lines().map(|s| s.to_string()).collect();
 167                Some(nbformat::v4::Output::Error(nbformat::v4::ErrorOutput {
 168                    ename: error_view.ename.clone(),
 169                    evalue: error_view.evalue.clone(),
 170                    traceback: traceback_lines,
 171                }))
 172            }
 173            Output::Image { .. }
 174            | Output::Markdown { .. }
 175            | Output::Table { .. }
 176            | Output::Json { .. } => None,
 177            Output::Message(_) => None,
 178            Output::ClearOutputWaitMarker => None,
 179        }
 180    }
 181}
 182
 183impl Output {
 184    fn render_output_controls<V: OutputContent + 'static>(
 185        v: Entity<V>,
 186        workspace: WeakEntity<Workspace>,
 187        window: &mut Window,
 188        cx: &mut Context<ExecutionView>,
 189    ) -> Option<AnyElement> {
 190        if !v.has_clipboard_content(window, cx) && !v.has_buffer_content(window, cx) {
 191            return None;
 192        }
 193
 194        Some(
 195            h_flex()
 196                .pl_1()
 197                .when(v.has_clipboard_content(window, cx), |el| {
 198                    let v = v.clone();
 199                    el.child(
 200                        IconButton::new(ElementId::Name("copy-output".into()), IconName::Copy)
 201                            .style(ButtonStyle::Transparent)
 202                            .tooltip(Tooltip::text("Copy Output"))
 203                            .on_click(move |_, window, cx| {
 204                                let clipboard_content = v.clipboard_content(window, cx);
 205
 206                                if let Some(clipboard_content) = clipboard_content.as_ref() {
 207                                    cx.write_to_clipboard(clipboard_content.clone());
 208                                }
 209                            }),
 210                    )
 211                })
 212                .when(v.has_buffer_content(window, cx), |el| {
 213                    let v = v.clone();
 214                    el.child(
 215                        IconButton::new(
 216                            ElementId::Name("open-in-buffer".into()),
 217                            IconName::FileTextOutlined,
 218                        )
 219                        .style(ButtonStyle::Transparent)
 220                        .tooltip(Tooltip::text("Open in Buffer"))
 221                        .on_click({
 222                            let workspace = workspace.clone();
 223                            move |_, window, cx| {
 224                                let buffer_content =
 225                                    v.update(cx, |item, cx| item.buffer_content(window, cx));
 226
 227                                if let Some(buffer_content) = buffer_content.as_ref() {
 228                                    let buffer = buffer_content.clone();
 229                                    let editor = Box::new(cx.new(|cx| {
 230                                        let multibuffer = cx.new(|cx| {
 231                                            let mut multi_buffer =
 232                                                MultiBuffer::singleton(buffer.clone(), cx);
 233
 234                                            multi_buffer.set_title("REPL Output".to_string(), cx);
 235                                            multi_buffer
 236                                        });
 237
 238                                        Editor::for_multibuffer(multibuffer, None, window, cx)
 239                                    }));
 240                                    workspace
 241                                        .update(cx, |workspace, cx| {
 242                                            workspace.add_item_to_active_pane(
 243                                                editor, None, true, window, cx,
 244                                            );
 245                                        })
 246                                        .ok();
 247                                }
 248                            }
 249                        }),
 250                    )
 251                })
 252                .into_any_element(),
 253        )
 254    }
 255
 256    pub fn render(
 257        &self,
 258        workspace: WeakEntity<Workspace>,
 259        window: &mut Window,
 260        cx: &mut Context<ExecutionView>,
 261    ) -> impl IntoElement + use<> {
 262        let max_width = plain::max_width_for_columns(
 263            ReplSettings::get_global(cx).output_max_width_columns,
 264            window,
 265            cx,
 266        );
 267        let content = match self {
 268            Self::Plain { content, .. } => Some(content.clone().into_any_element()),
 269            Self::Markdown { content, .. } => Some(content.clone().into_any_element()),
 270            Self::Stream { content, .. } => Some(content.clone().into_any_element()),
 271            Self::Image { content, .. } => Some(content.clone().into_any_element()),
 272            Self::Message(message) => Some(div().child(message.clone()).into_any_element()),
 273            Self::Table { content, .. } => Some(content.clone().into_any_element()),
 274            Self::Json { content, .. } => Some(content.clone().into_any_element()),
 275            Self::ErrorOutput(error_view) => error_view.render(window, cx),
 276            Self::ClearOutputWaitMarker => None,
 277        };
 278
 279        let needs_horizontal_scroll = matches!(self, Self::Table { .. } | Self::Image { .. });
 280
 281        h_flex()
 282            .id("output-content")
 283            .w_full()
 284            .when_some(max_width, |this, max_w| this.max_w(max_w))
 285            .overflow_x_scroll()
 286            .items_start()
 287            .child(
 288                div()
 289                    .when(!needs_horizontal_scroll, |el| {
 290                        el.flex_1().w_full().overflow_x_hidden()
 291                    })
 292                    .children(content),
 293            )
 294            .children(match self {
 295                Self::Plain { content, .. } => {
 296                    Self::render_output_controls(content.clone(), workspace, window, cx)
 297                }
 298                Self::Markdown { content, .. } => {
 299                    Self::render_output_controls(content.clone(), workspace, window, cx)
 300                }
 301                Self::Stream { content, .. } => {
 302                    Self::render_output_controls(content.clone(), workspace, window, cx)
 303                }
 304                Self::Image { content, .. } => {
 305                    Self::render_output_controls(content.clone(), workspace, window, cx)
 306                }
 307                Self::Json { content, .. } => {
 308                    Self::render_output_controls(content.clone(), workspace, window, cx)
 309                }
 310                Self::ErrorOutput(err) => Some(
 311                    h_flex()
 312                        .pl_1()
 313                        .child({
 314                            let ename = err.ename.clone();
 315                            let evalue = err.evalue.clone();
 316                            let traceback = err.traceback.clone();
 317                            let traceback_text = traceback.read(cx).full_text();
 318                            let full_error = format!("{}: {}\n{}", ename, evalue, traceback_text);
 319
 320                            CopyButton::new("copy-full-error", full_error)
 321                                .tooltip_label("Copy Full Error")
 322                        })
 323                        .child(
 324                            IconButton::new(
 325                                ElementId::Name("open-full-error-in-buffer-traceback".into()),
 326                                IconName::FileTextOutlined,
 327                            )
 328                            .style(ButtonStyle::Transparent)
 329                            .tooltip(Tooltip::text("Open Full Error in Buffer"))
 330                            .on_click({
 331                                let ename = err.ename.clone();
 332                                let evalue = err.evalue.clone();
 333                                let traceback = err.traceback.clone();
 334                                move |_, window, cx| {
 335                                    if let Some(workspace) = workspace.upgrade() {
 336                                        let traceback_text = traceback.read(cx).full_text();
 337                                        let full_error =
 338                                            format!("{}: {}\n{}", ename, evalue, traceback_text);
 339                                        let buffer = cx.new(|cx| {
 340                                            let mut buffer = Buffer::local(full_error, cx)
 341                                                .with_language(language::PLAIN_TEXT.clone(), cx);
 342                                            buffer
 343                                                .set_capability(language::Capability::ReadOnly, cx);
 344                                            buffer
 345                                        });
 346                                        let editor = Box::new(cx.new(|cx| {
 347                                            let multibuffer = cx.new(|cx| {
 348                                                let mut multi_buffer =
 349                                                    MultiBuffer::singleton(buffer.clone(), cx);
 350                                                multi_buffer
 351                                                    .set_title("Full Error".to_string(), cx);
 352                                                multi_buffer
 353                                            });
 354                                            Editor::for_multibuffer(multibuffer, None, window, cx)
 355                                        }));
 356                                        workspace.update(cx, |workspace, cx| {
 357                                            workspace.add_item_to_active_pane(
 358                                                editor, None, true, window, cx,
 359                                            );
 360                                        });
 361                                    }
 362                                }
 363                            }),
 364                        )
 365                        .into_any_element(),
 366                ),
 367                Self::Message(_) => None,
 368                Self::Table { content, .. } => {
 369                    Self::render_output_controls(content.clone(), workspace, window, cx)
 370                }
 371                Self::ClearOutputWaitMarker => None,
 372            })
 373    }
 374
 375    pub fn display_id(&self) -> Option<String> {
 376        match self {
 377            Output::Plain { display_id, .. } => display_id.clone(),
 378            Output::Stream { .. } => None,
 379            Output::Image { display_id, .. } => display_id.clone(),
 380            Output::ErrorOutput(_) => None,
 381            Output::Message(_) => None,
 382            Output::Table { display_id, .. } => display_id.clone(),
 383            Output::Markdown { display_id, .. } => display_id.clone(),
 384            Output::Json { display_id, .. } => display_id.clone(),
 385            Output::ClearOutputWaitMarker => None,
 386        }
 387    }
 388
 389    pub fn new(
 390        data: &MimeBundle,
 391        display_id: Option<String>,
 392        window: &mut Window,
 393        cx: &mut App,
 394    ) -> Self {
 395        match data.richest(rank_mime_type) {
 396            Some(MimeType::Json(json_value)) => match JsonView::from_value(json_value.clone()) {
 397                Ok(json_view) => Output::Json {
 398                    content: cx.new(|_| json_view),
 399                    display_id,
 400                },
 401                Err(_) => Output::Message("Failed to parse JSON".to_string()),
 402            },
 403            Some(MimeType::Plain(text)) => Output::Plain {
 404                content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
 405                display_id,
 406            },
 407            Some(MimeType::Markdown(text)) => {
 408                let content = cx.new(|cx| MarkdownView::from(text.clone(), cx));
 409                Output::Markdown {
 410                    content,
 411                    display_id,
 412                }
 413            }
 414            Some(MimeType::Png(data)) | Some(MimeType::Jpeg(data)) => match ImageView::from(data) {
 415                Ok(view) => Output::Image {
 416                    content: cx.new(|_| view),
 417                    display_id,
 418                },
 419                Err(error) => Output::Message(format!("Failed to load image: {}", error)),
 420            },
 421            Some(MimeType::DataTable(data)) => Output::Table {
 422                content: cx.new(|cx| TableView::new(data, window, cx)),
 423                display_id,
 424            },
 425            Some(MimeType::Html(html_content)) => match html::html_to_markdown(html_content) {
 426                Ok(markdown_text) => {
 427                    let content = cx.new(|cx| MarkdownView::from(markdown_text, cx));
 428                    Output::Markdown {
 429                        content,
 430                        display_id,
 431                    }
 432                }
 433                Err(_) => Output::Plain {
 434                    content: cx.new(|cx| TerminalOutput::from(html_content, window, cx)),
 435                    display_id,
 436                },
 437            },
 438            // Any other media types are not supported
 439            _ => Output::Message("Unsupported media type".to_string()),
 440        }
 441    }
 442}
 443
 444#[derive(Default, Clone, Debug)]
 445pub enum ExecutionStatus {
 446    #[default]
 447    Unknown,
 448    ConnectingToKernel,
 449    Queued,
 450    Executing,
 451    Finished,
 452    ShuttingDown,
 453    Shutdown,
 454    KernelErrored(String),
 455    Restarting,
 456}
 457
 458pub struct ExecutionViewFinishedEmpty;
 459pub struct ExecutionViewFinishedSmall(pub String);
 460
 461pub struct InputReplyEvent {
 462    pub value: String,
 463    pub parent_message: JupyterMessage,
 464}
 465
 466struct PendingInput {
 467    prompt: String,
 468    password: bool,
 469    editor: Entity<Editor>,
 470    parent_message: JupyterMessage,
 471}
 472
 473/// An ExecutionView shows the outputs of an execution.
 474/// It can hold zero or more outputs, which the user
 475/// sees as "the output" for a single execution.
 476pub struct ExecutionView {
 477    #[allow(unused)]
 478    workspace: WeakEntity<Workspace>,
 479    pub outputs: Vec<Output>,
 480    pub status: ExecutionStatus,
 481    pending_input: Option<PendingInput>,
 482}
 483
 484impl EventEmitter<ExecutionViewFinishedEmpty> for ExecutionView {}
 485impl EventEmitter<ExecutionViewFinishedSmall> for ExecutionView {}
 486impl EventEmitter<InputReplyEvent> for ExecutionView {}
 487
 488impl ExecutionView {
 489    pub fn new(
 490        status: ExecutionStatus,
 491        workspace: WeakEntity<Workspace>,
 492        _cx: &mut Context<Self>,
 493    ) -> Self {
 494        Self {
 495            workspace,
 496            outputs: Default::default(),
 497            status,
 498            pending_input: None,
 499        }
 500    }
 501
 502    fn submit_input(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
 503        if let Some(pending_input) = self.pending_input.take() {
 504            let value = pending_input.editor.read(cx).text(cx);
 505
 506            let display_text = if pending_input.password {
 507                format!("{}{}", pending_input.prompt, "*".repeat(value.len()))
 508            } else {
 509                format!("{}{}", pending_input.prompt, value)
 510            };
 511            self.outputs.push(Output::Message(display_text));
 512
 513            cx.emit(InputReplyEvent {
 514                value,
 515                parent_message: pending_input.parent_message,
 516            });
 517            cx.notify();
 518        }
 519    }
 520
 521    /// Handle an InputRequest message, storing the full message for replying
 522    pub fn handle_input_request(
 523        &mut self,
 524        message: &JupyterMessage,
 525        window: &mut Window,
 526        cx: &mut Context<Self>,
 527    ) {
 528        if let JupyterMessageContent::InputRequest(input_request) = &message.content {
 529            let prompt = input_request.prompt.clone();
 530            let password = input_request.password;
 531
 532            let editor = cx.new(|cx| {
 533                let mut editor = Editor::single_line(window, cx);
 534                editor.set_placeholder_text("Type here and press Enter", window, cx);
 535                if password {
 536                    editor.set_masked(true, cx);
 537                }
 538                editor
 539            });
 540
 541            self.pending_input = Some(PendingInput {
 542                prompt,
 543                password,
 544                editor,
 545                parent_message: message.clone(),
 546            });
 547            cx.notify();
 548        }
 549    }
 550
 551    /// Accept a Jupyter message belonging to this execution
 552    pub fn push_message(
 553        &mut self,
 554        message: &JupyterMessageContent,
 555        window: &mut Window,
 556        cx: &mut Context<Self>,
 557    ) {
 558        let output: Output = match message {
 559            JupyterMessageContent::ExecuteResult(result) => Output::new(
 560                &result.data,
 561                result.transient.as_ref().and_then(|t| t.display_id.clone()),
 562                window,
 563                cx,
 564            ),
 565            JupyterMessageContent::DisplayData(result) => Output::new(
 566                &result.data,
 567                result.transient.as_ref().and_then(|t| t.display_id.clone()),
 568                window,
 569                cx,
 570            ),
 571            JupyterMessageContent::StreamContent(result) => {
 572                // Previous stream data will combine together, handling colors, carriage returns, etc
 573                if let Some(new_terminal) = self.apply_terminal_text(&result.text, window, cx) {
 574                    new_terminal
 575                } else {
 576                    return;
 577                }
 578            }
 579            JupyterMessageContent::ErrorOutput(result) => {
 580                let terminal =
 581                    cx.new(|cx| TerminalOutput::from(&result.traceback.join("\n"), window, cx));
 582
 583                Output::ErrorOutput(ErrorView {
 584                    ename: result.ename.clone(),
 585                    evalue: result.evalue.clone(),
 586                    traceback: terminal,
 587                })
 588            }
 589            JupyterMessageContent::ExecuteReply(reply) => {
 590                for payload in reply.payload.iter() {
 591                    if let runtimelib::Payload::Page { data, .. } = payload {
 592                        let output = Output::new(data, None, window, cx);
 593                        self.outputs.push(output);
 594                    }
 595                }
 596                cx.notify();
 597                return;
 598            }
 599            JupyterMessageContent::ClearOutput(options) => {
 600                if !options.wait {
 601                    self.outputs.clear();
 602                    cx.notify();
 603                    return;
 604                }
 605
 606                // Create a marker to clear the output after we get in a new output
 607                Output::ClearOutputWaitMarker
 608            }
 609            JupyterMessageContent::InputRequest(_) => {
 610                // InputRequest is handled by handle_input_request which needs the full message
 611                return;
 612            }
 613            JupyterMessageContent::Status(status) => {
 614                match status.execution_state {
 615                    ExecutionState::Busy => {
 616                        self.status = ExecutionStatus::Executing;
 617                    }
 618                    ExecutionState::Idle => {
 619                        self.status = ExecutionStatus::Finished;
 620                        self.pending_input = None;
 621                        if self.outputs.is_empty() {
 622                            cx.emit(ExecutionViewFinishedEmpty);
 623                        } else if ReplSettings::get_global(cx).inline_output {
 624                            if let Some(small_text) = self.get_small_inline_output(cx) {
 625                                cx.emit(ExecutionViewFinishedSmall(small_text));
 626                            }
 627                        }
 628                    }
 629                    ExecutionState::Unknown => self.status = ExecutionStatus::Unknown,
 630                    ExecutionState::Starting => self.status = ExecutionStatus::ConnectingToKernel,
 631                    ExecutionState::Restarting => self.status = ExecutionStatus::Restarting,
 632                    ExecutionState::Terminating => self.status = ExecutionStatus::ShuttingDown,
 633                    ExecutionState::AutoRestarting => self.status = ExecutionStatus::Restarting,
 634                    ExecutionState::Dead => self.status = ExecutionStatus::Shutdown,
 635                    ExecutionState::Other(_) => self.status = ExecutionStatus::Unknown,
 636                }
 637                cx.notify();
 638                return;
 639            }
 640            _msg => {
 641                return;
 642            }
 643        };
 644
 645        // Check for a clear output marker as the previous output, so we can clear it out
 646        if let Some(output) = self.outputs.last()
 647            && let Output::ClearOutputWaitMarker = output
 648        {
 649            self.outputs.clear();
 650        }
 651
 652        self.outputs.push(output);
 653
 654        cx.notify();
 655    }
 656
 657    pub fn update_display_data(
 658        &mut self,
 659        data: &MimeBundle,
 660        display_id: &str,
 661        window: &mut Window,
 662        cx: &mut Context<Self>,
 663    ) {
 664        let mut any = false;
 665
 666        self.outputs.iter_mut().for_each(|output| {
 667            if let Some(other_display_id) = output.display_id().as_ref()
 668                && other_display_id == display_id
 669            {
 670                *output = Output::new(data, Some(display_id.to_owned()), window, cx);
 671                any = true;
 672            }
 673        });
 674
 675        if any {
 676            cx.notify();
 677        }
 678    }
 679
 680    /// Check if the output is a single small plain text that can be shown inline.
 681    /// Returns the text if it's suitable for inline display (single line, short enough).
 682    fn get_small_inline_output(&self, cx: &App) -> Option<String> {
 683        // Only consider single outputs
 684        if self.outputs.len() != 1 {
 685            return None;
 686        }
 687
 688        let output = self.outputs.first()?;
 689
 690        // Only Plain outputs can be inlined
 691        let content = match output {
 692            Output::Plain { content, .. } => content,
 693            _ => return None,
 694        };
 695
 696        let text = content.read(cx).full_text();
 697        let trimmed = text.trim();
 698
 699        let max_length = ReplSettings::get_global(cx).inline_output_max_length;
 700
 701        // Must be a single line and within the configured max length
 702        if trimmed.contains('\n') || trimmed.len() > max_length {
 703            return None;
 704        }
 705
 706        Some(trimmed.to_string())
 707    }
 708
 709    fn apply_terminal_text(
 710        &mut self,
 711        text: &str,
 712        window: &mut Window,
 713        cx: &mut Context<Self>,
 714    ) -> Option<Output> {
 715        if let Some(last_output) = self.outputs.last_mut()
 716            && let Output::Stream {
 717                content: last_stream,
 718            } = last_output
 719        {
 720            // Don't need to add a new output, we already have a terminal output
 721            // and can just update the most recent terminal output
 722            last_stream.update(cx, |last_stream, cx| {
 723                last_stream.append_text(text, cx);
 724                cx.notify();
 725            });
 726            return None;
 727        }
 728
 729        Some(Output::Stream {
 730            content: cx.new(|cx| TerminalOutput::from(text, window, cx)),
 731        })
 732    }
 733}
 734
 735impl ExecutionView {
 736    #[cfg(test)]
 737    fn output_as_stream_text(&self, cx: &App) -> Option<String> {
 738        self.outputs.iter().find_map(|output| {
 739            if let Output::Stream { content } = output {
 740                Some(content.read(cx).full_text())
 741            } else {
 742                None
 743            }
 744        })
 745    }
 746}
 747
 748impl Render for ExecutionView {
 749    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 750        let status = match &self.status {
 751            ExecutionStatus::ConnectingToKernel => Label::new("Connecting to kernel...")
 752                .color(Color::Muted)
 753                .into_any_element(),
 754            ExecutionStatus::Executing => h_flex()
 755                .gap_2()
 756                .child(
 757                    Icon::new(IconName::ArrowCircle)
 758                        .size(IconSize::Small)
 759                        .color(Color::Muted)
 760                        .with_rotate_animation(3),
 761                )
 762                .child(Label::new("Executing...").color(Color::Muted))
 763                .into_any_element(),
 764            ExecutionStatus::Finished => Icon::new(IconName::Check)
 765                .size(IconSize::Small)
 766                .into_any_element(),
 767            ExecutionStatus::Unknown => Label::new("Unknown status")
 768                .color(Color::Muted)
 769                .into_any_element(),
 770            ExecutionStatus::ShuttingDown => Label::new("Kernel shutting down...")
 771                .color(Color::Muted)
 772                .into_any_element(),
 773            ExecutionStatus::Restarting => Label::new("Kernel restarting...")
 774                .color(Color::Muted)
 775                .into_any_element(),
 776            ExecutionStatus::Shutdown => Label::new("Kernel shutdown")
 777                .color(Color::Muted)
 778                .into_any_element(),
 779            ExecutionStatus::Queued => Label::new("Queued...")
 780                .color(Color::Muted)
 781                .into_any_element(),
 782            ExecutionStatus::KernelErrored(error) => Label::new(format!("Kernel error: {}", error))
 783                .color(Color::Error)
 784                .into_any_element(),
 785        };
 786
 787        let pending_input_element = self.pending_input.as_ref().map(|pending_input| {
 788            let prompt_label = if pending_input.prompt.is_empty() {
 789                "Input:".to_string()
 790            } else {
 791                pending_input.prompt.clone()
 792            };
 793
 794            div()
 795                .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| {
 796                    this.submit_input(window, cx);
 797                }))
 798                .w_full()
 799                .child(
 800                    v_flex()
 801                        .gap_1()
 802                        .child(Label::new(prompt_label).color(Color::Muted))
 803                        .child(
 804                            div()
 805                                .px_2()
 806                                .py_1()
 807                                .border_1()
 808                                .border_color(cx.theme().colors().border)
 809                                .rounded_md()
 810                                .child(pending_input.editor.clone()),
 811                        ),
 812                )
 813        });
 814
 815        if self.outputs.is_empty() && pending_input_element.is_none() {
 816            return v_flex()
 817                .min_h(window.line_height())
 818                .justify_center()
 819                .child(status)
 820                .into_any_element();
 821        }
 822
 823        div()
 824            .w_full()
 825            .children(
 826                self.outputs
 827                    .iter()
 828                    .map(|output| output.render(self.workspace.clone(), window, cx)),
 829            )
 830            .children(pending_input_element)
 831            .children(match self.status {
 832                ExecutionStatus::Executing => vec![status],
 833                ExecutionStatus::Queued => vec![status],
 834                _ => vec![],
 835            })
 836            .into_any_element()
 837    }
 838}
 839
 840#[cfg(test)]
 841mod tests {
 842    use super::*;
 843    use gpui::TestAppContext;
 844    use runtimelib::{
 845        ClearOutput, ErrorOutput, ExecutionState, InputRequest, JupyterMessage,
 846        JupyterMessageContent, MimeType, Status, Stdio, StreamContent,
 847    };
 848    use settings::SettingsStore;
 849    use std::path::Path;
 850    use std::sync::Arc;
 851
 852    #[test]
 853    fn test_rank_mime_type_ordering() {
 854        let data_table = MimeType::DataTable(Box::default());
 855        let html = MimeType::Html(String::new());
 856        let json = MimeType::Json(serde_json::json!({}));
 857        let png = MimeType::Png(String::new());
 858        let jpeg = MimeType::Jpeg(String::new());
 859        let markdown = MimeType::Markdown(String::new());
 860        let plain = MimeType::Plain(String::new());
 861
 862        assert_eq!(rank_mime_type(&data_table), 7);
 863        assert_eq!(rank_mime_type(&html), 6);
 864        assert_eq!(rank_mime_type(&json), 5);
 865        assert_eq!(rank_mime_type(&png), 4);
 866        assert_eq!(rank_mime_type(&jpeg), 3);
 867        assert_eq!(rank_mime_type(&markdown), 2);
 868        assert_eq!(rank_mime_type(&plain), 1);
 869
 870        assert!(rank_mime_type(&data_table) > rank_mime_type(&html));
 871        assert!(rank_mime_type(&html) > rank_mime_type(&json));
 872        assert!(rank_mime_type(&json) > rank_mime_type(&png));
 873        assert!(rank_mime_type(&png) > rank_mime_type(&jpeg));
 874        assert!(rank_mime_type(&jpeg) > rank_mime_type(&markdown));
 875        assert!(rank_mime_type(&markdown) > rank_mime_type(&plain));
 876    }
 877
 878    #[test]
 879    fn test_rank_mime_type_unsupported_returns_zero() {
 880        let svg = MimeType::Svg(String::new());
 881        let latex = MimeType::Latex(String::new());
 882
 883        assert_eq!(rank_mime_type(&svg), 0);
 884        assert_eq!(rank_mime_type(&latex), 0);
 885    }
 886
 887    async fn init_test(
 888        cx: &mut TestAppContext,
 889    ) -> (gpui::VisualTestContext, WeakEntity<workspace::Workspace>) {
 890        cx.update(|cx| {
 891            let settings_store = SettingsStore::test(cx);
 892            cx.set_global(settings_store);
 893            theme::init(theme::LoadThemes::JustBase, cx);
 894        });
 895        let fs = project::FakeFs::new(cx.background_executor.clone());
 896        let project = project::Project::test(fs, [] as [&Path; 0], cx).await;
 897        let window =
 898            cx.add_window(|window, cx| workspace::MultiWorkspace::test_new(project, window, cx));
 899        let workspace = window
 900            .read_with(cx, |mw, _| mw.workspace().clone())
 901            .unwrap();
 902        let weak_workspace = workspace.downgrade();
 903        let visual_cx = gpui::VisualTestContext::from_window(window.into(), cx);
 904        (visual_cx, weak_workspace)
 905    }
 906
 907    fn create_execution_view(
 908        cx: &mut gpui::VisualTestContext,
 909        weak_workspace: WeakEntity<workspace::Workspace>,
 910    ) -> Entity<ExecutionView> {
 911        cx.update(|_window, cx| {
 912            cx.new(|cx| ExecutionView::new(ExecutionStatus::Queued, weak_workspace, cx))
 913        })
 914    }
 915
 916    #[gpui::test]
 917    async fn test_push_message_stream_content(cx: &mut TestAppContext) {
 918        let (mut cx, workspace) = init_test(cx).await;
 919        let execution_view = create_execution_view(&mut cx, workspace);
 920
 921        cx.update(|window, cx| {
 922            execution_view.update(cx, |view, cx| {
 923                let message = JupyterMessageContent::StreamContent(StreamContent {
 924                    name: Stdio::Stdout,
 925                    text: "hello world\n".to_string(),
 926                });
 927                view.push_message(&message, window, cx);
 928            });
 929        });
 930
 931        cx.update(|_, cx| {
 932            let view = execution_view.read(cx);
 933            assert_eq!(view.outputs.len(), 1);
 934            assert!(matches!(view.outputs[0], Output::Stream { .. }));
 935            let text = view.output_as_stream_text(cx);
 936            assert!(text.is_some());
 937            assert!(text.as_ref().is_some_and(|t| t.contains("hello world")));
 938        });
 939    }
 940
 941    #[gpui::test]
 942    async fn test_push_message_stream_appends(cx: &mut TestAppContext) {
 943        let (mut cx, workspace) = init_test(cx).await;
 944        let execution_view = create_execution_view(&mut cx, workspace);
 945
 946        cx.update(|window, cx| {
 947            execution_view.update(cx, |view, cx| {
 948                let message1 = JupyterMessageContent::StreamContent(StreamContent {
 949                    name: Stdio::Stdout,
 950                    text: "first ".to_string(),
 951                });
 952                let message2 = JupyterMessageContent::StreamContent(StreamContent {
 953                    name: Stdio::Stdout,
 954                    text: "second".to_string(),
 955                });
 956                view.push_message(&message1, window, cx);
 957                view.push_message(&message2, window, cx);
 958            });
 959        });
 960
 961        cx.update(|_, cx| {
 962            let view = execution_view.read(cx);
 963            assert_eq!(
 964                view.outputs.len(),
 965                1,
 966                "consecutive streams should merge into one output"
 967            );
 968            let text = view.output_as_stream_text(cx);
 969            assert!(text.as_ref().is_some_and(|t| t.contains("first ")));
 970            assert!(text.as_ref().is_some_and(|t| t.contains("second")));
 971        });
 972    }
 973
 974    #[gpui::test]
 975    async fn test_push_message_error_output(cx: &mut TestAppContext) {
 976        let (mut cx, workspace) = init_test(cx).await;
 977        let execution_view = create_execution_view(&mut cx, workspace);
 978
 979        cx.update(|window, cx| {
 980            execution_view.update(cx, |view, cx| {
 981                let message = JupyterMessageContent::ErrorOutput(ErrorOutput {
 982                    ename: "NameError".to_string(),
 983                    evalue: "name 'x' is not defined".to_string(),
 984                    traceback: vec![
 985                        "Traceback (most recent call last):".to_string(),
 986                        "NameError: name 'x' is not defined".to_string(),
 987                    ],
 988                });
 989                view.push_message(&message, window, cx);
 990            });
 991        });
 992
 993        cx.update(|_, cx| {
 994            let view = execution_view.read(cx);
 995            assert_eq!(view.outputs.len(), 1);
 996            match &view.outputs[0] {
 997                Output::ErrorOutput(error_view) => {
 998                    assert_eq!(error_view.ename, "NameError");
 999                    assert_eq!(error_view.evalue, "name 'x' is not defined");
1000                }
1001                other => panic!(
1002                    "expected ErrorOutput, got {:?}",
1003                    std::mem::discriminant(other)
1004                ),
1005            }
1006        });
1007    }
1008
1009    #[gpui::test]
1010    async fn test_push_message_clear_output_immediate(cx: &mut TestAppContext) {
1011        let (mut cx, workspace) = init_test(cx).await;
1012        let execution_view = create_execution_view(&mut cx, workspace);
1013
1014        cx.update(|window, cx| {
1015            execution_view.update(cx, |view, cx| {
1016                let stream = JupyterMessageContent::StreamContent(StreamContent {
1017                    name: Stdio::Stdout,
1018                    text: "some output\n".to_string(),
1019                });
1020                view.push_message(&stream, window, cx);
1021                assert_eq!(view.outputs.len(), 1);
1022
1023                let clear = JupyterMessageContent::ClearOutput(ClearOutput { wait: false });
1024                view.push_message(&clear, window, cx);
1025                assert_eq!(
1026                    view.outputs.len(),
1027                    0,
1028                    "immediate clear should remove all outputs"
1029                );
1030            });
1031        });
1032    }
1033
1034    #[gpui::test]
1035    async fn test_push_message_clear_output_deferred(cx: &mut TestAppContext) {
1036        let (mut cx, workspace) = init_test(cx).await;
1037        let execution_view = create_execution_view(&mut cx, workspace);
1038
1039        cx.update(|window, cx| {
1040            execution_view.update(cx, |view, cx| {
1041                let stream = JupyterMessageContent::StreamContent(StreamContent {
1042                    name: Stdio::Stdout,
1043                    text: "old output\n".to_string(),
1044                });
1045                view.push_message(&stream, window, cx);
1046                assert_eq!(view.outputs.len(), 1);
1047
1048                let clear = JupyterMessageContent::ClearOutput(ClearOutput { wait: true });
1049                view.push_message(&clear, window, cx);
1050                assert_eq!(view.outputs.len(), 2, "deferred clear adds a wait marker");
1051                assert!(matches!(view.outputs[1], Output::ClearOutputWaitMarker));
1052
1053                let new_stream = JupyterMessageContent::StreamContent(StreamContent {
1054                    name: Stdio::Stdout,
1055                    text: "new output\n".to_string(),
1056                });
1057                view.push_message(&new_stream, window, cx);
1058                assert_eq!(
1059                    view.outputs.len(),
1060                    1,
1061                    "next output after wait marker should clear previous outputs"
1062                );
1063            });
1064        });
1065    }
1066
1067    #[gpui::test]
1068    async fn test_push_message_status_transitions(cx: &mut TestAppContext) {
1069        let (mut cx, workspace) = init_test(cx).await;
1070        let execution_view = create_execution_view(&mut cx, workspace);
1071
1072        cx.update(|window, cx| {
1073            execution_view.update(cx, |view, cx| {
1074                let busy = JupyterMessageContent::Status(Status {
1075                    execution_state: ExecutionState::Busy,
1076                });
1077                view.push_message(&busy, window, cx);
1078                assert!(matches!(view.status, ExecutionStatus::Executing));
1079
1080                let idle = JupyterMessageContent::Status(Status {
1081                    execution_state: ExecutionState::Idle,
1082                });
1083                view.push_message(&idle, window, cx);
1084                assert!(matches!(view.status, ExecutionStatus::Finished));
1085
1086                let starting = JupyterMessageContent::Status(Status {
1087                    execution_state: ExecutionState::Starting,
1088                });
1089                view.push_message(&starting, window, cx);
1090                assert!(matches!(view.status, ExecutionStatus::ConnectingToKernel));
1091
1092                let dead = JupyterMessageContent::Status(Status {
1093                    execution_state: ExecutionState::Dead,
1094                });
1095                view.push_message(&dead, window, cx);
1096                assert!(matches!(view.status, ExecutionStatus::Shutdown));
1097
1098                let restarting = JupyterMessageContent::Status(Status {
1099                    execution_state: ExecutionState::Restarting,
1100                });
1101                view.push_message(&restarting, window, cx);
1102                assert!(matches!(view.status, ExecutionStatus::Restarting));
1103
1104                let terminating = JupyterMessageContent::Status(Status {
1105                    execution_state: ExecutionState::Terminating,
1106                });
1107                view.push_message(&terminating, window, cx);
1108                assert!(matches!(view.status, ExecutionStatus::ShuttingDown));
1109            });
1110        });
1111    }
1112
1113    #[gpui::test]
1114    async fn test_push_message_status_idle_emits_finished_empty(cx: &mut TestAppContext) {
1115        let (mut cx, workspace) = init_test(cx).await;
1116        let execution_view = create_execution_view(&mut cx, workspace);
1117
1118        let emitted = Arc::new(std::sync::atomic::AtomicBool::new(false));
1119        let emitted_clone = emitted.clone();
1120
1121        cx.update(|_, cx| {
1122            cx.subscribe(
1123                &execution_view,
1124                move |_, _event: &ExecutionViewFinishedEmpty, _cx| {
1125                    emitted_clone.store(true, std::sync::atomic::Ordering::SeqCst);
1126                },
1127            )
1128            .detach();
1129        });
1130
1131        cx.update(|window, cx| {
1132            execution_view.update(cx, |view, cx| {
1133                assert!(view.outputs.is_empty());
1134                let idle = JupyterMessageContent::Status(Status {
1135                    execution_state: ExecutionState::Idle,
1136                });
1137                view.push_message(&idle, window, cx);
1138            });
1139        });
1140
1141        assert!(
1142            emitted.load(std::sync::atomic::Ordering::SeqCst),
1143            "should emit ExecutionViewFinishedEmpty when idle with no outputs"
1144        );
1145    }
1146
1147    #[gpui::test]
1148    async fn test_handle_input_request_creates_pending_input(cx: &mut TestAppContext) {
1149        let (mut cx, workspace) = init_test(cx).await;
1150        let execution_view = create_execution_view(&mut cx, workspace);
1151
1152        cx.update(|window, cx| {
1153            execution_view.update(cx, |view, cx| {
1154                assert!(view.pending_input.is_none());
1155
1156                let message = JupyterMessage::new(
1157                    InputRequest {
1158                        prompt: "Enter name: ".to_string(),
1159                        password: false,
1160                    },
1161                    None,
1162                );
1163                view.handle_input_request(&message, window, cx);
1164            });
1165        });
1166
1167        cx.update(|_, cx| {
1168            let view = execution_view.read(cx);
1169            assert!(view.pending_input.is_some());
1170            let pending = view.pending_input.as_ref().unwrap();
1171            assert_eq!(pending.prompt, "Enter name: ");
1172            assert!(!pending.password);
1173        });
1174    }
1175
1176    #[gpui::test]
1177    async fn test_handle_input_request_with_password(cx: &mut TestAppContext) {
1178        let (mut cx, workspace) = init_test(cx).await;
1179        let execution_view = create_execution_view(&mut cx, workspace);
1180
1181        cx.update(|window, cx| {
1182            execution_view.update(cx, |view, cx| {
1183                let message = JupyterMessage::new(
1184                    InputRequest {
1185                        prompt: "Password: ".to_string(),
1186                        password: true,
1187                    },
1188                    None,
1189                );
1190                view.handle_input_request(&message, window, cx);
1191            });
1192        });
1193
1194        cx.update(|_, cx| {
1195            let view = execution_view.read(cx);
1196            assert!(view.pending_input.is_some());
1197            let pending = view.pending_input.as_ref().unwrap();
1198            assert_eq!(pending.prompt, "Password: ");
1199            assert!(pending.password);
1200        });
1201    }
1202
1203    #[gpui::test]
1204    async fn test_submit_input_emits_reply_event(cx: &mut TestAppContext) {
1205        let (mut cx, workspace) = init_test(cx).await;
1206        let execution_view = create_execution_view(&mut cx, workspace);
1207
1208        let received_value = Arc::new(std::sync::Mutex::new(None::<String>));
1209        let received_clone = received_value.clone();
1210
1211        cx.update(|_, cx| {
1212            cx.subscribe(&execution_view, move |_, event: &InputReplyEvent, _cx| {
1213                *received_clone.lock().unwrap() = Some(event.value.clone());
1214            })
1215            .detach();
1216        });
1217
1218        cx.update(|window, cx| {
1219            execution_view.update(cx, |view, cx| {
1220                let message = JupyterMessage::new(
1221                    InputRequest {
1222                        prompt: "Name: ".to_string(),
1223                        password: false,
1224                    },
1225                    None,
1226                );
1227                view.handle_input_request(&message, window, cx);
1228
1229                // Type into the editor
1230                if let Some(ref pending) = view.pending_input {
1231                    pending.editor.update(cx, |editor, cx| {
1232                        editor.set_text("test_user", window, cx);
1233                    });
1234                }
1235
1236                view.submit_input(window, cx);
1237            });
1238        });
1239
1240        let value = received_value.lock().unwrap().clone();
1241        assert_eq!(value, Some("test_user".to_string()));
1242
1243        cx.update(|_, cx| {
1244            let view = execution_view.read(cx);
1245            assert!(
1246                view.pending_input.is_none(),
1247                "pending_input should be cleared after submit"
1248            );
1249        });
1250    }
1251
1252    #[gpui::test]
1253    async fn test_status_idle_clears_pending_input(cx: &mut TestAppContext) {
1254        let (mut cx, workspace) = init_test(cx).await;
1255        let execution_view = create_execution_view(&mut cx, workspace);
1256
1257        cx.update(|window, cx| {
1258            execution_view.update(cx, |view, cx| {
1259                let message = JupyterMessage::new(
1260                    InputRequest {
1261                        prompt: "Input: ".to_string(),
1262                        password: false,
1263                    },
1264                    None,
1265                );
1266                view.handle_input_request(&message, window, cx);
1267                assert!(view.pending_input.is_some());
1268
1269                // Simulate kernel going idle (e.g., execution interrupted)
1270                let idle = JupyterMessageContent::Status(Status {
1271                    execution_state: ExecutionState::Idle,
1272                });
1273                view.push_message(&idle, window, cx);
1274            });
1275        });
1276
1277        cx.update(|_, cx| {
1278            let view = execution_view.read(cx);
1279            assert!(
1280                view.pending_input.is_none(),
1281                "pending_input should be cleared when kernel goes idle"
1282            );
1283        });
1284    }
1285}