Skip to content
This repository
Browse code

Added shortcut key (s) to save SVG to file. Very simple, no existing …

…file protections.
  • Loading branch information...
commit cff507c9a61c30b52cc2d94e6f48cc6716656490 1 parent c79b4b7
John Woods mohawkjohn authored

Showing 1 changed file with 25 additions and 2 deletions. Show diff stats Hide diff stats

  1. +25 2 lib/sciruby/plotter.rb
27 lib/sciruby/plotter.rb
@@ -29,6 +29,15 @@ def initialize script_or_handle
29 29 r = rect 10, 10, handle.width+2, handle.height+2
30 30 img = image(:data => handle).tap { |i| i.move(11, 11) }
31 31
  32 + keypress do |key|
  33 + if key == 's'
  34 + file = ask_save_file
  35 + File.open(file, "w+") do |f|
  36 + f.write(SciRuby::Plotter.create_svg(script_or_handle))
  37 + end
  38 + end
  39 + end
  40 +
32 41 # If a script was provided, watch it for updates
33 42 every(2) do
34 43 new_time = File.mtime(script_or_handle)
@@ -84,11 +93,25 @@ def eval_script
84 93 end
85 94
86 95 class << self
87   - # Render an SVG into memory from the watched file / editor.
  96 + # Render an SVG into memory from the watched file / editor, returning a handle.
88 97 def create_handle filename, script=nil
  98 + svg = create_svg filename, script
  99 + begin
  100 + RSVG::Handle.new_from_data(svg).tap { |s| s.close }
  101 + rescue RSVG::Error => e
  102 + STDERR.puts "There appears to be a mysterious problem with the SVG output for your plot. Storing debug output in debug.svg."
  103 + STDERR.puts "Make sure your data() call is in the right place."
  104 + File.open("debug.svg", "w") { |f| f.puts svg }
  105 + raise e
  106 + end
  107 + end
  108 +
  109 + # Render an SVG, returning the file contents (not written).
  110 + def create_svg filename, script=nil
89 111 vis = Interpreter.new(filename, script).eval_script
90   - RSVG::Handle.new_from_data(vis.to_svg).tap { |s| s.close }
  112 + svg = vis.to_svg
91 113 end
  114 +
92 115
93 116 # Clean a trace so only the relevant information is included.
94 117 def clean_trace bt, script_filename

0 comments on commit cff507c

Please sign in to comment.
Something went wrong with that request. Please try again.