Module: VIBes

Defined in:
lib/vibes-rb.rb,
lib/vibes-rb/version.rb

Overview

The VIBes module handles communication with the VIBes viewer executable.

Defined Under Namespace

Classes: Figure

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Class Method Details

.begin_drawing(fname = nil) ⇒ self

Open communication with the VIBes viewer. If the viewer is not executing, forks to launch it in a new process.

Normaly the user do not have to call this function, it is automatically called when the first message is being sent to the viewer.

It is only useful when the user wants to save the commands in a specific file instead of sending them to the viewer. In this case, this method should be called before any figure method is called.

Examples:

VIBes.begin_drawing('titi.json')
f = VIBes::Figure.new
f.width = 500
f.height = 250
f.draw_box([-2, 3], [-1, 4], color: 'k[r]')
VIBes.end_drawing
puts File.read('titi.json')
# {"action":"new","figure":"default"}
# {"action":"set","figure":"default","properties":{"width":500}}
# {"action":"set","figure":"default","properties":{"height":500}}
# {"action":"draw","figure":"default","shape":{"format":"k[r]","type":"box","bounds":[-2.0,3.0,-1.0,4.0]}}

Parameters:

  • fname (String, nil) (defaults to: nil)

    name of the file used to exchange data with the viewer



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
# File 'lib/vibes-rb.rb', line 54

def self.begin_drawing(fname = nil)
  if fname.kind_of?(String) and not(fname.empty?) then
    @@vibes_channel.close if @@vibes_channel
    @@vibes_channel = File.open(fname, 'a')
  else
    user_dir = ENV['USERPROFILE'] # windows
    user_dir = ENV['HOME'] if user_dir.nil?
    if user_dir then # Environment variable found, connect to a file in user's profile directory
      fname = File.join(user_dir, '.vibes.json')
      unless File.exist?(fname) then
        Process.detach(fork{Process.exec('VIBes-viewer')})
        startTime = Time.now
        until File.exist?(fname) do
          sleep 0.1
          if (Time.now - startTime) > 1 then
            warn 'VIBes-viewer process does not seem to be started'
            break
          end
        end
      end
      begin_drawing(fname)
    else # Connect to a file in working directory
      begin_drawing('vibes.json')
    end
  end
  self
end

.end_drawingself

Close the communication channel with the viewer.

In most cases, users don't have to call this method. It is only useful when commands are saved in a specific file instead of being send to the VIBes viewer.

See Also:



89
90
91
92
93
94
95
# File 'lib/vibes-rb.rb', line 89

def self.end_drawing
  if @@vibes_channel then
    @@vibes_channel.close
    @@vibes_channel = nil
  end
  self
end