forked from ievgrafov/gnuplotrb
/
settings.rb
65 lines (61 loc) · 1.82 KB
/
settings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module GnuplotRB
##
# This module takes care of path to gnuplot executable
# and checking its version.
module Settings
##
# Since gem uses some modern gnuplot features it's
# required to have modern gnuplot installed.
MIN_GNUPLOT_VERSION = 5.0
##
# ====== Overview
# Get path that should be used to run gnuplot executable.
# Default value: 'gnuplot'
def self.gnuplot_path
self.gnuplot_path = 'gnuplot' unless defined?(@gnuplot_path)
@gnuplot_path
end
##
# ====== Overview
# Set path to gnuplot executable.
def self.gnuplot_path=(path)
validate_version(path)
opts = { stdin_data: "set term\n" }
@available_terminals = Open3.capture2e(path, **opts)
.first
.scan(/[:\n] +([a-z][^ ]+)/)
.map(&:first)
@gnuplot_path = path
end
##
# ====== Overview
# Get list of terminals (png, html, qt, jpeg etc)
# available for that gnuplot.
def self.available_terminals
gnuplot_path
@available_terminals
end
##
# ====== Overview
# Get gnuplot version. Uses gnuplot_path to find
# gnuplot executable.
def self.version
gnuplot_path
@version
end
##
# ====== Overview
# Validates gnuplot version. Compares current gnuplot's
# version with ::MIN_GNUPLOT_VERSION.
# ====== Arguments
# * *path* - path to gnuplot executable.
def self.validate_version(path)
@version = IO.popen("#{path} --version")
.read
.match(/gnuplot ([^ ]+)/)[1]
.to_f
message = "Your Gnuplot version is #{@version}, please update it to at least 5.0"
fail(ArgumentError, message) if @version < MIN_GNUPLOT_VERSION
end
end
end