diff --git a/src/config.cc b/src/config.cc index 82f62fd..f8de208 100644 --- a/src/config.cc +++ b/src/config.cc @@ -7,24 +7,10 @@ #include Config::Config() { - std::vector environment_variables = {"JUCI_HOME", "HOME", "AppData"}; - char *ptr = nullptr; - for (auto &variable : environment_variables) { - ptr=std::getenv(variable.c_str()); - if (ptr!=nullptr && boost::filesystem::exists(ptr)) { - home_path = ptr; - 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); - } + home_path=filesystem::get_home_path(); + if(home_path.empty()) + throw std::runtime_error("Could not find home path"); + home_juci_path=home_path/".juci"; #ifdef _WIN32 auto env_MSYSTEM_PREFIX=std::getenv("MSYSTEM_PREFIX"); diff --git a/src/filesystem.cc b/src/filesystem.cc index 7d24b03..8b5f0b8 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -174,6 +174,28 @@ std::string filesystem::unescape_argument(const std::string &argument) { return unescaped; } +boost::filesystem::path filesystem::get_home_path() noexcept { + std::vector 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) { if(std::distance(file_path.begin(), file_path.end())update_status_file_path=[this](Source::View* view) { - if(get_current_view()==view) { - if(filesystem::file_in_path(view->file_path, Config::get().home_path)) { - 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()); - } + if(get_current_view()==view) + status_file_path.set_text(' '+filesystem::get_short_path(view->file_path).string()); }; source_views.back()->update_status_branch=[this](Source::DiffView* 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) { auto &tab_label=tab_labels.at(c); 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); return; } diff --git a/src/project.cc b/src/project.cc index 25701ac..19ca888 100644 --- a/src/project.cc +++ b/src/project.cc @@ -220,12 +220,12 @@ std::pair Project::Clang::get_run_arguments() { if(arguments.empty()) { 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()) - arguments=filesystem::escape_argument(executable); + arguments=filesystem::escape_argument(filesystem::get_short_path(executable).string()); 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}; @@ -240,7 +240,7 @@ void Project::Clang::compile() { Terminal::get().clear(); 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; if(dynamic_cast(build.get())) compile_command=Config::get().project.cmake.compile_command; @@ -265,13 +265,13 @@ void Project::Clang::compile_and_run() { if(arguments.empty()) { auto view=Notebook::get().get_current_view(); - arguments=build->get_executable(view?view->file_path:Directories::get().path).string(); - if(arguments.empty()) { + auto executable=build->get_executable(view?view->file_path:Directories::get().path); + if(executable.empty()) { 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); return; } - arguments=filesystem::escape_argument(arguments); + arguments=filesystem::escape_argument(filesystem::get_short_path(executable).string()); } if(Config::get().project.clear_terminal_on_compile) @@ -324,7 +324,7 @@ void Project::Clang::recreate_build() { boost::filesystem::remove_all(debug_build_path); } 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; } } @@ -368,10 +368,10 @@ std::pair Project::Clang::debug_get_run_arguments() { size_t pos=executable.find(default_build_path.string()); if(pos!=std::string::npos) 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 - 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}; @@ -407,7 +407,7 @@ void Project::Clang::debug_start() { size_t pos=run_arguments->find(default_build_path.string()); if(pos!=std::string::npos) 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) @@ -680,7 +680,7 @@ void Project::Markdown::compile_and_run() { std::stringstream stdin_stream, stdout_stream; auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "command -v grip"); 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().async_process(command, "", nullptr, true); } @@ -689,7 +689,7 @@ void Project::Markdown::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().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'); @@ -697,7 +697,7 @@ void Project::Python::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().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'); diff --git a/tests/filesystem_test.cc b/tests/filesystem_test.cc index b36d74d..959c0ef 100644 --- a/tests/filesystem_test.cc +++ b/tests/filesystem_test.cc @@ -2,6 +2,11 @@ #include int main() { + { + auto home_path=filesystem::get_home_path(); + g_assert(!home_path.empty()); + } + { auto original="test () '\""; auto escaped=filesystem::escape_argument(original);