Browse Source

Replaced home path with ~ in various user messages

merge-requests/365/head
eidheim 8 years ago
parent
commit
7fc4ab069c
  1. 20
      src/config.cc
  2. 22
      src/filesystem.cc
  3. 3
      src/filesystem.h
  4. 14
      src/notebook.cc
  5. 28
      src/project.cc
  6. 5
      tests/filesystem_test.cc

20
src/config.cc

@ -7,24 +7,10 @@
#include <algorithm> #include <algorithm>
Config::Config() { Config::Config() {
std::vector<std::string> environment_variables = {"JUCI_HOME", "HOME", "AppData"}; home_path=filesystem::get_home_path();
char *ptr = nullptr; if(home_path.empty())
for (auto &variable : environment_variables) { throw std::runtime_error("Could not find home path");
ptr=std::getenv(variable.c_str());
if (ptr!=nullptr && boost::filesystem::exists(ptr)) {
home_path = ptr;
home_juci_path=home_path/".juci"; home_juci_path=home_path/".juci";
break;
}
}
if(home_juci_path.empty()) {
std::string searched_envs = "[";
for(auto &variable : environment_variables)
searched_envs+=variable+", ";
searched_envs.erase(searched_envs.end()-2, searched_envs.end());
searched_envs+="]";
throw std::runtime_error("One of these environment variables needs to point to a writable directory to save configuration: " + searched_envs);
}
#ifdef _WIN32 #ifdef _WIN32
auto env_MSYSTEM_PREFIX=std::getenv("MSYSTEM_PREFIX"); auto env_MSYSTEM_PREFIX=std::getenv("MSYSTEM_PREFIX");

22
src/filesystem.cc

@ -174,6 +174,28 @@ std::string filesystem::unescape_argument(const std::string &argument) {
return unescaped; return unescaped;
} }
boost::filesystem::path filesystem::get_home_path() noexcept {
std::vector<std::string> environment_variables = {"HOME", "AppData"};
char *ptr = nullptr;
for (auto &variable : environment_variables) {
ptr=std::getenv(variable.c_str());
boost::system::error_code ec;
if (ptr!=nullptr && boost::filesystem::exists(ptr, ec))
return ptr;
}
return boost::filesystem::path();
}
boost::filesystem::path filesystem::get_short_path(const boost::filesystem::path &path) noexcept {
static auto home_path=get_home_path();
if(!home_path.empty()) {
auto relative_path=filesystem::get_relative_path(path, home_path);
if(!relative_path.empty())
return "~"/relative_path;
}
return path;
}
bool filesystem::file_in_path(const boost::filesystem::path &file_path, const boost::filesystem::path &path) { bool filesystem::file_in_path(const boost::filesystem::path &file_path, const boost::filesystem::path &path) {
if(std::distance(file_path.begin(), file_path.end())<std::distance(path.begin(), path.end())) if(std::distance(file_path.begin(), file_path.end())<std::distance(path.begin(), path.end()))
return false; return false;

3
src/filesystem.h

@ -28,6 +28,9 @@ public:
static std::string escape_argument(const std::string &argument); static std::string escape_argument(const std::string &argument);
static std::string unescape_argument(const std::string &argument); static std::string unescape_argument(const std::string &argument);
static boost::filesystem::path get_home_path() noexcept;
static boost::filesystem::path get_short_path(const boost::filesystem::path &path) noexcept;
static bool file_in_path(const boost::filesystem::path &file_path, const boost::filesystem::path &path); static bool file_in_path(const boost::filesystem::path &file_path, const boost::filesystem::path &path);
static boost::filesystem::path find_file_in_path_parents(const std::string &file_name, const boost::filesystem::path &path); static boost::filesystem::path find_file_in_path_parents(const std::string &file_name, const boost::filesystem::path &path);

14
src/notebook.cc

@ -167,16 +167,8 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i
} }
}; };
source_views.back()->update_status_file_path=[this](Source::View* view) { source_views.back()->update_status_file_path=[this](Source::View* view) {
if(get_current_view()==view) { if(get_current_view()==view)
if(filesystem::file_in_path(view->file_path, Config::get().home_path)) { status_file_path.set_text(' '+filesystem::get_short_path(view->file_path).string());
auto relative_path=filesystem::get_relative_path(view->file_path, Config::get().home_path);
if(!relative_path.empty()) {
status_file_path.set_text((" ~"/relative_path).string());
return;
}
}
status_file_path.set_text(" "+view->file_path.string());
}
}; };
source_views.back()->update_status_branch=[this](Source::DiffView* view) { source_views.back()->update_status_branch=[this](Source::DiffView* view) {
if(get_current_view()==view) { if(get_current_view()==view) {
@ -196,7 +188,7 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i
if(source_views[c]==view) { if(source_views[c]==view) {
auto &tab_label=tab_labels.at(c); auto &tab_label=tab_labels.at(c);
tab_label->label.set_text(title); tab_label->label.set_text(title);
tab_label->set_tooltip_text(view->file_path.string()); tab_label->set_tooltip_text(filesystem::get_short_path(view->file_path).string());
update_status(view); update_status(view);
return; return;
} }

28
src/project.cc

@ -220,12 +220,12 @@ std::pair<std::string, std::string> Project::Clang::get_run_arguments() {
if(arguments.empty()) { if(arguments.empty()) {
auto view=Notebook::get().get_current_view(); auto view=Notebook::get().get_current_view();
auto executable=build->get_executable(view?view->file_path:Directories::get().path).string(); auto executable=build->get_executable(view?view->file_path:Directories::get().path);
if(!executable.empty()) if(!executable.empty())
arguments=filesystem::escape_argument(executable); arguments=filesystem::escape_argument(filesystem::get_short_path(executable).string());
else else
arguments=filesystem::escape_argument(build->get_default_path().string()); arguments=filesystem::escape_argument(filesystem::get_short_path(build->get_default_path()).string());
} }
return {project_path, arguments}; return {project_path, arguments};
@ -240,7 +240,7 @@ void Project::Clang::compile() {
Terminal::get().clear(); Terminal::get().clear();
compiling=true; compiling=true;
Terminal::get().print("Compiling project "+build->project_path.string()+"\n"); Terminal::get().print("Compiling project "+filesystem::get_short_path(build->project_path).string()+"\n");
std::string compile_command; std::string compile_command;
if(dynamic_cast<CMakeBuild*>(build.get())) if(dynamic_cast<CMakeBuild*>(build.get()))
compile_command=Config::get().project.cmake.compile_command; compile_command=Config::get().project.cmake.compile_command;
@ -265,13 +265,13 @@ void Project::Clang::compile_and_run() {
if(arguments.empty()) { if(arguments.empty()) {
auto view=Notebook::get().get_current_view(); auto view=Notebook::get().get_current_view();
arguments=build->get_executable(view?view->file_path:Directories::get().path).string(); auto executable=build->get_executable(view?view->file_path:Directories::get().path);
if(arguments.empty()) { if(executable.empty()) {
Terminal::get().print("Warning: could not find executable.\n"); Terminal::get().print("Warning: could not find executable.\n");
Terminal::get().print("Solution: either use Project Set Run Arguments, or open a source file within a directory where an executable is defined.\n", true); Terminal::get().print("Solution: either use Project Set Run Arguments, or open a source file within a directory where an executable is defined.\n", true);
return; return;
} }
arguments=filesystem::escape_argument(arguments); arguments=filesystem::escape_argument(filesystem::get_short_path(executable).string());
} }
if(Config::get().project.clear_terminal_on_compile) if(Config::get().project.clear_terminal_on_compile)
@ -324,7 +324,7 @@ void Project::Clang::recreate_build() {
boost::filesystem::remove_all(debug_build_path); boost::filesystem::remove_all(debug_build_path);
} }
catch(const std::exception &e) { catch(const std::exception &e) {
Terminal::get().print(std::string("Error: could remove build: ")+e.what()+"\n", true); Terminal::get().print(std::string("Error: could not remove build: ")+e.what()+"\n", true);
return; return;
} }
} }
@ -368,10 +368,10 @@ std::pair<std::string, std::string> Project::Clang::debug_get_run_arguments() {
size_t pos=executable.find(default_build_path.string()); size_t pos=executable.find(default_build_path.string());
if(pos!=std::string::npos) if(pos!=std::string::npos)
executable.replace(pos, default_build_path.string().size(), debug_build_path.string()); executable.replace(pos, default_build_path.string().size(), debug_build_path.string());
arguments=filesystem::escape_argument(executable); arguments=filesystem::escape_argument(filesystem::get_short_path(executable).string());
} }
else else
arguments=filesystem::escape_argument(build->get_debug_path().string()); arguments=filesystem::escape_argument(filesystem::get_short_path(build->get_debug_path()).string());
} }
return {project_path, arguments}; return {project_path, arguments};
@ -407,7 +407,7 @@ void Project::Clang::debug_start() {
size_t pos=run_arguments->find(default_build_path.string()); size_t pos=run_arguments->find(default_build_path.string());
if(pos!=std::string::npos) if(pos!=std::string::npos)
run_arguments->replace(pos, default_build_path.string().size(), debug_build_path.string()); run_arguments->replace(pos, default_build_path.string().size(), debug_build_path.string());
*run_arguments=filesystem::escape_argument(*run_arguments); *run_arguments=filesystem::escape_argument(filesystem::get_short_path(*run_arguments).string());
} }
if(Config::get().project.clear_terminal_on_compile) if(Config::get().project.clear_terminal_on_compile)
@ -680,7 +680,7 @@ void Project::Markdown::compile_and_run() {
std::stringstream stdin_stream, stdout_stream; std::stringstream stdin_stream, stdout_stream;
auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "command -v grip"); auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "command -v grip");
if(exit_status==0) { if(exit_status==0) {
auto command="grip -b "+filesystem::escape_argument(Notebook::get().get_current_view()->file_path.string()); auto command="grip -b "+filesystem::escape_argument(filesystem::get_short_path(Notebook::get().get_current_view()->file_path).string());
Terminal::get().print("Running: "+command+" in a quiet background process\n"); Terminal::get().print("Running: "+command+" in a quiet background process\n");
Terminal::get().async_process(command, "", nullptr, true); Terminal::get().async_process(command, "", nullptr, true);
} }
@ -689,7 +689,7 @@ void Project::Markdown::compile_and_run() {
} }
void Project::Python::compile_and_run() { void Project::Python::compile_and_run() {
auto command="PYTHONUNBUFFERED=1 python "+filesystem::escape_argument(Notebook::get().get_current_view()->file_path.string()); auto command="PYTHONUNBUFFERED=1 python "+filesystem::escape_argument(filesystem::get_short_path(Notebook::get().get_current_view()->file_path).string());
Terminal::get().print("Running "+command+"\n"); Terminal::get().print("Running "+command+"\n");
Terminal::get().async_process(command, Notebook::get().get_current_view()->file_path.parent_path(), [command](int exit_status) { Terminal::get().async_process(command, Notebook::get().get_current_view()->file_path.parent_path(), [command](int exit_status) {
Terminal::get().async_print(command+" returned: "+std::to_string(exit_status)+'\n'); Terminal::get().async_print(command+" returned: "+std::to_string(exit_status)+'\n');
@ -697,7 +697,7 @@ void Project::Python::compile_and_run() {
} }
void Project::JavaScript::compile_and_run() { void Project::JavaScript::compile_and_run() {
auto command="node --harmony "+filesystem::escape_argument(Notebook::get().get_current_view()->file_path.string()); auto command="node --harmony "+filesystem::escape_argument(filesystem::get_short_path(Notebook::get().get_current_view()->file_path).string());
Terminal::get().print("Running "+command+"\n"); Terminal::get().print("Running "+command+"\n");
Terminal::get().async_process(command, Notebook::get().get_current_view()->file_path.parent_path(), [command](int exit_status) { Terminal::get().async_process(command, Notebook::get().get_current_view()->file_path.parent_path(), [command](int exit_status) {
Terminal::get().async_print(command+" returned: "+std::to_string(exit_status)+'\n'); Terminal::get().async_print(command+" returned: "+std::to_string(exit_status)+'\n');

5
tests/filesystem_test.cc

@ -2,6 +2,11 @@
#include <glib.h> #include <glib.h>
int main() { int main() {
{
auto home_path=filesystem::get_home_path();
g_assert(!home_path.empty());
}
{ {
auto original="test () '\""; auto original="test () '\"";
auto escaped=filesystem::escape_argument(original); auto escaped=filesystem::escape_argument(original);

Loading…
Cancel
Save