Class: GnuplotRB::Terminal
- Inherits:
-
Object
- Object
- GnuplotRB::Terminal
- Includes:
- ErrorHandling
- Defined in:
- lib/gnuplotrb/staff/terminal.rb
Overview
Overview
Terminal keeps open pipe to gnuplot process, cares about naming in-memory datablocks (just indexing with sequential integers). All the output to gnuplot handled by this class. Terminal also handles options passed to gnuplot as ‘set key value’.
Constant Summary collapse
- OPTION_ORDER =
[:term, :output, :multiplot, :timefmt, :xrange]
Class Method Summary collapse
-
.close_arg(stream) ⇒ Object
Close given gnuplot pipe.
Instance Method Summary collapse
-
#<<(item) ⇒ Object
Overview Short way to plot Datablock, Plot or Splot object.
-
#close ⇒ Object
Overview Send gnuplot command to turn it off and for its Process to quit.
-
#initialize(persist: false) {|_self| ... } ⇒ Terminal
constructor
Overview Creates new Terminal connected with gnuplot.
-
#options_hash_to_string(options) ⇒ Object
Overview Converts given options to gnuplot format; for val1, .. , optN: valN it returns set opt1 val1 ..
-
#replot(**options) ⇒ Object
Overview Call replot on gnuplot.
-
#set(options) ⇒ Object
Overview Applies given options to current gnuplot instance; for val1, .. , optN: valN it will output to gnuplot set opt1 val1 ..
-
#store_datablock(data) ⇒ Object
Overview Outputs datablock to this gnuplot terminal.
-
#stream_print(command) ⇒ Object
Overview Just prints command to gnuplot pipe and returns self to allow chaining.
-
#stream_puts(command) ⇒ Object
Overview Just puts command to gnuplot pipe and returns self to allow chaining.
-
#unset(*options) ⇒ Object
Overview Unset some options ====== Arguments * *options - Array of options need to unset.
Methods included from ErrorHandling
Constructor Details
#initialize(persist: false) {|_self| ... } ⇒ Terminal
Overview
Creates new Terminal connected with gnuplot. Uses Settings::gnuplot_path to find gnuplot executable. Each time you create Terminal it starts new gnuplot subprocess which is closed after GC deletes linked Terminal object.
Arguments
-
persist - gnuplot’s -persist option
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 31 def initialize(persist: false) @cmd = Settings.gnuplot_path @current_datablock = 0 @cmd += ' -persist' if persist @cmd += ' 2>&1' stream = IO.popen(@cmd, 'w+') handle_stderr(stream) ObjectSpace.define_finalizer(self, proc { Terminal.close_arg(stream) }) @in = stream yield(self) if block_given? end |
Class Method Details
.close_arg(stream) ⇒ Object
Close given gnuplot pipe
15 16 17 18 19 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 15 def close_arg(stream) stream.puts stream.puts 'exit' Process.waitpid(stream.pid) end |
Instance Method Details
#<<(item) ⇒ Object
Overview
Short way to plot Datablock, Plot or Splot object. Other items will be just piped out to gnuplot.
120 121 122 123 124 125 126 127 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 120 def <<(item) if item.is_a? Plottable item.plot(self) else stream_print(item.to_s) end self end |
#close ⇒ Object
Overview
Send gnuplot command to turn it off and for its Process to quit. Closes pipe so Terminal object should not be used after #close call.
163 164 165 166 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 163 def close check_errors Terminal.close_arg(@in) end |
#options_hash_to_string(options) ⇒ Object
Overview
Converts given options to gnuplot format; for val1, .. , optN: valN it returns
set opt1 val1
..
set optN valN
Arguments
-
options - hash of options to convert
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 75 def () result = '' .sort_by { |key, _| OPTION_ORDER.find_index(key) || -1 }.each do |key, value| if value result += "set #{OptionHandling.option_to_string(key, value)}\n" else result += "unset #{key}\n" end end result end |
#replot(**options) ⇒ Object
Overview
Call replot on gnuplot. This will execute last plot once again with rereading data.
151 152 153 154 155 156 157 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 151 def replot(**) set() stream_puts('replot') unset(.keys) sleep 0.01 until File.size?([:output]) if [:output] self end |
#set(options) ⇒ Object
Overview
Applies given options to current gnuplot instance; for val1, .. , optN: valN it will output to gnuplot
set opt1 val1
..
set optN valN
Arguments
options - hash of options to set
Examples
set({term: ['qt', size: [100, 100]]})
#=> outputs to gnuplot: "set term qt size 100,100\n"
99 100 101 102 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 99 def set() OptionHandling.() stream_puts(()) end |
#store_datablock(data) ⇒ Object
Overview
Outputs datablock to this gnuplot terminal.
Arguments
-
data - data stored in datablock
Examples
data = "1 1\n2 4\n3 9"
Terminal.new.store_datablock(data)
#=> returns '$DATA1'
#=> outputs to gnuplot:
#=> $DATA1 << EOD
#=> 1 1
#=> 2 4
#=> 3 9
#=> EOD
58 59 60 61 62 63 64 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 58 def store_datablock(data) name = "$DATA#{@current_datablock += 1}" stream_puts "#{name} << EOD" stream_puts data stream_puts 'EOD' name end |
#stream_print(command) ⇒ Object
Overview
Just prints command to gnuplot pipe and returns self to allow chaining.
141 142 143 144 145 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 141 def stream_print(command) check_errors @in.print(command) self end |
#stream_puts(command) ⇒ Object
Overview
Just puts command to gnuplot pipe and returns self to allow chaining.
133 134 135 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 133 def stream_puts(command) stream_print("#{command}\n") end |
#unset(*options) ⇒ Object
Overview
Unset some options
Arguments
-
*options - Array of options need to unset
109 110 111 112 113 114 |
# File 'lib/gnuplotrb/staff/terminal.rb', line 109 def unset(*) .flatten .sort_by { |key| OPTION_ORDER.find_index(key) || -1 } .each { |key| stream_puts "unset #{OptionHandling.string_key(key)}" } self end |