Class: TiogaUI

Inherits:
Object
  • Object
show all
Defined in:
lib/Tioga/tioga_ui_cmds.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, just_setup = false) ⇒ TiogaUI

Returns a new instance of TiogaUI.



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/Tioga/tioga_ui_cmds.rb', line 370

def initialize(args, just_setup=false)
  
  setup(args)
  
  return if just_setup
  
  if ($tioga_args.length == 0) || ($tioga_args[0] == '-h') || ($tioga_args[0] == '-help')
    show_help(nil,nil)
    return
  end
  
  if ($tioga_args[0] == '-v') || ($tioga_args[0] == '-version') || ($tioga_args[0] == '-V')
    show_version
    return
  end
  
  if $tioga_args.length > 0
    filename = fix_filename($tioga_args[0])
  end
  
  begin
  # currently, we are only supporting command line interface.
  # the code for making a Ruby/Tk interface can be enabled if you want to play with it.
  
  argnum = 1 # the filename is in $tioga_args[0]
  
  # do control commands
  while argnum < $tioga_args.length
    cmd = $tioga_args[argnum]
    argnum = argnum + 1
    if cmd == '-r'
      puts 'require ' + $tioga_args[argnum]
      require $tioga_args[argnum]
      argnum = argnum + 1
    elsif cmd == '-C'
      puts 'chdir to ' + $tioga_args[argnum]
      Dir.chdir($tioga_args[argnum])
      $change_working_directory = false
      argnum = argnum + 1
    elsif cmd == '-v'
      show_version
    else
      argnum = argnum - 1 # backup
      break
    end
  end
  
  setdir_and_load(filename)

  # do the figure command
  
  if argnum == $tioga_args.length
    view_pdf(require_pdf(0))
    return
  end
  
  cmd = $tioga_args[argnum]
  argnum = argnum + 1
  
  if /^-\d+$/ === cmd
    view_pdf(require_pdf(cmd[1..-1].to_i))
  elsif cmd == '-l'
    list_figures
  elsif cmd == '-h' || cmd == '-help'
    show_help(nil,nil)
  elsif (cmd == '-s' || cmd == '-m')
    if argnum == $tioga_args.length
      make_all_pdfs(cmd != '-m')
      return
    end
    do_fignums(parse_figs($tioga_args[argnum]),cmd == '-s')
  elsif cmd == '-p'
    if argnum == $tioga_args.length
      make_portfolio(true)
      return
    end
    make_portfolio(true, parse_figs($tioga_args[argnum]))
  #else # unrecognized command
  #  show_help(filename,cmd)
  end
rescue
end

end

Instance Method Details

#append_to_log(str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/Tioga/tioga_ui_cmds.rb', line 36

def append_to_log(str)
  if @no_Tk
    puts str
    return
  end
  return if @logText == nil
  return unless str.kind_of?String
  @logText.insert('end', str + "\n")
  @logText.see('end')
end

#check_have_loadedObject



48
49
50
51
52
# File 'lib/Tioga/tioga_ui_cmds.rb', line 48

def check_have_loaded
  return true if @have_loaded
  append_to_log "Must open a file first!"
  return false
end

#do_fignums(fignums, view) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/Tioga/tioga_ui_cmds.rb', line 351

def do_fignums(fignums,view)
  fignums.each do |n|
    pdf_name = require_pdf(n)
    if view
      view_pdf(pdf_name)
    else
      report_number_and_name(n,pdf_name)
    end
  end
end

#fix_filename(filename) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/Tioga/tioga_ui_cmds.rb', line 342

def fix_filename(filename)
  if filename != nil && filename[-3..-1] != '.rb' && 
      filename[-3..-1] != '.RB' && filename != '-help' && filename != '-v'
    filename += '.rb'
  end
  return filename
end

#fmObject



31
32
33
# File 'lib/Tioga/tioga_ui_cmds.rb', line 31

def fm
  FigureMaker.default
end

#list_figuresObject



363
364
365
366
367
# File 'lib/Tioga/tioga_ui_cmds.rb', line 363

def list_figures
  fm.figure_names.each_with_index do |name,i| 
    puts sprintf("%3i    %s\n",i,name)
  end
end

#loadfile(fname, reselect = true) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/Tioga/tioga_ui_cmds.rb', line 136

def loadfile(fname,reselect=true)
  @have_loaded = false
  fm.reset_state
  begin
    
    append_to_log "loading #{fname}\n"
    load(fname) # this should define the TiogaFigures class
    num_fig = fm.num_figures
    if num_fig == 0 
        raise "Failed to define any figures.  ' +
          'Remember to invoke 'new' for the class containing the figure definitions"
    end
    
    @title_name = fname.split('/')[-1]
    @title_name = @title_name[0..-4] if @title_name[-3..-1] == ".rb"
    fname = fname[0..-4] if fname[-3..-1] == ".rb"
    @pdf_name = fname + ".pdf"
    @have_loaded = true
    
    return if @no_Tk
    
    @root.title('Tioga:' + @title_name)
    @listBox.delete(0, 'end')
    fm.figure_names.each { |name| @listBox.insert('end', name) }
    set_selection(0) if reselect
    
  rescue Exception => er
    report_error(er, "ERROR: load failed for #{fname}\n")
  end
