@@ -6,7 +6,7 @@ use anyhow::Result;
66use gpui:: { img, AnyElement , FontWeight , ImageData , Render , View } ;
77use runtimelib:: datatable:: TableSchema ;
88use runtimelib:: media:: datatable:: TabularDataResource ;
9- use runtimelib:: { ExecutionState , JupyterMessageContent , MimeType } ;
9+ use runtimelib:: { ExecutionState , JupyterMessageContent , MimeBundle , MimeType } ;
1010use serde_json:: Value ;
1111use ui:: { div, prelude:: * , v_flex, IntoElement , Styled , ViewContext } ;
1212
@@ -70,8 +70,8 @@ pub trait LineHeight: Sized {
7070
7171fn rank_mime_type ( mimetype : & MimeType ) -> usize {
7272 match mimetype {
73- // SVG Rendering is incomplete so we don't show it
7473 MimeType :: DataTable ( _) => 6 ,
74+ // SVG Rendering is incomplete so we don't show it
7575 // MimeType::Svg(_) => 5,
7676 MimeType :: Png ( _) => 4 ,
7777 MimeType :: Jpeg ( _) => 3 ,
@@ -319,6 +319,30 @@ pub fn extract_image_output(base64_encoded_data: &str) -> Result<OutputType> {
319319 } ) ) ;
320320}
321321
322+ impl From < & MimeBundle > for OutputType {
323+ fn from ( data : & MimeBundle ) -> Self {
324+ match data. richest ( rank_mime_type) {
325+ Some ( MimeType :: Plain ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
326+ Some ( MimeType :: Markdown ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
327+ Some ( MimeType :: Svg ( text) ) => match svg_to_vec ( text, 1.0 ) {
328+ Ok ( output) => output,
329+ Err ( error) => OutputType :: Message ( format ! ( "Failed to load image: {}" , error) ) ,
330+ } ,
331+ Some ( MimeType :: Png ( data) ) | Some ( MimeType :: Jpeg ( data) ) => {
332+ match extract_image_output ( & data) {
333+ Ok ( output) => output,
334+ Err ( error) => OutputType :: Message ( format ! ( "Failed to load image: {}" , error) ) ,
335+ }
336+ }
337+ Some ( MimeType :: DataTable ( data) ) => OutputType :: Table ( TableView {
338+ table : data. clone ( ) ,
339+ } ) ,
340+ // Any other media types are not supported
341+ _ => OutputType :: Message ( "Unsupported media type" . to_string ( ) ) ,
342+ }
343+ }
344+ }
345+
322346impl ExecutionView {
323347 pub fn new ( execution_id : ExecutionId , _cx : & mut ViewContext < Self > ) -> Self {
324348 Self {
@@ -330,57 +354,9 @@ impl ExecutionView {
330354
331355 /// Accept a Jupyter message belonging to this execution
332356 pub fn push_message ( & mut self , message : & JupyterMessageContent , cx : & mut ViewContext < Self > ) {
333- let output = match message {
334- JupyterMessageContent :: ExecuteResult ( result) => {
335- match result. data . richest ( rank_mime_type) {
336- Some ( MimeType :: Plain ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
337- Some ( MimeType :: Markdown ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
338- Some ( MimeType :: Svg ( text) ) => match svg_to_vec ( text, 1.0 ) {
339- Ok ( output) => output,
340- Err ( error) => {
341- OutputType :: Message ( format ! ( "Failed to load image: {}" , error) )
342- }
343- } ,
344- Some ( MimeType :: Png ( data) ) | Some ( MimeType :: Jpeg ( data) ) => {
345- match extract_image_output ( & data) {
346- Ok ( output) => output,
347- Err ( error) => {
348- OutputType :: Message ( format ! ( "Failed to load image: {}" , error) )
349- }
350- }
351- }
352- Some ( MimeType :: DataTable ( data) ) => OutputType :: Table ( TableView {
353- table : data. clone ( ) ,
354- } ) ,
355- // Any other media types are not supported
356- _ => return ,
357- }
358- }
359- JupyterMessageContent :: DisplayData ( result) => {
360- match result. data . richest ( rank_mime_type) {
361- Some ( MimeType :: Plain ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
362- Some ( MimeType :: Markdown ( text) ) => OutputType :: Plain ( TerminalOutput :: from ( text) ) ,
363- Some ( MimeType :: Svg ( text) ) => match svg_to_vec ( text, 1.0 ) {
364- Ok ( output) => output,
365- Err ( error) => {
366- OutputType :: Message ( format ! ( "Failed to load image: {}" , error) )
367- }
368- } ,
369- Some ( MimeType :: Png ( data) ) | Some ( MimeType :: Jpeg ( data) ) => {
370- match extract_image_output ( & data) {
371- Ok ( output) => output,
372- Err ( error) => {
373- OutputType :: Message ( format ! ( "Failed to load image: {}" , error) )
374- }
375- }
376- }
377- Some ( MimeType :: DataTable ( data) ) => OutputType :: Table ( TableView {
378- table : data. clone ( ) ,
379- } ) ,
380- // Any other media types are not supported
381- _ => return ,
382- }
383- }
357+ let output: OutputType = match message {
358+ JupyterMessageContent :: ExecuteResult ( result) => ( & result. data ) . into ( ) ,
359+ JupyterMessageContent :: DisplayData ( result) => ( & result. data ) . into ( ) ,
384360 JupyterMessageContent :: StreamContent ( result) => {
385361 // Previous stream data will combine together, handling colors, carriage returns, etc
386362 if let Some ( new_terminal) = self . apply_terminal_text ( & result. text ) {
0 commit comments