aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schumacher <roschuma@microsoft.com>2017-10-06 14:42:34 -0700
committerGitHub <noreply@github.com>2017-10-06 14:42:34 -0700
commitbb48a94606ee1fa44078a811d411a24bfd4911da (patch)
treef49dc3e52b5eeae8ff49e78f794189066d5372ae
parent1008d3dd7ad3e466f92eff215924e816f0165b61 (diff)
parent016c53e231f353bdfa35f8e265b281e8d56031ed (diff)
downloadvcpkg-bb48a94606ee1fa44078a811d411a24bfd4911da.tar.gz
vcpkg-bb48a94606ee1fa44078a811d411a24bfd4911da.zip
Merge pull request #1896 from mariatav/dev/mariatav/autocomplete
Autocomplete: Command to enable tab completion
-rw-r--r--toolsrc/include/vcpkg_Commands.h5
-rw-r--r--toolsrc/src/commands_autocomplete.cpp79
-rw-r--r--toolsrc/src/commands_available_commands.cpp2
-rw-r--r--toolsrc/vcpkglib/vcpkglib.vcxproj1
-rw-r--r--toolsrc/vcpkglib/vcpkglib.vcxproj.filters3
5 files changed, 89 insertions, 1 deletions
diff --git a/toolsrc/include/vcpkg_Commands.h b/toolsrc/include/vcpkg_Commands.h
index 590f0208c..7cfa2760e 100644
--- a/toolsrc/include/vcpkg_Commands.h
+++ b/toolsrc/include/vcpkg_Commands.h
@@ -194,6 +194,11 @@ namespace vcpkg::Commands
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
+ namespace Autocomplete
+ {
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
+ }
+
namespace Help
{
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
diff --git a/toolsrc/src/commands_autocomplete.cpp b/toolsrc/src/commands_autocomplete.cpp
new file mode 100644
index 000000000..3963f904b
--- /dev/null
+++ b/toolsrc/src/commands_autocomplete.cpp
@@ -0,0 +1,79 @@
+#include "pch.h"
+
+#include "Paragraphs.h"
+#include "SortedVector.h"
+#include "vcpkg_Commands.h"
+#include "vcpkg_Maps.h"
+#include "vcpkg_System.h"
+#include "vcpkglib.h"
+
+namespace vcpkg::Commands::Autocomplete
+{
+ std::vector<std::string> autocomplete_install(
+ const std::vector<std::unique_ptr<SourceControlFile>>& source_paragraphs, const std::string& start_with)
+ {
+ std::vector<std::string> results;
+ const auto& istartswith = Strings::case_insensitive_ascii_starts_with;
+
+ for (const auto& source_control_file : source_paragraphs)
+ {
+ auto&& sp = *source_control_file->core_paragraph;
+
+ if (istartswith(sp.name, start_with))
+ {
+ results.push_back(sp.name);
+ }
+ }
+ return results;
+ }
+
+ std::vector<std::string> autocomplete_remove(std::vector<StatusParagraph*> installed_packages,
+ const std::string& start_with)
+ {
+ std::vector<std::string> results;
+ const auto& istartswith = Strings::case_insensitive_ascii_starts_with;
+
+ for (const auto& installed_package : installed_packages)
+ {
+ auto sp = installed_package->package.displayname();
+
+ if (istartswith(sp, start_with))
+ {
+ results.push_back(sp);
+ }
+ }
+ return results;
+ }
+
+ 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"));
+
+ 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")
+ {
+ 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);
+ }
+
+ System::println(Strings::join(" ", results));
+ Checks::exit_success(VCPKG_LINE_INFO);
+ }
+}
diff --git a/toolsrc/src/commands_available_commands.cpp b/toolsrc/src/commands_available_commands.cpp
index 87cc43dca..d3280e6d7 100644
--- a/toolsrc/src/commands_available_commands.cpp
+++ b/toolsrc/src/commands_available_commands.cpp
@@ -34,7 +34,7 @@ namespace vcpkg::Commands
{"import", &Import::perform_and_exit},
{"cache", &Cache::perform_and_exit},
{"portsdiff", &PortsDiff::perform_and_exit},
- };
+ {"autocomplete", &Autocomplete::perform_and_exit}};
return t;
}
diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj b/toolsrc/vcpkglib/vcpkglib.vcxproj
index 7c5537e15..3bb945e85 100644
--- a/toolsrc/vcpkglib/vcpkglib.vcxproj
+++ b/toolsrc/vcpkglib/vcpkglib.vcxproj
@@ -183,6 +183,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\BinaryParagraph.cpp" />
+ <ClCompile Include="..\src\commands_autocomplete.cpp" />
<ClCompile Include="..\src\commands_ci.cpp" />
<ClCompile Include="..\src\commands_depends.cpp" />
<ClCompile Include="..\src\commands_env.cpp" />
diff --git a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters
index cc82198ee..918f83b0a 100644
--- a/toolsrc/vcpkglib/vcpkglib.vcxproj.filters
+++ b/toolsrc/vcpkglib/vcpkglib.vcxproj.filters
@@ -183,6 +183,9 @@
<ClCompile Include="..\src\vcpkg_GlobalState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\src\commands_autocomplete.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\SourceParagraph.h">