end

#make_all_pdfs(view = true, fignums = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/Tioga/tioga_ui_cmds.rb', line 66

def make_all_pdfs(view = true, fignums = nil)
  return unless check_have_loaded
  fm.make_all(fignums,false)
  if view == false
    if fignums == nil
      fm.figure_pdfs.each_with_index { |name,num| report_number_and_name(num,name) }
    else
      fignums.each { |num| report_number_and_name(num,fm.figure_pdfs[num]) }
    end
    return
  end
  if fignums == nil
    fm.num_figures.times { |i| view_pdf(fm.figure_pdfs[i]) }
  else
    fignums.each { |i| view_pdf(fm.figure_pdfs[i]) }
  end
end

#make_portfolio(view = true, fignums = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/Tioga/tioga_ui_cmds.rb', line 85

def make_portfolio(view = true, fignums = nil)
  return unless check_have_loaded
  name = @title_name + '_portfolio'
  make_all_pdfs(false,fignums)
  portfolio_name = fm.make_portfolio(name,fignums)
  return unless view
  view_pdf(portfolio_name)
  return if @no_Tk
  return unless $mac_command_key
  append_to_log "\nNote: Preview fails to make updated thumbnails after a Revert for a portfolio,"
  append_to_log "so for now you'll have to Close and redo Open as a workaround.\n"
end

#parse_figs(figs) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/Tioga/tioga_ui_cmds.rb', line 284

def parse_figs(figs)
  return [0] if (figs == nil) || (figs.length == 0)
  return figs unless (/^\d/ === figs) || (/^-\d/ === figs)
  ranges = figs.split(',')
  fignums = Array.new(fm.num_figures) {|i| i}
  result = []
  ranges.each do |r|
    nums = eval('fignums[' + r + ']') # do it this way in case r is negative
    if nums.kind_of?Integer
      result << nums
    else
      nums.each {|n| result << n}
    end
  end
  return result
end

#report_error(er, msg) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/Tioga/tioga_ui_cmds.rb', line 248

def report_error(er, msg)
    if msg != nil
        append_to_log msg
        append_to_log ""
    end
    append_to_log "    " + "#{er.message}" + "  [version: " + FigureMaker.version + "]"
    line_count = 0
    show_count = 0
    past_callers_routines = false
    in_callers_routines = false
    er.backtrace.each do |line|
        if (line.include?('Tioga/FigMkr.rb')) || (line.include?('Tioga/tioga_ui.rb'))
          if in_callers_routines
            past_callers_routines = true 
            in_callers_routines = false
          end
        else
          in_callers_routines = true
        end 
        if (show_count < fm.num_error_lines) and in_callers_routines
            append_to_log "    " + line
            show_count = show_count + 1
        end
        line_count = line_count + 1
    end
end

#report_number_and_name(num, name) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/Tioga/tioga_ui_cmds.rb', line 55

def report_number_and_name(num,name)
  if num < 10
    puts '  ' + num.to_s + '  ' + name
  elsif num < 100
    puts ' ' + num.to_s + '  ' + name
  else
    puts num.to_s + '  ' + name
  end
end

#require_pdf(arg) ⇒ Object

num is either figure number or name



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/Tioga/tioga_ui_cmds.rb', line 99

def require_pdf(arg) # num is either figure number or name
  begin
    num = arg
    num = fm.figure_names.index(num) unless num == nil || num.kind_of?(Integer)
    if fm.num_figures == 0
      puts "\nCan't build pdf because failed to define any figures."
      puts ''
      raise
    end
    if num == nil || num < 0 || num >= fm.num_figures
      puts "\n" + arg.to_s + ' is an invalid figure specification.'
      puts ''
      raise
    end
    result = fm.require_pdf(num)
    if result == nil
      puts "\nFailed during attempt to create pdf file."
      puts ''
      raise
    end
    #append_to_log result + "\n"
    return result
  end
end

#set_working_dir(filename) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/Tioga/tioga_ui_cmds.rb', line 168

def set_working_dir(filename)
  
  return filename unless $change_working_directory
  
  if (filename.length > 2) && (filename[0..1] == '~/')
    filename = ENV['HOME'] + filename[1..-1]
  elsif filename[0..0] != '/'
    # if necessary, add the current directory to the front of the filename
    filename = Dir.getwd + '/' + filename
  end
  
  parts = filename.split('/')
  if parts[-1].length < 2 || parts[-1][-2..-1] != "rb"
    append_to_log "ERROR: filename must have extension 'rb'   instead has <" + parts[-1][-2..-1] + ">"
    return nil
  end
  dir = ""
  parts[0..-2].each {|part| dir << '/' + part unless part.length == 0 }
  
  if dir != Dir.getwd
    append_to_log "changing working directory to " + dir
    Dir.chdir(dir) # change current working directory
  end
  
  return filename

end

#setdir_and_load(filename) ⇒ Object



276
277
278
279
280
281
# File 'lib/Tioga/tioga_ui_cmds.rb', line 276

