mirror of
https://github.com/microsoft/vcpkg.git
synced 2024-11-29 03:29:01 +08:00
vcpkg autocomplete
More work on autocomplete
This commit is contained in:
parent
53ad140251
commit
9e19c24d29
@ -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<std::string> 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<StatusParagraph*> 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<std::string> tokens = Strings::split(to_autocomplete, " ");
|
||||
if (tokens.size() == 1)
|
||||
{
|
||||
const std::string requested_command = tokens[0];
|
||||
|
||||
std::vector<std::string> 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<std::string> 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<StatusParagraph*> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user