From eae299e8c9d1471eca2c76f1e8e19765fde556a2 2022-12-09 18:17:05 From: Jason Grout Date: 2022-12-09 18:17:05 Subject: [PATCH] Move formatting of inspect reply html into the format_mime function Also simplify the formatting of the inspect reply text/plain logic --- diff --git a/IPython/core/oinspect.py b/IPython/core/oinspect.py index 13d6268..3a80f05 100644 --- a/IPython/core/oinspect.py +++ b/IPython/core/oinspect.py @@ -553,23 +553,23 @@ class Inspector(Colorable): def format_mime(self, bundle): """Format a mimebundle being created by _make_info_unformatted into a real mimebundle""" - # First, format the field names and values for the text/plain field - text_plain = bundle['text/plain'] - if isinstance(text_plain, (list, tuple)): - text = '' - heads, bodies = list(zip(*text_plain)) - _len = max(len(h) for h in heads) - - for head, body in zip(heads, bodies): + # Format text/plain mimetype + if isinstance(bundle['text/plain'], (list, tuple)): + # bundle['text/plain'] is a list of (head, formatted body) pairs + lines = [] + _len = max(len(h) for h,_ in bundle['text/plain']) + + for head, body in bundle['text/plain']: body = body.strip('\n') delim = '\n' if '\n' in body else ' ' - text += self.__head(head+':') + (_len - len(head))*' ' +delim + body +'\n' + lines.append(f"{self.__head(head+':')}{(_len - len(head))*' '}{delim}{body}") - bundle['text/plain'] = text + bundle['text/plain'] = '\n'.join(lines) - # Next format the text/html value by joining strings if it is a list of strings + # Format the text/html mimetype if isinstance(bundle['text/html'], (list, tuple)): - bundle['text/html'] = '\n'.join(bundle['text/html']) + # bundle['text/html'] is a list of (head, formatted body) pairs + bundle['text/html'] = '\n'.join((f'

{head}

\n{body}' for (head,body) in bundle['text/html'])) return bundle def _append_info_field(self, bundle, title:str, key:str, info, omit_sections, formatter): @@ -580,7 +580,7 @@ class Inspector(Colorable): if field is not None: formatted_field = self._mime_format(field, formatter) bundle['text/plain'].append((title, formatted_field['text/plain'])) - bundle['text/html'] += '

' + title + '

\n' + formatted_field['text/html'] + bundle['text/html'].append((title, formatted_field['text/html'])) def _make_info_unformatted(self, info, formatter, detail_level, omit_sections): """Assemble the mimebundle as unformatted lists of information"""