Skip to content

Commit d2434fa

Browse files
committed
Make sure terminal is available
1 parent 28e02d9 commit d2434fa

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
//
44

55
#include "gnuplot.h"
6+
#ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
7+
#include <experimental/filesystem>
8+
#else
69
#include <filesystem>
10+
#endif
711
#include <iostream>
812
#include <matplot/util/common.h>
913
#include <matplot/util/popen.h>
1014
#include <regex>
1115
#include <thread>
16+
#include <cstdlib>
1217

1318
#ifdef MATPLOT_HAS_FBUFSIZE
1419

@@ -31,14 +36,32 @@ namespace matplot::backend {
3136
bool gnuplot::consumes_gnuplot_commands() { return true; }
3237

3338
gnuplot::gnuplot() {
34-
// List terminal types
35-
terminal_ = default_terminal_type();
39+
// 1st option: terminal in GNUTERM environment variable
40+
const char *environment_terminal = std::getenv("GNUTERM");
41+
if (environment_terminal) {
42+
if (terminal_is_available(environment_terminal)) {
43+
terminal_ = environment_terminal;
44+
}
45+
#if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) || defined(__CYGWIN__)
46+
} else if (terminal_is_available("wxt")) {
47+
// 2nd option: wxt on windows, even if not default
48+
terminal_ = "wxt";
49+
#endif
50+
} else if (terminal_is_available("qt")) {
51+
// 3rd option: qt
52+
terminal_ = "qt";
53+
} else {
54+
// 4rd option: default terminal type
55+
terminal_ = default_terminal_type();
56+
}
57+
3658
// Open the gnuplot pipe_
3759
if constexpr (windows_should_persist_by_default) {
3860
pipe_ = POPEN("gnuplot --persist", "w");
3961
} else {
4062
pipe_ = POPEN("gnuplot", "w");
4163
}
64+
4265
// Check if everything is OK
4366
if (!pipe_) {
4467
std::cerr << "Opening the gnuplot pipe_ failed!" << std::endl;
@@ -71,6 +94,12 @@ namespace matplot::backend {
7194

7295
const std::string &gnuplot::output_format() { return terminal_; }
7396

97+
#ifdef STRING_VIEW_CONSTEXPR_BUG
98+
#define SV_CONSTEXPR
99+
#else
100+
#define SV_CONSTEXPR constexpr
101+
#endif
102+
74103
bool gnuplot::output(const std::string &filename) {
75104
if (filename.empty()) {
76105
output_ = filename;
@@ -79,12 +108,17 @@ namespace matplot::backend {
79108
}
80109

81110
// look at the extension
111+
#ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
112+
namespace fs = std::experimental::filesystem;
113+
#else
82114
namespace fs = std::filesystem;
115+
#endif
116+
83117
fs::path p{filename};
84118
std::string ext = p.extension().string();
85119

86120
// check terminal for that extension
87-
constexpr auto exts = extension_terminal();
121+
SV_CONSTEXPR auto exts = extension_terminal();
88122
auto it = std::find_if(exts.begin(), exts.end(),
89123
[&](const auto &e) { return e.first == ext; });
90124

@@ -115,7 +149,7 @@ namespace matplot::backend {
115149
}
116150

117151
// Check if file format is valid
118-
constexpr auto exts = extension_terminal();
152+
SV_CONSTEXPR auto exts = extension_terminal();
119153
auto it = std::find_if(exts.begin(), exts.end(), [&](const auto &e) {
120154
return e.second == format;
121155
});
@@ -127,7 +161,11 @@ namespace matplot::backend {
127161
}
128162

129163
// Create file if it does not exist
164+
#ifdef CXX_FILESYSTEM_IS_EXPERIMENTAL
165+
namespace fs = std::experimental::filesystem;
166+
#else
130167
namespace fs = std::filesystem;
168+
#endif
131169
fs::path p{filename};
132170
if (!p.parent_path().empty() && !fs::exists(p.parent_path())) {
133171
fs::create_directory(p.parent_path());
@@ -271,6 +309,11 @@ namespace matplot::backend {
271309
return terminal_type;
272310
}
273311

312+
bool gnuplot::terminal_is_available(std::string_view term) {
313+
std::string msg = run_and_get_output("gnuplot -e \"set terminal " + std::string(term.data()) + "\" 2>&1");
314+
return msg.empty();
315+
}
316+
274317
std::tuple<int, int, int> gnuplot::gnuplot_version() {
275318
static std::tuple<int, int, int> version{0, 0, 0};
276319
const bool dont_know_gnuplot_version_yet =
@@ -316,7 +359,7 @@ namespace matplot::backend {
316359
}
317360

318361
bool gnuplot::terminal_has_title_option(const std::string &t) {
319-
constexpr std::string_view whitelist[] = {
362+
SV_CONSTEXPR std::string_view whitelist[] = {
320363
"qt", "aqua", "caca", "canvas", "windows", "wxt", "x11"};
321364
return std::find(std::begin(whitelist), std::end(whitelist), t) !=
322365
std::end(whitelist);
@@ -326,7 +369,7 @@ namespace matplot::backend {
326369
// Terminals that have the size option *in the way we expect it to work*
327370
// This includes only the size option with {width, height} and not
328371
// the size option for cropping or scaling
329-
constexpr std::string_view whitelist[] = {
372+
SV_CONSTEXPR std::string_view whitelist[] = {
330373
"qt", "aqua", "caca", "canvas", "eepic",
331374
"emf", "gif", "jpeg", "pbm", "png",
332375
"sixelgd", "tkcanvas", "windows", "wxt", "svg"};
@@ -335,13 +378,13 @@ namespace matplot::backend {
335378
}
336379

337380
bool gnuplot::terminal_has_position_option(const std::string &t) {
338-
constexpr std::string_view whitelist[] = {"qt", "windows", "wxt"};
381+
SV_CONSTEXPR std::string_view whitelist[] = {"qt", "windows", "wxt"};
339382
return std::find(std::begin(whitelist), std::end(whitelist), t) !=
340383
std::end(whitelist);
341384
}
342385

343386
bool gnuplot::terminal_has_enhanced_option(const std::string &t) {
344-
constexpr std::string_view whitelist[] = {
387+
SV_CONSTEXPR std::string_view whitelist[] = {
345388
"canvas", "postscript", "qt", "aqua", "caca",
346389
"canvas", "dumb", "emf", "enhanced", "jpeg",
347390
"pdf", "pdfcairo", "pm", "png", "pngcairo",
@@ -352,7 +395,7 @@ namespace matplot::backend {
352395
}
353396

354397
bool gnuplot::terminal_has_color_option(const std::string &t) {
355-
constexpr std::string_view whitelist[] = {
398+
SV_CONSTEXPR std::string_view whitelist[] = {
356399
"postscript", "aifm", "caca", "cairolatex", "context",
357400
"corel", "eepic", "emf", "epscairo", "epslatex",
358401
"fig", "lua tikz", "mif", "mp", "pbm",
@@ -367,7 +410,7 @@ namespace matplot::backend {
367410
// and terminals for which we want to use only the default fonts
368411
// We prefer a blacklist because it's better to get a warning
369412
// in a false positive than remove the fonts in a false negative.
370-
constexpr std::string_view blacklist[] = {
413+
SV_CONSTEXPR std::string_view blacklist[] = {
371414
"dxf", "eepic", "emtex", "hpgl", "latex",
372415
"mf", "pcl5", "pslatex", "pstex", "pstricks",
373416
"qms", "tek40xx", "tek410x", "texdraw", "tkcanvas",

source/matplot/backend/gnuplot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace matplot::backend {
4444

4545
/// Identify the default terminal type in the system
4646
static std::string default_terminal_type();
47+
static bool terminal_is_available(std::string_view);
4748
static std::tuple<int, int, int> gnuplot_version();
4849
static bool terminal_has_title_option(const std::string &t);
4950
static bool terminal_has_size_option(const std::string &t);

0 commit comments

Comments
 (0)