From 780fe03dd01bbee5b63b9461370531af7fd0abff Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 10 Sep 2016 11:01:45 +0200 Subject: [PATCH] Now uses grip when choosing Compile and run on a markdown file --- src/project.cc | 31 ++++++------------------------- src/terminal.cc | 14 ++++++++------ src/terminal.h | 2 +- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/project.cc b/src/project.cc index 11de813..6a2ecf1 100644 --- a/src/project.cc +++ b/src/project.cc @@ -629,33 +629,14 @@ void Project::Markdown::compile_and_run() { } std::stringstream stdin_stream, stdout_stream; - auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "markdown "+filesystem::escape_argument(Notebook::get().get_current_view()->file_path.string())); + auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "command -v grip"); if(exit_status==0) { - boost::system::error_code ec; - auto temp_path=boost::filesystem::temp_directory_path(ec); - if(!ec) { - temp_path/=boost::filesystem::unique_path(); - temp_path+=".html"; - if(!boost::filesystem::exists(temp_path)) { - last_temp_path=temp_path; - std::ofstream file_stream(temp_path.string(), std::fstream::binary); - file_stream << stdout_stream.rdbuf(); - file_stream.close(); - - auto uri=temp_path.string(); -#ifdef __APPLE__ - Terminal::get().process("open "+filesystem::escape_argument(uri)); -#else -#ifdef __linux - uri="file://"+uri; -#endif - GError* error=nullptr; - gtk_show_uri(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error); - g_clear_error(&error); -#endif - } - } + auto command="grip -b "+filesystem::escape_argument(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); } + else + Terminal::get().print("Warning: install grip to preview Markdown files\n"); } void Project::Python::compile_and_run() { diff --git a/src/terminal.cc b/src/terminal.cc index 831f7e8..ffd4596 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -109,14 +109,16 @@ int Terminal::process(std::istream &stdin_stream, std::ostream &stdout_stream, c return process.get_exit_status(); } -void Terminal::async_process(const std::string &command, const boost::filesystem::path &path, std::function callback) { - std::thread async_execute_thread([this, command, path, callback](){ +void Terminal::async_process(const std::string &command, const boost::filesystem::path &path, std::function callback, bool quiet) { + std::thread async_execute_thread([this, command, path, callback, quiet]() { std::unique_lock processes_lock(processes_mutex); stdin_buffer.clear(); - auto process=std::make_shared(command, path.string(), [this](const char* bytes, size_t n) { - async_print(std::string(bytes, n)); - }, [this](const char* bytes, size_t n) { - async_print(std::string(bytes, n), true); + auto process=std::make_shared(command, path.string(), [this, quiet](const char* bytes, size_t n) { + if(!quiet) + async_print(std::string(bytes, n)); + }, [this, quiet](const char* bytes, size_t n) { + if(!quiet) + async_print(std::string(bytes, n), true); }, true); auto pid=process->get_id(); if (pid<=0) { diff --git a/src/terminal.h b/src/terminal.h index cdde39c..bae5ba2 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -41,7 +41,7 @@ public: int process(const std::string &command, const boost::filesystem::path &path="", bool use_pipes=true); int process(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path=""); - void async_process(const std::string &command, const boost::filesystem::path &path="", std::function callback=nullptr); + void async_process(const std::string &command, const boost::filesystem::path &path="", std::function callback=nullptr, bool quiet=false); void kill_last_async_process(bool force=false); void kill_async_processes(bool force=false);