devices, flavours, config: add "inherited from ABC" to "selected by XYZ" output

This commit is contained in:
InsanePrawn 2023-06-25 16:04:35 +02:00
parent 7425356f10
commit 60b38d895c
3 changed files with 43 additions and 46 deletions

View file

@ -4,10 +4,11 @@ import logging
from json import dumps as json_dump
from typing import Optional
from config.cli import resolve_profile_field
from config.state import config
from utils import colors_supported, color_str
from utils import color_mark_selected, colors_supported
from .flavour import get_flavours, get_profile_flavour
from .flavour import get_flavours, get_flavour
profile_option = click.option('-p', '--profile', help="name of the profile to use", required=False, default=None)
@ -23,13 +24,17 @@ def cmd_flavours(json: bool = False, output_file: Optional[str] = None):
flavours = get_flavours()
interactive_json = json and not output_file
use_colors = colors_supported(config.runtime.colors) and not interactive_json
profile_name = config.file.profiles.current
selected, inherited_from = None, None
if output_file:
json = True
if not flavours:
raise Exception("No flavours found!")
if not interactive_json:
try:
profile_flavour = get_profile_flavour()
selected, inherited_from = resolve_profile_field(None, profile_name, 'flavour', config.file.profiles)
if selected:
profile_flavour = get_flavour(selected)
except Exception as ex:
logging.debug(f"Failed to get profile flavour for marking as currently selected, continuing anyway. Exception: {ex}")
for name in sorted(flavours.keys()):
@ -39,15 +44,11 @@ def cmd_flavours(json: bool = False, output_file: Optional[str] = None):
except Exception as ex:
logging.debug(f"A problem happened while parsing flavourinfo for {name}, continuing anyway. Exception: {ex}")
if not interactive_json:
block = [*f.nice_str(newlines=True, colors=use_colors).split('\n'), '']
snippet = f.nice_str(newlines=True, colors=use_colors)
if profile_flavour == f:
prefix = color_str('>>> ', bold=True, fg='bright_green', use_colors=use_colors)
block += [
color_str("Currently selected by profile ", bold=True, use_colors=use_colors) +
color_str(f'"{config.file.profiles.current}"\n', bold=True, fg="bright_green")
]
block = [prefix + line for line in block]
results += block
snippet = color_mark_selected(snippet, profile_name or '[unknown]', inherited_from)
snippet += '\n'
results += snippet.split('\n')
if json:
d = dict(f)
d["description"] = f.flavour_info.description if (f.flavour_info and f.flavour_info.description) else f.description
@ -58,7 +59,7 @@ def cmd_flavours(json: bool = False, output_file: Optional[str] = None):
d["pkgbuild"] = f.pkgbuild.path if f.pkgbuild else None
d["package"] = f.pkgbuild.name
d["arches"] = sorted(f.pkgbuild.arches) if f.pkgbuild else None
json_results[d["name"]] = d
json_results[name] = d
print()
if output_file:
with open(output_file, 'w') as fd: