From d50717bfb0c0c894843d1c4e11b609ed3db9b2cc Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 18 Jul 2021 14:39:51 +0200 Subject: Use package_task instead --- markdown.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'markdown.cpp') diff --git a/markdown.cpp b/markdown.cpp index 3f2eb4e..b4d692f 100644 --- a/markdown.cpp +++ b/markdown.cpp @@ -3,11 +3,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -22,6 +24,20 @@ ImFont * g_font_regular; ImFont * g_font_bold; ImFont * g_font_bold_large; +static std::string get(const std::string & command) +{ + std::string result; + std::array buffer; + std::unique_ptr opener(popen(command.c_str(), "r"), pclose); + if (!opener) + return std::string(); // TODO: time to do some error handling + while (nullptr != fgets(buffer.data(), buffer.size(), opener.get())) + { + result += buffer.data(); + } + return result; +} + struct Markdown : public imgui_md { ImFont * get_font() const override @@ -43,7 +59,6 @@ struct Markdown : public imgui_md void open_url() const override { - std::array buffer; std::string command = "browse -f markdown "; std::string_view prefix(m_href.data(), 4); if (prefix == "http") @@ -67,13 +82,9 @@ struct Markdown : public imgui_md return; // TODO: also an error } } - std::unique_ptr opener(popen(command.c_str(), "r"), pclose); - if (!opener) - return; // TODO: time to do some error handling - while (nullptr != fgets(buffer.data(), buffer.size(), opener.get())) - { - // TODO: Support reopening - } + std::packaged_task task(get); + std::thread t(std::move(task), command); + t.detach(); } }; -- cgit v1.1