aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/path.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-02-06 14:39:47 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-02-18 17:00:25 -0800
commit2d68b250253eb07f8f796a6fe5f421efc90b70e1 (patch)
treeeed41798a1f5d624a4b3366c683569d5065b227b /src/path.cpp
parent1767681f9a61ac0789f5dafa76126e3446924b6d (diff)
Early work towards moving the cd special autosuggestion into completions
This will simplify some code and make the cd autosuggestion smarter
Diffstat (limited to 'src/path.cpp')
-rw-r--r--src/path.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/path.cpp b/src/path.cpp
index b8cfd516..a85b662f 100644
--- a/src/path.cpp
+++ b/src/path.cpp
@@ -240,6 +240,54 @@ bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wch
return result;
}
+/* If the given path looks like it's relative to the working directory, then prepend that working directory. This operates on unescaped paths only (so a ~ means a literal ~) */
+wcstring path_apply_working_directory(const wcstring &path, const wcstring &working_directory)
+{
+ if (path.empty() || working_directory.empty())
+ return path;
+
+ /* We're going to make sure that if we want to prepend the wd, that the string has no leading / */
+ bool prepend_wd;
+ switch (path.at(0))
+ {
+ case L'/':
+ case HOME_DIRECTORY:
+ prepend_wd = false;
+ break;
+ default:
+ prepend_wd = true;
+ break;
+ }
+
+ if (! prepend_wd)
+ {
+ /* No need to prepend the wd, so just return the path we were given */
+ return path;
+ }
+ else
+ {
+ /* Remove up to one ./ */
+ wcstring path_component = path;
+ if (string_prefixes_string(L"./", path_component))
+ {
+ path_component.erase(0, 2);
+ }
+
+ /* Removing leading /s */
+ while (string_prefixes_string(L"/", path_component))
+ {
+ path_component.erase(0, 1);
+ }
+
+ /* Construct and return a new path */
+ wcstring new_path = working_directory;
+ append_path_component(new_path, path_component);
+ return new_path;
+ }
+}
+
+
+
static wcstring path_create_config()
{
bool done = false;