diff --git a/gravitiny/src/cli.py b/gravitiny/src/cli.py index d193c48..b5be97a 100644 --- a/gravitiny/src/cli.py +++ b/gravitiny/src/cli.py @@ -50,17 +50,15 @@ def print_help() -> None: def cmd_search(args: List[str]) -> int: - if not args: - print(catalog.get("commands.search.missing_query")) - print(catalog.get("commands.search.usage")) - return 1 + force_refresh = "--refresh" in args + filtered_args = [a for a in args if a != "--refresh"] + query = " ".join(filtered_args) if filtered_args else "" - query = " ".join(args) client = RegistryClient() - results = client.search(query) + results = client.search(query, force_refresh=force_refresh) if not results: - print(catalog.format("commands.search.no_results", query=query)) + print(catalog.format("commands.search.no_results", query=query or "all")) return 0 print(catalog.format("commands.search.results_header", count=len(results))) @@ -121,44 +119,54 @@ def cmd_remove(args: List[str]) -> int: return 1 -def cmd_list() -> int: +def cmd_list(args: Optional[List[str]] = None) -> int: + raw_args = args or [] + show_remote = "-a" in raw_args or "--all" in raw_args or "-r" in raw_args or "--remote" in raw_args + discovery = AppletDiscovery() applets = discovery.discover_all() if not applets: print(catalog.get("commands.list.no_applets_found")) - return 0 + if not show_remote: + print("\nRun 'gravitiny search' or 'gravitiny list --all' to discover available applets in the registry.") + return 0 - print(catalog.format("commands.list.installed_header", count=len(applets))) - print() + if applets: + print(catalog.format("commands.list.installed_header", count=len(applets))) + print() - # Calculate column widths - id_w = max(len(catalog.get("commands.list.table_col_id")), max(len(a.id) for a in applets.values())) + 2 - name_w = max(len(catalog.get("commands.list.table_col_name")), max(len(a.name) for a in applets.values())) + 2 - ver_w = max(len(catalog.get("commands.list.table_col_version")), max(len(a.version) for a in applets.values())) + 2 - shims_w = max(len(catalog.get("commands.list.table_col_shims")), max(len(", ".join(a.shims)) for a in applets.values())) + 2 + id_w = max(len(catalog.get("commands.list.table_col_id")), max(len(a.id) for a in applets.values())) + 2 + name_w = max(len(catalog.get("commands.list.table_col_name")), max(len(a.name) for a in applets.values())) + 2 + ver_w = max(len(catalog.get("commands.list.table_col_version")), max(len(a.version) for a in applets.values())) + 2 + shims_w = max(len(catalog.get("commands.list.table_col_shims")), max(len(", ".join(a.shims)) for a in applets.values())) + 2 - header = ( - f"{catalog.get('commands.list.table_col_id'):<{id_w}} " - f"{catalog.get('commands.list.table_col_name'):<{name_w}} " - f"{catalog.get('commands.list.table_col_version'):<{ver_w}} " - f"{catalog.get('commands.list.table_col_shims'):<{shims_w}} " - f"{catalog.get('commands.list.table_col_description')}" - ) - separator = "-" * max(len(header), 70) - - print(header) - print(separator) - - for applet in sorted(applets.values(), key=lambda x: x.id): - shims_str = ", ".join(applet.shims) if applet.shims else applet.id - print( - f"{applet.id:<{id_w}} " - f"{applet.name:<{name_w}} " - f"{applet.version:<{ver_w}} " - f"{shims_str:<{shims_w}} " - f"{applet.description}" + header = ( + f"{catalog.get('commands.list.table_col_id'):<{id_w}} " + f"{catalog.get('commands.list.table_col_name'):<{name_w}} " + f"{catalog.get('commands.list.table_col_version'):<{ver_w}} " + f"{catalog.get('commands.list.table_col_shims'):<{shims_w}} " + f"{catalog.get('commands.list.table_col_description')}" ) + separator = "-" * max(len(header), 70) + + print(header) + print(separator) + + for applet in sorted(applets.values(), key=lambda x: x.id): + shims_str = ", ".join(applet.shims) if applet.shims else applet.id + print( + f"{applet.id:<{id_w}} " + f"{applet.name:<{name_w}} " + f"{applet.version:<{ver_w}} " + f"{shims_str:<{shims_w}} " + f"{applet.description}" + ) + + if show_remote: + print() + print("Available Remote Registry Applets:") + cmd_search(["--refresh"]) return 0 @@ -431,7 +439,7 @@ def run_cli(args: Optional[List[str]] = None) -> int: elif command == "remove": return cmd_remove(sub_args) elif command == "list": - return cmd_list() + return cmd_list(sub_args) elif command == "run": return cmd_run(sub_args) elif command == "info": diff --git a/gravitiny/src/registry.py b/gravitiny/src/registry.py index cb9068f..d69677d 100644 --- a/gravitiny/src/registry.py +++ b/gravitiny/src/registry.py @@ -69,14 +69,15 @@ class RegistryClient: return {} - def search(self, query: str) -> List[Dict[str, Any]]: - """Searches across applet id, name, description, tags, and author.""" + def search(self, query: str = "", force_refresh: bool = False) -> List[Dict[str, Any]]: + """Searches across applet id, name, description, tags, and author. Empty query returns all.""" clean_q = query.strip().lower() - if not clean_q: - return [] - - index = self.fetch_index() + index = self.fetch_index(force_refresh=force_refresh) applets = index.get("applets", {}) + + if not clean_q or clean_q in ["all", "*", "--all", "-a"]: + return list(applets.values()) + results: List[Dict[str, Any]] = [] for applet_id, info in applets.items(): @@ -96,9 +97,13 @@ class RegistryClient: return results - def get_applet(self, applet_id: str) -> Optional[Dict[str, Any]]: + def get_applet(self, applet_id: str, force_refresh: bool = False) -> Optional[Dict[str, Any]]: """Retrieves metadata for a specific remote applet.""" clean_id = applet_id.strip().lower() - index = self.fetch_index() + index = self.fetch_index(force_refresh=force_refresh) applets = index.get("applets", {}) + if clean_id not in applets and not force_refresh: + # Retry with force refresh once + index = self.fetch_index(force_refresh=True) + applets = index.get("applets", {}) return applets.get(clean_id)