From 2d68b250253eb07f8f796a6fe5f421efc90b70e1 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 6 Feb 2016 14:39:47 -0800 Subject: Early work towards moving the cd special autosuggestion into completions This will simplify some code and make the cd autosuggestion smarter --- src/path.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/path.cpp') 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; -- cgit v1.2.3