class GnuplotRB::Terminal

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'.

Constants

OPTION_ORDER

order is important for some options

Public Class Methods

close_arg(stream) click to toggle source

Close given gnuplot pipe @param stream [IO] pipe to close

# File lib/gnuplotrb/staff/terminal.rb, line 19
def close_arg(stream)
  stream.puts
  stream.puts 'exit'
  Process.waitpid(stream.pid)
end
new(persist: false) { |self| ... } click to toggle source

Create 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.

@param :persist [Boolean] gnuplot's “-persist” option

# File lib/gnuplotrb/staff/terminal.rb, line 47
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
test(term_name, file_name = nil) click to toggle source

Plot test page for given term_name into file with file_name (optional).

Test page contains possibilities of the term. @param term_name [String] terminal name ('png', 'gif', 'svg' etc) @param file_name [String] filename to output image if needed

and chosen terminal supports image output

@return nil

# File lib/gnuplotrb/staff/terminal.rb, line 34
def test(term_name, file_name = nil)
  Terminal.new.set(term: term_name).test(file_name)
end

Public Instance Methods

<<(item) click to toggle source

Short way to plot Datablock, Plot or Splot object. Other items will be just piped out to gnuplot. @param item Object that should be outputted to Gnuplot @return [Terminal] self

# File lib/gnuplotrb/staff/terminal.rb, line 138
def <<(item)
  if item.is_a? Plottable
    item.plot(self)
  else
    stream_print(item.to_s)
  end
  self
end
close() click to toggle source

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.

# File lib/gnuplotrb/staff/terminal.rb, line 182
def close
  check_errors
  Terminal.close_arg(@in)
end
options_hash_to_string(options) click to toggle source

Convert given options to gnuplot format.

For “{ opt1: val1, .. , optN: valN }” it returns

set opt1 val1
..
set optN valN

@param ptions [Hash] options to convert @return [String] options in Gnuplot format

# File lib/gnuplotrb/staff/terminal.rb, line 91
def options_hash_to_string(options)
  result = ''
  options.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) click to toggle source

@deprecated Call replot on gnuplot. This will execute last plot once again with rereading data. @param options [Hash] options will be set before replotting @return [Terminal] self

# File lib/gnuplotrb/staff/terminal.rb, line 171
def replot(**options)
  set(options)
  stream_puts('replot')
  unset(options.keys)
  sleep 0.01 until File.size?(options[:output]) if options[:output]
  self
end
set(options) click to toggle source

Applie given options to current gnuplot instance.

For “{ opt1: val1, .. , optN: valN }” it will output to gnuplot

set opt1 val1
..
set optN valN

@param options [Hash] options to set @return [Terminal] self @example

set({term: ['qt', size: [100, 100]]})
#=> outputs to gnuplot: "set term qt size 100,100\n"
# File lib/gnuplotrb/staff/terminal.rb, line 116
def set(options)
  OptionHandling.validate_terminal_options(options)
  stream_puts(options_hash_to_string(options))
end
store_datablock(data) click to toggle source

Output datablock to this gnuplot terminal.

@param data [String] data stored in datablock @example

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
# File lib/gnuplotrb/staff/terminal.rb, line 73
def store_datablock(data)
  name = "$DATA#{@current_datablock += 1}"
  stream_puts "#{name} << EOD"
  stream_puts data
  stream_puts 'EOD'
  name
end
stream_print(command) click to toggle source

Just print command to gnuplot pipe. @param command [String] command to send @return [Terminal] self

# File lib/gnuplotrb/staff/terminal.rb, line 159
def stream_print(command)
  check_errors
  @in.print(command)
  self
end
stream_puts(command) click to toggle source

Just put command + “n” to gnuplot pipe. @param command [String] command to send @return [Terminal] self

# File lib/gnuplotrb/staff/terminal.rb, line 151
def stream_puts(command)
  stream_print("#{command}\n")
end
test(file_name = nil) click to toggle source

Plot test page into file with file_name (optional).

Test page contains possibilities of the term. @param file_name [String] filename to output image if needed

and chosen terminal supports image output

@return nil

# File lib/gnuplotrb/staff/terminal.rb, line 195
def test(file_name = nil)
  set(output: file_name) if file_name
  stream_puts('test')
  unset(:output)
  nil
end
unset(*options) click to toggle source

Unset options

@param *options [Sequence of Symbol] each symbol considered as option key @return [Terminal] self

# File lib/gnuplotrb/staff/terminal.rb, line 126
def unset(*options)
  options.flatten
         .sort_by { |key| OPTION_ORDER.find_index(key) || -1 }
         .each { |key| stream_puts "unset #{OptionHandling.string_key(key)}" }
  self
end