From 72e94398bf9fba5045a5c5686754b2c200f01e79 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Thu, 29 Dec 2016 01:31:44 +0200 Subject: [PATCH] Improve output of `pio lib list` and `pio lib search` commands --- docs | 2 +- platformio/commands/lib.py | 85 +++++++++++++++----------------------- platformio/managers/lib.py | 5 +-- tests/commands/test_lib.py | 2 +- 4 files changed, 37 insertions(+), 57 deletions(-) diff --git a/docs b/docs index 47c714ca..19fe2670 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 47c714cad73562ae4e46766e24db092a4cb00c90 +Subproject commit 19fe267062e8e2f33d84d8aedcf7a30f4056c2c4 diff --git a/platformio/commands/lib.py b/platformio/commands/lib.py index 1ca3a939..d0a22fc8 100644 --- a/platformio/commands/lib.py +++ b/platformio/commands/lib.py @@ -118,46 +118,33 @@ def lib_update(lm, libraries, only_check): lm.update(library, only_check=only_check) -####### +def print_lib_item(item): + click.secho(item['name'], fg="cyan") + click.echo("=" * len(item['name'])) + if "id" in item: + click.secho("#ID: %d" % item['id'], bold=True) + click.echo(item.get("description", item.get("url", "")).encode("utf-8")) + click.echo() -LIBLIST_TPL = ("[{id:^15}] {name:<25} {compatibility:<30} " - "\"{authornames}\": {description}") + for key in ("homepage", "license", "keywords"): + if key not in item or not item[key]: + continue + if isinstance(item[key], list): + click.echo("%s: %s" % (key.title(), ", ".join(item[key]))) + else: + click.echo("%s: %s" % (key.title(), item[key])) + for key in ("frameworks", "platforms"): + if key not in item: + continue + click.echo("Compatible %s: %s" % (key, ", ".join( + [i['title'] if isinstance(i, dict) else i for i in item[key]]))) -def echo_liblist_header(): - click.echo( - LIBLIST_TPL.format( - id=click.style( - "ID", fg="green"), - name=click.style( - "Name", fg="cyan"), - compatibility=click.style( - "Compatibility", fg="yellow"), - authornames="Authors", - description="Description")) - - terminal_width, _ = click.get_terminal_size() - click.echo("-" * terminal_width) - - -def echo_liblist_item(item): - description = item.get("description", item.get("url", "")).encode("utf-8") - if "version" in item: - description += " | @" + click.style(item['version'], fg="yellow") - - click.echo( - LIBLIST_TPL.format( - id=click.style( - str(item.get("id", "-")), fg="green"), - name=click.style( - item['name'], fg="cyan"), - compatibility=click.style( - ", ".join( - item.get("frameworks", ["-"]) + item.get("platforms", [])), - fg="yellow"), - authornames=", ".join(item.get("authornames", ["Unknown"])).encode( - "utf-8"), - description=description)) + if "authors" in item or "authornames" in item: + click.echo("Authors: %s" % ", ".join( + item.get("authornames", + [a.get("name", "") for a in item.get("authors", [])]))) + click.echo() @cli.command("search", short_help="Search for a library") @@ -185,7 +172,7 @@ def lib_search(query, json_output, page, noninteractive, **filters): query.append('%s:"%s"' % (key, value)) result = get_api_result( - "/lib/search", + "/v2/lib/search", dict( query=" ".join(query), page=page), cache_valid="3d") @@ -214,12 +201,9 @@ def lib_search(query, json_output, page, noninteractive, **filters): "Found %d libraries:\n" % result['total'], fg="green" if result['total'] else "yellow") - if result['total']: - echo_liblist_header() - while True: for item in result['items']: - echo_liblist_item(item) + print_lib_item(item) if (int(result['page']) * int(result['perpage']) >= int(result['total'])): @@ -236,7 +220,7 @@ def lib_search(query, json_output, page, noninteractive, **filters): elif not click.confirm("Show next libraries?"): break result = get_api_result( - "/lib/search", + "/v2/lib/search", dict( query=" ".join(query), page=int(result['page']) + 1), cache_valid="3d") @@ -254,11 +238,8 @@ def lib_list(lm, json_output): if not items: return - echo_liblist_header() for item in sorted(items, key=lambda i: i['name']): - if "authors" in item: - item['authornames'] = [i['name'] for i in item['authors']] - echo_liblist_item(item) + print_lib_item(item) @cli.command("show", short_help="Show detailed info about a library") @@ -275,13 +256,13 @@ def lib_show(library, json_output): click.secho(lib['name'], fg="cyan") click.echo("=" * len(lib['name'])) + click.secho("#ID: %d" % lib['id'], bold=True) click.echo(lib['description']) click.echo() click.echo("Version: %s, released %s" % (lib['version']['name'], arrow.get(lib['version']['released']).humanize())) - click.echo("Registry ID: %d" % lib['id']) click.echo("Manifest: %s" % lib['confurl']) for key in ("homepage", "repository", "license"): if key not in lib or not lib[key]: @@ -310,10 +291,10 @@ def lib_show(library, json_output): blocks.append(("Authors", _authors)) blocks.append(("Keywords", lib['keywords'])) - if lib['frameworks']: - blocks.append(("Compatible Frameworks", lib['frameworks'])) - if lib['platforms']: - blocks.append(("Compatible Platforms", lib['platforms'])) + for key in ("frameworks", "platforms"): + if key not in lib or not lib[key]: + continue + blocks.append(("Compatible %s" % key, [i['title'] for i in lib[key]])) blocks.append(("Headers", lib['headers'])) blocks.append(("Examples", lib['examples'])) blocks.append(("Versions", [ diff --git a/platformio/managers/lib.py b/platformio/managers/lib.py index 8008c753..380afd66 100644 --- a/platformio/managers/lib.py +++ b/platformio/managers/lib.py @@ -291,7 +291,7 @@ class LibraryManager(BasePkgManager): lib_info = None result = util.get_api_result( - "/lib/search", dict(query=" ".join(query)), cache_valid="3d") + "/v2/lib/search", dict(query=" ".join(query)), cache_valid="3d") if result['total'] == 1: lib_info = result['items'][0] elif result['total'] > 1: @@ -303,9 +303,8 @@ class LibraryManager(BasePkgManager): "by request %s:" % json.dumps(filters), fg="red", err=True) - commands.lib.echo_liblist_header() for item in result['items']: - commands.lib.echo_liblist_item(item) + commands.lib.print_lib_item(item) if not interactive: click.secho( diff --git a/tests/commands/test_lib.py b/tests/commands/test_lib.py index 94d932f0..47217262 100644 --- a/tests/commands/test_lib.py +++ b/tests/commands/test_lib.py @@ -125,7 +125,7 @@ def test_lib_show(clirunner, validate_cliresult, isolated_pio_home): result = clirunner.invoke(cmd_lib, ["show", "64"]) validate_cliresult(result) assert all([ - s in result.output for s in ("ArduinoJson", "arduino", "atmelavr") + s in result.output for s in ("ArduinoJson", "Arduino", "Atmel AVR") ]) result = clirunner.invoke(cmd_lib, ["show", "OneWire"]) validate_cliresult(result)