summaryrefslogtreecommitdiff
path: root/markdown.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-07-18 14:39:51 +0200
committerAki <please@ignore.pl>2021-07-18 14:39:51 +0200
commitd50717bfb0c0c894843d1c4e11b609ed3db9b2cc (patch)
treedad387965b647e68c119014d3cc203cc9bc04648 /markdown.cpp
parent460e065d6856b3752f5fa2377a055f33d1c844ae (diff)
downloadmarkdown-d50717bfb0c0c894843d1c4e11b609ed3db9b2cc.zip
markdown-d50717bfb0c0c894843d1c4e11b609ed3db9b2cc.tar.gz
markdown-d50717bfb0c0c894843d1c4e11b609ed3db9b2cc.tar.bz2
Use package_task instead
Diffstat (limited to 'markdown.cpp')
-rw-r--r--markdown.cpp27
1 files changed, 19 insertions, 8 deletions
diff --git a/markdown.cpp b/markdown.cpp
index 3f2eb4e..b4d692f 100644
--- a/markdown.cpp
+++ b/markdown.cpp
@@ -3,11 +3,13 @@
#include <array>
#include <cstdio>
#include <fstream>
+#include <future>
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
#include <string_view>
+#include <thread>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@@ -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<char, 512> buffer;
+ std::unique_ptr<FILE, decltype(&pclose)> 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<char, 512> 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<FILE, decltype(&pclose)> 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<std::string (const std::string &)> task(get);
+ std::thread t(std::move(task), command);
+ t.detach();
}
};