aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2022-02-19 14:26:34 +0100
committerNaïm Favier <n@monade.li>2022-03-07 12:01:54 +0100
commit55c6906701ee7fc7e915f7889fea86957b020f94 (patch)
tree6cb5da6e1fb9f55ad0197fd28ade86a0c1db31df /src/libutil
parent5461ff532d6169be86af703b15cfb49569732cbb (diff)
Perform tilde expansion when completing flake fragments
Allows completing `nix build ~/flake#<Tab>`. We can implement expansion for `~user` later if needed. Not using wordexp(3) since that expands way too much.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/args.cc7
-rw-r--r--src/libutil/util.cc11
-rw-r--r--src/libutil/util.hh3
3 files changed, 18 insertions, 3 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index b60c609a6..10f01af5b 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -282,12 +282,13 @@ static void _completePath(std::string_view prefix, bool onlyDirs)
{
completionType = ctFilenames;
glob_t globbuf;
- int flags = GLOB_NOESCAPE | GLOB_TILDE;
+ int flags = GLOB_NOESCAPE;
#ifdef GLOB_ONLYDIR
if (onlyDirs)
flags |= GLOB_ONLYDIR;
#endif
- if (glob((std::string(prefix) + "*").c_str(), flags, nullptr, &globbuf) == 0) {
+ // using expandTilde here instead of GLOB_TILDE(_CHECK) so that ~<Tab> expands to /home/user/
+ if (glob((expandTilde(prefix) + "*").c_str(), flags, nullptr, &globbuf) == 0) {
for (size_t i = 0; i < globbuf.gl_pathc; ++i) {
if (onlyDirs) {
auto st = stat(globbuf.gl_pathv[i]);
@@ -295,8 +296,8 @@ static void _completePath(std::string_view prefix, bool onlyDirs)
}
completions->add(globbuf.gl_pathv[i]);
}
- globfree(&globbuf);
}
+ globfree(&globbuf);
}
void completePath(size_t, std::string_view prefix)
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index deaa17a7f..d9db24397 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -200,6 +200,17 @@ std::string_view baseNameOf(std::string_view path)
}
+std::string expandTilde(std::string_view path)
+{
+ // TODO: expand ~user ?
+ auto tilde = path.substr(0, 2);
+ if (tilde == "~/" || tilde == "~")
+ return getHome() + std::string(path.substr(1));
+ else
+ return std::string(path);
+}
+
+
bool isInDir(std::string_view path, std::string_view dir)
{
return path.substr(0, 1) == "/"
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 94ae8ab7d..a1d0e0e6b 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -68,6 +68,9 @@ Path dirOf(const PathView path);
following the final `/' (trailing slashes are removed). */
std::string_view baseNameOf(std::string_view path);
+/* Perform tilde expansion on a path. */
+std::string expandTilde(std::string_view path);
+
/* Check whether 'path' is a descendant of 'dir'. Both paths must be
canonicalized. */
bool isInDir(std::string_view path, std::string_view dir);