diff --git a/devices/cli.py b/devices/cli.py index 6ccfc27..08a93db 100644 --- a/devices/cli.py +++ b/devices/cli.py @@ -3,12 +3,13 @@ from json import dumps as json_dump import logging from config.state import config +from utils import colors_supported, color_str from .device import get_devices, get_profile_device @click.command(name='devices') -@click.option('--json', is_flag=True, help='output machine-parsable JSON format') +@click.option('-j', '--json', is_flag=True, help='output machine-parsable JSON format') def cmd_devices(json: bool = False): 'list the available devices and descriptions' devices = get_devices() @@ -23,6 +24,7 @@ def cmd_devices(json: bool = False): logging.debug(f"Failed to get profile device for visual highlighting, not a problem: {ex}") output = [''] json_output = {} + use_colors = colors_supported(False if json else config.runtime.colors) for name in sorted(devices.keys()): prefix = '' suffix = '' @@ -36,9 +38,11 @@ def cmd_devices(json: bool = False): json_output[name] = device.get_summary().toDict() continue if profile_device and profile_device.name == device.name: - prefix = '>>> ' - suffix = f'\n\nCurrently selected by profile "{config.file.profiles.current}"' - snippet = f'{device}{suffix}' + prefix = color_str('>>> ', bold=True, fg="bright_green", use_colors=use_colors) + suffix = '\n\n' + suffix += color_str('Currently selected by profile', bold=True, use_colors=use_colors) + " " + suffix += color_str(f'"{config.file.profiles.current}"', bold=True, fg="bright_green", use_colors=use_colors) + snippet = f'{device.nice_str(colors=use_colors, newlines=True)}{suffix}' # prefix each line in the snippet snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')]) output.append(f"{snippet}\n") diff --git a/devices/device.py b/devices/device.py index 02c25af..0947800 100644 --- a/devices/device.py +++ b/devices/device.py @@ -9,7 +9,7 @@ from config.scheme import DataClass, munchclass from distro.distro import get_kupfer_local from distro.package import LocalPackage from packages.pkgbuild import Pkgbuild, _pkgbuilds_cache, discover_pkgbuilds, get_pkgbuild_by_path, init_pkgbuilds -from utils import read_files_from_tar +from utils import read_files_from_tar, color_str from .deviceinfo import DeviceInfo, parse_deviceinfo @@ -29,16 +29,18 @@ class DeviceSummary(DataClass): package_name: Optional[str] package_path: Optional[str] - def nice_str(self, newlines: bool = False): + def nice_str(self, newlines: bool = False, colors: bool = False) -> str: separator = '\n' if newlines else ', ' - return separator.join([ - f'Device: {self.name}', - 'Description: ' + - (self.description or f"[no package {'description' if self.package_name else 'associated (?!)'} and deviceinfo not parsed]"), - f'Architecture: {self.arch}', - 'Package Name: ' + - (f"{self.package_name}{separator}Package Path: {self.package_path}" if self.package_name else "no package associated. PROBABLY A BUG!"), - ]) + assert bool(self.package_path) == bool(self.package_name) + package_path = {"Package Path": self.package_path} if self.package_path else {} + fields = { + "Device": self.name, + "Description": self.description or f"[no package {'description' if self.package_name else 'associated (?!)'} and deviceinfo not parsed]", + "Architecture": self.arch, + "Package Name": self.package_name or "no package associated. PROBABLY A BUG!", + **package_path, + } + return separator.join([f"{color_str(name, bold=True, use_colors=colors)}: {value}" for name, value in fields.items()]) @munchclass()