def setdir_and_load(filename)
  filename = set_working_dir(filename)
  return nil if filename == nil
  loadfile(filename)
  return filename
end

#setup(args) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/Tioga/tioga_ui_cmds.rb', line 307

def setup(args)
  
  # set the standard defaults
  $tioga_args = Array.new(args.length) {|i| args[i]} # copy the args
  $change_working_directory = true
  if RbConfig::CONFIG["target"] =~ /darwin/i
    $pdf_viewer = "repreview"
    #$mac_command_key = true
  else
    $pdf_viewer = "xpdf"
    #$mac_command_key = false
  end
 
=begin     
  # Ruby/Tk defaults
  $geometry = '600x250+700+50'
  $background = 'WhiteSmoke'
  $mac_command_key = false
  $log_font = 'system 12'
  $figures_font = 'system 12'
=end
  
  tiogainit_names = %w[ .tiogarc .tiogainit ].map do |f|
    File.join(ENV['HOME'], f)
  end
  tiogainit_name = tiogainit_names.find {|f| test ?e, f}
  load(tiogainit_name) unless tiogainit_name.nil?
  
  @pdf_name = nil
  @have_loaded = false    
  @no_Tk = true

end

#show_help(filename, opt1) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/Tioga/tioga_ui_cmds.rb', line 197

def show_help(filename,opt1)
  unless opt1 == '-help' || filename == '-help' || filename == nil
    puts 'Sorry: ' + opt1 + ' is not a recognized option.' 
  end
  puts "\nThis program is a command line interface for the open-source tioga kernel."
  puts "The tioga kernel is for creating figures and plots using Ruby, PDF, and TeX."
  puts "Following is a brief description of the tioga command line options."
  puts "For more information, visit http://www.kitp.ucsb.edu/~paxton/tioga.html."
  puts "\nBefore any command line information is processed, tioga runs ~/.tiogainit if it exists."
  puts "    The primary use of this file is to set your default pdf viewer command (see below)."
  puts "\nIf there are no command line arguments, or the argument is -h, this help info is output."
  puts "\nOtherwise, the command line should start with a tioga file name (with extension .rb)."
  puts "     Since the extension is known, you can skip typing it if you like."
  puts "\nThe remainder of the command line should consist of an optional series of control commands"
  puts "    followed by a figure command."
  puts "\nAny control commands are done after ~/.tiogainit and before the figure file is loaded."
  puts "     -r file      runs the file (using Ruby's require method)."
  puts '     -C dir       changes the working directory.'
  puts '                  If there is no -C command, tioga changes the working directory to the'
  puts '                  location of the figure file .'
  puts '     -v           prints version information.'
  puts "\nThe figure command comes last and should be one of these:"
  puts "     -l           output a list of the defined figures by number and name."
  puts "     -<num>       make and show figure with index equal <num> (0 <= num < num_figures)."
  puts "     -m <figs>    make PDFs without showing them in the viewer."
  puts "     -s <figs>    make and show PDFs, each in a separate viewer window."
  puts "     -p <figs>    make PDFs and show the portfolio as a multi-page document."
  puts "\nIf the figure command is omitted, then it defaults to -0."
  puts "\nIf <figs> is omitted, then tioga does all the figures defined in the file"
  puts "     ordered by their definition index numbers."
  puts "\nOtherwise, <figs> must be either"
  puts "     a defined figure name (as supplied to def_figure in the tioga file), or"
  puts "     a valid ruby array index number for a figure (can be negative), or"
  puts "     a valid ruby range specification selecting a sequence of figures, or"
  puts "     a space-less, comma-separated list of figure indices and ranges."
  puts ''
  puts "     For example, -s Plot1 makes and shows the pdf for the figure named Plot1,"
  puts "     and -p 5,0..3,-1 makes a portfolio with the figure having index 5 on page 1,"
  puts "     followed by pages showing the figures with indices 0, 1, 2, 3, and -1."
  puts "\nThe viewer for showing PDFs is specified by the $pdf_viewer variable in tioga."
  puts "     The default value can be set by creating a .tiogainit file in your home directory."
  puts "     The .tiogainit file is run before any command line options are processed."
  puts "     Your current setting for $pdf_viewer is " + $pdf_viewer + '.'
  puts "     To change it, edit ~/.tiogainit to add the line $pdf_viewer = 'my viewer command'."
  puts "     The command tioga uses to show a pdf is $pdf_viewer + ' ' + full_PDF_filename."
  puts "     You can use the -e control command to try a different viewer setting"
  puts "     by doing the $pdf_viewer assignment from the command line."
  puts ''
end

#show_versionObject



302
303
304
# File 'lib/Tioga/tioga_ui_cmds.rb', line 302

def show_version
  puts FigureMaker.version
end

#view_pdf(pdf_file) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/Tioga/tioga_ui_cmds.rb', line 125

def view_pdf(pdf_file)
  if pdf_file == nil || pdf_file == false
    puts "\nERROR: invalid pdf file."
    puts ''
    raise
  end
  system($pdf_viewer + ' ' + pdf_file + ' > /dev/null')
  append_to_log pdf_file
end