From cc3ecab010f9446edf24b2963d3704f564b0f99f Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 31 May 2024 09:38:37 +0200 Subject: [PATCH] Added project.open_with_default_application to preferences --- CMakeLists.txt | 2 +- src/config.cpp | 12 +++++++++++- src/config.hpp | 1 + src/notebook.cpp | 12 ++++++++++++ src/window.cpp | 4 ++-- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb59141..57b2166 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5) project(juci) -set(JUCI_VERSION "1.8.0") +set(JUCI_VERSION "1.8.0.1") set(CPACK_PACKAGE_NAME "jucipp") set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim ") diff --git a/src/config.cpp b/src/config.cpp index 5e4e3b5..e002d33 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -189,6 +189,14 @@ void Config::read(const JSON &cfg) { project.cargo_command = project_json.string("cargo_command"); project.python_command = project_json.string("python_command"); project.markdown_command = project_json.string("markdown_command"); + std::stringstream ss(project_json.string("open_with_default_application")); + std::string str; + project.open_with_default_application.clear(); + while(getline(ss, str, ',')) { + while(!str.empty() && str.front() == ' ') + str.erase(str.begin()); + project.open_with_default_application.emplace_back(std::move(str)); + } auto terminal_json = cfg.object("terminal"); terminal.history_size = terminal_json.integer("history_size", JSON::ParseOptions::accept_string); @@ -347,7 +355,9 @@ std::string Config::default_config() { "grep_command": "grep", "cargo_command": "cargo", "python_command": "python -u", - "markdown_command": "grip -b" + "markdown_command": "grip -b", + "open_with_default_application_comment": "Comma-separated list of file extensions that should be opened with system default applications", + "open_with_default_application": ".pdf,.png" }, "keybindings": { "preferences": "comma", diff --git a/src/config.hpp b/src/config.hpp index 5d39cc0..73389f6 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -54,6 +54,7 @@ public: std::string cargo_command; std::string python_command; std::string markdown_command; + std::vector open_with_default_application; }; class Source { diff --git a/src/notebook.cpp b/src/notebook.cpp index d4f10a8..fdd04d4 100644 --- a/src/notebook.cpp +++ b/src/notebook.cpp @@ -149,6 +149,18 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position auto file_path = filesystem::get_normal_path(file_path_); + if(std::any_of(Config::get().project.open_with_default_application.begin(), Config::get().project.open_with_default_application.end(), [&file_path](const boost::filesystem::path &extension) { + return extension == file_path.extension(); + })) { +#ifdef __APPLE__ + Terminal::get().async_process("open " + filesystem::escape_argument(file_path.string()), "", nullptr, true); +#else + Terminal::get().async_process("xdg-open " + filesystem::escape_argument(file_path.string()), "", nullptr, true); +#endif + Directories::get().select(file_path); + return true; + } + if((position == Position::right || position == Position::split) && !split) toggle_split(); diff --git a/src/window.cpp b/src/window.cpp index 3bd8f1e..c443709 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1017,8 +1017,8 @@ void Window::set_menu_actions() { SelectionDialog::get()->on_select = [files = std::move(files)](unsigned int index, const std::string &text, bool hide_window) { if(Notebook::get().open(files[index])) { - auto view = Notebook::get().get_current_view(); - view->hide_tooltips(); + if(auto view = Notebook::get().get_current_view()) + view->hide_tooltips(); } };