From 9e19c24d2911ff1ef648aeed0c596e3987eafebf Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 12 Oct 2017 14:03:01 -0700 Subject: [PATCH] `vcpkg autocomplete` More work on autocomplete --- toolsrc/src/commands_autocomplete.cpp | 91 +++++++++++++++++++-------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/toolsrc/src/commands_autocomplete.cpp b/toolsrc/src/commands_autocomplete.cpp index 3963f904b4..abad19df7d 100644 --- a/toolsrc/src/commands_autocomplete.cpp +++ b/toolsrc/src/commands_autocomplete.cpp @@ -1,9 +1,8 @@ #include "pch.h" #include "Paragraphs.h" -#include "SortedVector.h" +#include "metrics.h" #include "vcpkg_Commands.h" -#include "vcpkg_Maps.h" #include "vcpkg_System.h" #include "vcpkglib.h" @@ -35,7 +34,7 @@ namespace vcpkg::Commands::Autocomplete for (const auto& installed_package : installed_packages) { - auto sp = installed_package->package.displayname(); + const auto sp = installed_package->package.displayname(); if (istartswith(sp, start_with)) { @@ -47,33 +46,73 @@ namespace vcpkg::Commands::Autocomplete void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) { - static const std::string EXAMPLE = - Strings::format("The argument should be a command line to autocomplete.\n%s", - Commands::Help::create_example_string("autocomplete install z")); + Metrics::g_metrics.lock()->set_send_metrics(false); - args.check_min_arg_count(1, EXAMPLE); - args.check_max_arg_count(2, EXAMPLE); - args.check_and_get_optional_command_arguments({}); - - const std::string requested_command = args.command_arguments.at(0); - const std::string start_with = - args.command_arguments.size() > 1 ? args.command_arguments.at(1) : Strings::EMPTY; - std::vector results; - if (requested_command == "install") + if (args.command_arguments.size() != 1) { - auto sources_and_errors = Paragraphs::try_load_all_ports(paths.get_filesystem(), paths.ports); - auto& source_paragraphs = sources_and_errors.paragraphs; - - results = autocomplete_install(source_paragraphs, start_with); - } - else if (requested_command == "remove") - { - const StatusParagraphs status_db = database_load_check(paths); - std::vector installed_packages = get_installed_ports(status_db); - results = autocomplete_remove(installed_packages, start_with); + Checks::exit_success(VCPKG_LINE_INFO); + } + + const std::string to_autocomplete = args.command_arguments.at(0); + const std::vector tokens = Strings::split(to_autocomplete, " "); + if (tokens.size() == 1) + { + const std::string requested_command = tokens[0]; + + std::vector valid_commands = {"install", + "search", + "remove", + "list", + "update", + "hash", + "help", + "integrate", + "export", + "edit", + "create", + "owns", + "cache", + "version", + "contact"}; + + Util::unstable_keep_if(valid_commands, [&](const std::string& s) { + return Strings::case_insensitive_ascii_starts_with(s, requested_command); + }); + + if (valid_commands.size() == 1) + { + System::println(valid_commands[0] + " "); + } + else + { + System::println(Strings::join("\n", valid_commands)); + } + + Checks::exit_success(VCPKG_LINE_INFO); + } + + if (tokens.size() == 2) + { + const std::string requested_command = tokens[0]; + const std::string start_with = tokens[1]; + std::vector results; + if (requested_command == "install") + { + auto sources_and_errors = Paragraphs::try_load_all_ports(paths.get_filesystem(), paths.ports); + auto& source_paragraphs = sources_and_errors.paragraphs; + + results = autocomplete_install(source_paragraphs, start_with); + } + else if (requested_command == "remove") + { + const StatusParagraphs status_db = database_load_check(paths); + const std::vector installed_packages = get_installed_ports(status_db); + results = autocomplete_remove(installed_packages, start_with); + } + + System::println(Strings::join("\n", results)); } - System::println(Strings::join(" ", results)); Checks::exit_success(VCPKG_LINE_INFO); } }