Skip to content

Commit 1d33e0b

Browse files
committed
Improve version checking to guarantee we can use legends
1 parent bb839cf commit 1d33e0b

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ namespace matplot::backend {
271271
return terminal_type;
272272
}
273273

274-
std::pair<int, int> gnuplot::gnuplot_version() {
275-
static std::pair<int, int> version{0, 0};
274+
std::tuple<int, int, int> gnuplot::gnuplot_version() {
275+
static std::tuple<int, int, int> version{0, 0, 0};
276276
const bool dont_know_gnuplot_version_yet =
277-
version == std::pair<int, int>({0, 0});
277+
version == std::tuple<int, int, int>({0, 0, 0});
278278
if (dont_know_gnuplot_version_yet) {
279279
std::string version_str =
280280
run_and_get_output("gnuplot --version 2>&1");
@@ -286,20 +286,30 @@ namespace matplot::backend {
286286
version_str,
287287
std::regex("[^]*gnuplot \\d+\\.(\\d+) patchlevel \\d+ *"),
288288
"$1");
289+
std::string version_patch = std::regex_replace(
290+
version_str,
291+
std::regex("[^]*gnuplot \\d+\\.\\d+ patchlevel (\\d+) *"),
292+
"$1");
293+
try {
294+
std::get<0>(version) = std::stoi(version_major);
295+
} catch (...) {
296+
std::get<0>(version) = 0;
297+
}
289298
try {
290-
version.first = std::stoi(version_major);
299+
std::get<1>(version) = std::stoi(version_minor);
291300
} catch (...) {
292-
version.first = 0;
301+
std::get<1>(version) = 0;
293302
}
294303
try {
295-
version.second = std::stoi(version_minor);
304+
std::get<2>(version) = std::stoi(version_patch);
296305
} catch (...) {
297-
version.second = 0;
306+
std::get<2>(version) = 0;
298307
}
299308
const bool still_dont_know_gnuplot_version =
300-
version == std::pair<int, int>({0, 0});
309+
version == std::tuple<int, int, int>({0, 0, 0});
301310
if (still_dont_know_gnuplot_version) {
302-
version = std::pair<int, int>({5, 2});
311+
// assume it's 5.2.6 by convention
312+
version = std::tuple<int, int, int>({5, 2, 6});
303313
}
304314
}
305315
return version;

source/matplot/backend/gnuplot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <array>
99
#include <chrono>
1010
#include <matplot/backend/backend_interface.h>
11+
#include <tuple>
1112

1213
namespace matplot::backend {
1314
class gnuplot : public backend_interface {
@@ -43,7 +44,7 @@ namespace matplot::backend {
4344

4445
/// Identify the default terminal type in the system
4546
static std::string default_terminal_type();
46-
static std::pair<int, int> gnuplot_version();
47+
static std::tuple<int, int, int> gnuplot_version();
4748
static bool terminal_has_title_option(const std::string &t);
4849
static bool terminal_has_size_option(const std::string &t);
4950
static bool terminal_has_position_option(const std::string &t);

source/matplot/core/axes_type.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ namespace matplot {
667667
// Gnuplot version needs to be 5.2.6+ for keyentry
668668
bool ok = true;
669669
if (parent_->backend_->consumes_gnuplot_commands()) {
670-
std::pair<int, int> v = backend::gnuplot::gnuplot_version();
671-
if (v.first < 5 || v.second < 2) {
670+
if (backend::gnuplot::gnuplot_version() <
671+
std::tuple<int, int, int>{5, 2, 6}) {
672672
ok = false;
673673
}
674674
}
@@ -798,7 +798,7 @@ namespace matplot {
798798
} else if (is_3d()) {
799799
auto v = backend::gnuplot::gnuplot_version();
800800
const bool has_wall_option =
801-
v.first > 5 || (v.first == 5 && v.second >= 5);
801+
v < std::tuple<int, int, int>{5, 5, 0};
802802
if (has_wall_option) {
803803
run_command("set wall z0 fs transparent solid 0.5 "
804804
"border -1 fc 'slategray'");
@@ -884,8 +884,9 @@ namespace matplot {
884884
static bool msg_shown_once = false;
885885
// Gnuplot version needs to be 5.2.6+ for keyentry
886886
if (parent_->backend_->consumes_gnuplot_commands()) {
887-
std::pair<int, int> v = backend::gnuplot::gnuplot_version();
888-
if (v.first < 5 || v.second < 2) {
887+
std::tuple<int, int, int> v =
888+
backend::gnuplot::gnuplot_version();
889+
if (v < std::tuple<int, int, int>{5, 2, 6}) {
889890
if (!msg_shown_once) {
890891
std::cerr
891892
<< "You need gnuplot 5.2.6+ to include legends"

source/matplot/core/figure_type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ namespace matplot {
629629
// with a rectangle workaround, which does not work well for 3d.
630630
static const auto v = backend::gnuplot::gnuplot_version();
631631
const bool has_wall_option =
632-
v.first > 5 || (v.first == 5 && v.second >= 5);
632+
std::get<0>(v) > 5 || (std::get<0>(v) == 5 && std::get<1>(v) >= 5);
633633
// So we only plot the default background if it's not 3d or version is
634634
// higher than 5.5. Otherwise, gnuplot won't be able to set the axes
635635
// colors.

0 commit comments

Comments
 (0)