devices/cli: colorize output

This commit is contained in:
InsanePrawn 2023-01-04 04:09:28 +01:00
parent e269841038
commit 69c73e41dd
2 changed files with 20 additions and 14 deletions

View file

@ -3,12 +3,13 @@ from json import dumps as json_dump
import logging import logging
from config.state import config from config.state import config
from utils import colors_supported, color_str
from .device import get_devices, get_profile_device from .device import get_devices, get_profile_device
@click.command(name='devices') @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): def cmd_devices(json: bool = False):
'list the available devices and descriptions' 'list the available devices and descriptions'
devices = get_devices() 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}") logging.debug(f"Failed to get profile device for visual highlighting, not a problem: {ex}")
output = [''] output = ['']
json_output = {} json_output = {}
use_colors = colors_supported(False if json else config.runtime.colors)
for name in sorted(devices.keys()): for name in sorted(devices.keys()):
prefix = '' prefix = ''
suffix = '' suffix = ''
@ -36,9 +38,11 @@ def cmd_devices(json: bool = False):
json_output[name] = device.get_summary().toDict() json_output[name] = device.get_summary().toDict()
continue continue
if profile_device and profile_device.name == device.name: if profile_device and profile_device.name == device.name:
prefix = '>>> ' prefix = color_str('>>> ', bold=True, fg="bright_green", use_colors=use_colors)
suffix = f'\n\nCurrently selected by profile "{config.file.profiles.current}"' suffix = '\n\n'
snippet = f'{device}{suffix}' 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 # prefix each line in the snippet
snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')]) snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')])
output.append(f"{snippet}\n") output.append(f"{snippet}\n")

View file

@ -9,7 +9,7 @@ from config.scheme import DataClass, munchclass
from distro.distro import get_kupfer_local from distro.distro import get_kupfer_local
from distro.package import LocalPackage from distro.package import LocalPackage
from packages.pkgbuild import Pkgbuild, _pkgbuilds_cache, discover_pkgbuilds, get_pkgbuild_by_path, init_pkgbuilds 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 from .deviceinfo import DeviceInfo, parse_deviceinfo
@ -29,16 +29,18 @@ class DeviceSummary(DataClass):
package_name: Optional[str] package_name: Optional[str]
package_path: 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 ', ' separator = '\n' if newlines else ', '
return separator.join([ assert bool(self.package_path) == bool(self.package_name)
f'Device: {self.name}', package_path = {"Package Path": self.package_path} if self.package_path else {}
'Description: ' + fields = {
(self.description or f"[no package {'description' if self.package_name else 'associated (?!)'} and deviceinfo not parsed]"), "Device": self.name,
f'Architecture: {self.arch}', "Description": self.description or f"[no package {'description' if self.package_name else 'associated (?!)'} and deviceinfo not parsed]",
'Package Name: ' + "Architecture": self.arch,
(f"{self.package_name}{separator}Package Path: {self.package_path}" if self.package_name else "no package associated. PROBABLY A BUG!"), "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() @munchclass()