Expand "~/" in paths to $HOME/

This commit is contained in:
Josh Holtrop 2018-03-20 22:07:40 -04:00
parent d93953a929
commit ec6d0eb8b7
3 changed files with 17 additions and 1 deletions

View File

@ -73,3 +73,17 @@ std::shared_ptr<std::vector<std::shared_ptr<std::string>>> Path::listdir(const s
return result; return result;
} }
std::string Path::clean(const std::string & s)
{
std::string p = s;
if (p.substr(0, 2) == "~/")
{
char * home = getenv("HOME");
if (home != nullptr)
{
p = home + p.substr(1, p.size());
}
}
return p;
}

View File

@ -19,6 +19,7 @@ public:
static bool is_file(const std::string & s); static bool is_file(const std::string & s);
static bool is_dir(const std::string & s); static bool is_dir(const std::string & s);
static std::shared_ptr<std::vector<std::shared_ptr<std::string>>> listdir(const std::string & path); static std::shared_ptr<std::vector<std::shared_ptr<std::string>>> listdir(const std::string & path);
static std::string clean(const std::string & s);
protected: protected:
static std::string _join(const std::string & first, const std::string & second); static std::string _join(const std::string & first, const std::string & second);

View File

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <unordered_map> #include <unordered_map>
#include "DefaultCommandMap.h" #include "DefaultCommandMap.h"
#include "Path.h"
#define INITIAL_WIDTH 800 #define INITIAL_WIDTH 800
#define INITIAL_HEIGHT 800 #define INITIAL_HEIGHT 800
@ -571,7 +572,7 @@ void Window::command_write_file(const CommandParser & cp)
} }
if (path != "") if (path != "")
{ {
buffer->write_to_file(path.c_str()); buffer->write_to_file(Path::clean(path).c_str());
} }
} }