Skip to content

Commit

Permalink
actually fix info formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Dec 23, 2024
1 parent 0dfd0b0 commit cf2dbfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
48 changes: 10 additions & 38 deletions asdf/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
"DEFAULT_MAX_COLS",
"DEFAULT_SHOW_VALUES",
"render_tree",
"format_bold",
"format_faint",
"format_italic",
]


Expand Down Expand Up @@ -63,31 +60,6 @@ def render_tree(
return renderer.render(info)


def format_bold(value):
"""
Wrap the input value in the ANSI escape sequence for increased intensity.
"""
return _format_code(value, 1)


def format_faint(value):
"""
Wrap the input value in the ANSI escape sequence for decreased intensity.
"""
return _format_code(value, 2)


def format_italic(value):
"""
Wrap the input value in the ANSI escape sequence for italic.
"""
return _format_code(value, 3)


def _format_code(value, code):
return f"\x1B[{code}m{value}\x1B[0m"


class _TreeRenderer:
"""
Render a _NodeInfo tree with indent showing depth.
Expand Down Expand Up @@ -118,7 +90,7 @@ def format_italic(self, value):
return self._format_code(value, 3)

def _format_code(self, value, code):
if not self._isattry:
if not self._isatty:
return f"{value}"
return f"\x1B[{code}m{value}\x1B[0m"

Check warning on line 95 in asdf/_display.py

View check run for this annotation

Codecov / codecov/patch

asdf/_display.py#L95

Added line #L95 was not covered by tests

Expand All @@ -128,7 +100,7 @@ def render(self, info):
lines, elided = self._render(info, set(), True)

if elided:
lines.append(format_faint(format_italic("Some nodes not shown.")))
lines.append(self.format_faint(self.format_italic("Some nodes not shown.")))

return lines

Expand Down Expand Up @@ -246,7 +218,7 @@ def _render(self, info, active_depths, is_tail):
if num_visible_children > 0 and num_visible_children != len(info.children):
hidden_count = len(info.children) - num_visible_children
prefix = self._make_prefix(info.depth + 1, active_depths, True)
message = format_faint(format_italic(str(hidden_count) + " not shown"))
message = self.format_faint(self.format_italic(str(hidden_count) + " not shown"))
lines.append(f"{prefix}{message}")

return lines, elided
Expand All @@ -256,23 +228,23 @@ def _render_node(self, info, active_depths, is_tail):
value = self._render_node_value(info)

line = (
f"{prefix}[{format_bold(info.identifier)}] {value}"
f"{prefix}[{self.format_bold(info.identifier)}] {value}"
if isinstance(info.parent_node, (list, tuple))
else f"{prefix}{format_bold(info.identifier)} {value}"
else f"{prefix}{self.format_bold(info.identifier)} {value}"
)

if info.info is not None:
line = line + format_faint(format_italic(" # " + info.info))
line = line + self.format_faint(self.format_italic(" # " + info.info))
visible_children = info.visible_children
if len(visible_children) == 0 and len(info.children) > 0:
line = line + format_italic(" ...")
line = line + self.format_italic(" ...")

if info.recursive:
line = line + " " + format_faint(format_italic("(recursive reference)"))
line = line + " " + self.format_faint(self.format_italic("(recursive reference)"))

if self._max_cols is not None and len(line) > self._max_cols:
message = " (truncated)"
line = line[0 : (self._max_cols - len(message))] + format_faint(format_italic(message))
line = line[0 : (self._max_cols - len(message))] + self.format_faint(self.format_italic(message))

return line

Expand Down Expand Up @@ -313,4 +285,4 @@ def _make_prefix(self, depth, active_depths, is_tail):

prefix = prefix + "└─" if is_tail else prefix + "├─"

return format_faint(prefix)
return self.format_faint(prefix)
4 changes: 2 additions & 2 deletions asdf/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import typing

from ._display import DEFAULT_MAX_COLS, DEFAULT_MAX_ROWS, DEFAULT_SHOW_VALUES, format_faint, format_italic, render_tree
from ._display import DEFAULT_MAX_COLS, DEFAULT_MAX_ROWS, DEFAULT_SHOW_VALUES, render_tree
from ._node_info import collect_schema_info
from .treeutil import get_children, is_container
from .util import NotSet
Expand Down Expand Up @@ -322,7 +322,7 @@ def __repr__(self):
)

if len(lines) == 0:
return format_faint(format_italic("No results found."))
return "No results found."

return "\n".join(lines)

Expand Down

0 comments on commit cf2dbfe

Please sign in to comment.