forked from ievgrafov/gnuplotrb
/
settings.rb
89 lines (83 loc) · 2.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module GnuplotRB
##
# This module takes care of path to gnuplot executable and checking its version.
module Settings
##
# GnuplotRB can work with Gnuplot 5.0+
MIN_GNUPLOT_VERSION = 5.0
class << self
DEFAULT_MAX_FIT_DELAY = 5
DEFAULT_GNUPLOT_PATH = 'gnuplot'
##
# For heavy calculations max_fit_delay may be increased.
attr_writer :max_fit_delay
##
# Get max fit delay.
#
# Max fit delay (5s by default) is used inside Fit::fit function.
# If it waits for output more than max_fit_delay seconds
# this behaviour is considered as errorneus.
# @return [Integer] seconds to wait for output
def max_fit_delay
@max_fit_delay ||= DEFAULT_MAX_FIT_DELAY
end
##
# Get path that should be used to run gnuplot executable.
# Default value: 'gnuplot'.
# @return [String] path to gnuplot executable
def gnuplot_path
self.gnuplot_path = DEFAULT_GNUPLOT_PATH unless defined?(@gnuplot_path)
@gnuplot_path
end
##
# Set path to gnuplot executable.
# @param path [String] path to gnuplot executable
# @return given path
def 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
##
# Get list of terminals (png, html, qt, jpeg etc) available for this gnuplot.
# @return [Array of String] array of terminals available for this gnuplot
def available_terminals
gnuplot_path
@available_terminals
end
##
# Get gnuplot version. Uses gnuplot_path to find gnuplot executable.
# @return [Numeric] gnuplot version
def version
gnuplot_path
@version
end
##
# @private
# Validate gnuplot version. Compares current gnuplot's
# version with ::MIN_GNUPLOT_VERSION. Throws exception if version is
# less than min.
#
# @param path [String] path to gnuplot executable
def validate_version(path)
@version = IO.popen("#{path} --version")
.read
.match(/gnuplot ([^ ]+)/)[1]
.to_f
raise(
ArgumentError,
"Your Gnuplot version is #{@version}, please update it to at least 5.0"
) if @version < MIN_GNUPLOT_VERSION
rescue Errno::ENOENT
raise(
ArgumentError,
"Can't find Gnuplot executable. Please make sure it's installed and added to PATH."
)
end
end
end
end