diff --git a/netplan_cli/cli/commands/status.py b/netplan_cli/cli/commands/status.py index 51d7438da..34e4d8c28 100644 --- a/netplan_cli/cli/commands/status.py +++ b/netplan_cli/cli/commands/status.py @@ -18,22 +18,29 @@ '''netplan status command line''' import json +import logging +import re import yaml -from rich.console import Console -from rich.highlighter import RegexHighlighter -from rich.theme import Theme from .. import utils from ..state import SystemConfigState, JSON -class NetplanHighlighter(RegexHighlighter): - base_style = 'netplan.' - highlights = [ - r'(^|[\s\/])(?P\d+)([\s:]?\s|$)', - r'(?P(\"|\').+(\"|\'))', - ] +RICH_OUTPUT = False +try: + from rich.console import Console + from rich.highlighter import RegexHighlighter + from rich.theme import Theme + class NetplanHighlighter(RegexHighlighter): + base_style = 'netplan.' + highlights = [ + r'(^|[\s\/])(?P\d+)([\s:]?\s|$)', + r'(?P(\"|\').+(\"|\'))', + ] + RICH_OUTPUT = True +except ImportError: + logging.debug("python3-rich not found, falling back to plain output") class NetplanStatus(utils.NetplanCommand): @@ -55,20 +62,31 @@ def run(self): self.parse_args() self.run_command() + def plain_print(self, *args, **kwargs): + if len(args): + pattern = r'\[\/?\w+\]' + lst = list(args) + lst[0] = re.sub(pattern, '', lst[0]) # remove any tags, like '[...]' or '[/...]' + return print(*lst, **kwargs) + return print(*args, **kwargs) + def pretty_print(self, data: JSON, total: int, _console_width=None) -> None: - # TODO: Use a proper (subiquity?) color palette - theme = Theme({ - 'netplan.int': 'bold cyan', - 'netplan.str': 'yellow', - 'muted': 'grey62', - 'online': 'green bold', - 'offline': 'red bold', - 'unknown': 'yellow bold', - 'highlight': 'bold' - }) - console = Console(highlighter=NetplanHighlighter(), theme=theme, - width=_console_width, emoji=False) - pprint = console.print + if RICH_OUTPUT: + # TODO: Use a proper (subiquity?) color palette + theme = Theme({ + 'netplan.int': 'bold cyan', + 'netplan.str': 'yellow', + 'muted': 'grey62', + 'online': 'green bold', + 'offline': 'red bold', + 'unknown': 'yellow bold', + 'highlight': 'bold' + }) + console = Console(highlighter=NetplanHighlighter(), theme=theme, + width=_console_width, emoji=False) + pprint = console.print + else: + pprint = self.plain_print pad = '18' global_state = data.get('netplan-global-state', {})