Module: Bio::Shell::Ghost
- Includes:
- Core
- Included in:
- Bio::Shell
- Defined in:
- lib/bio/shell/core.rb
Constant Summary
collapse
- Readline =
::Reline
Constants included
from Core
Core::BIOFLAT, Core::CONFIG, Core::DATADIR, Core::ESC_SEQ, Core::HISTORY, Core::MARSHAL, Core::MESSAGE, Core::OBJECT, Core::PLUGIN, Core::SCRIPT, Core::SESSION, Core::SHELLDIR
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Core
#ask_yes_or_no, #bioflat_dir, #colors, #config_file, #data_dir, #history_file, #object_file, #plugin_dir, #script_dir, #script_file, #session_dir, #shell_dir
Instance Attribute Details
A hash to store temporal (per session) configurations
124
125
126
|
# File 'lib/bio/shell/core.rb', line 124
def cache
@cache
end
|
A hash to store persistent configurations
121
122
123
|
# File 'lib/bio/shell/core.rb', line 121
def config
@config
end
|
Instance Method Details
#check_marshal ⇒ Object
312
313
314
315
316
|
# File 'lib/bio/shell/core.rb', line 312
def check_marshal
if @config[:marshal] and @config[:marshal] != MARSHAL
raise "Marshal version mismatch"
end
end
|
#close_history ⇒ Object
395
396
397
398
399
400
401
|
# File 'lib/bio/shell/core.rb', line 395
def close_history
if @cache[:histfile]
STDERR.print "Saving history (#{history_file}) ... "
@cache[:histfile].close
STDERR.puts "done"
end
end
|
#closing_splash ⇒ Object
579
580
581
582
583
584
585
586
587
588
589
|
# File 'lib/bio/shell/core.rb', line 579
def closing_splash
STDERR.puts
STDERR.puts
if @config[:color]
STDERR.print splash_message_color
else
STDERR.print splash_message
end
STDERR.puts
STDERR.puts
end
|
#config_color ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/bio/shell/core.rb', line 264
def config_color
bind = Bio::Shell.cache[:binding]
flag = ! @config[:color]
@config[:color] = flag
if flag
IRB.conf[:PROMPT_MODE] = :BIORUBY_COLOR
eval("conf.prompt_mode = :BIORUBY_COLOR", bind)
else
IRB.conf[:PROMPT_MODE] = :BIORUBY
eval("conf.prompt_mode = :BIORUBY", bind)
end
end
|
#config_echo ⇒ Object
256
257
258
259
260
261
262
|
# File 'lib/bio/shell/core.rb', line 256
def config_echo
bind = Bio::Shell.cache[:binding]
flag = ! @config[:echo]
@config[:echo] = IRB.conf[:ECHO] = flag
eval("conf.echo = #{flag}", bind)
STDERR.puts "Echo #{flag ? 'on' : 'off'}"
end
|
#config_message(str = nil) ⇒ Object
288
289
290
291
292
|
# File 'lib/bio/shell/core.rb', line 288
def config_message(str = nil)
str ||= MESSAGE
@config[:message] = str
opening_splash
end
|
277
278
279
|
# File 'lib/bio/shell/core.rb', line 277
def (cmd = nil)
@config[:pager] = cmd
end
|
#config_show ⇒ Object
250
251
252
253
254
|
# File 'lib/bio/shell/core.rb', line 250
def config_show
@config.each do |k, v|
STDERR.puts "#{k}\t= #{v.inspect}"
end
end
|
#config_splash ⇒ Object
281
282
283
284
285
286
|
# File 'lib/bio/shell/core.rb', line 281
def config_splash
flag = ! @config[:splash]
@config[:splash] = flag
STDERR.puts "Splash #{flag ? 'on' : 'off'}"
opening_splash
end
|
save/restore the environment
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/bio/shell/core.rb', line 128
def configure(savedir)
@config = {}
@cache = {
:savedir => savedir,
:workdir => Dir.pwd,
}
create_save_dir
load_config
load_plugin
end
|
#create_flat_dir(dbname) ⇒ Object
201
202
203
204
205
206
207
|
# File 'lib/bio/shell/core.rb', line 201
def create_flat_dir(dbname)
dir = File.join(bioflat_dir, dbname.to_s.strip)
unless File.directory?(dir)
FileUtils.makedirs(dir)
end
return dir
end
|
#create_real_dir(dir) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/bio/shell/core.rb', line 187
def create_real_dir(dir)
unless File.directory?(dir)
begin
STDERR.print "Creating directory (#{dir}) ... "
FileUtils.makedirs(dir)
STDERR.puts "done"
rescue
warn "Error: Failed to create directory (#{dir}) : #{$!}"
end
end
end
|
#find_flat_dir(dbname) ⇒ Object
209
210
211
212
213
214
215
216
|
# File 'lib/bio/shell/core.rb', line 209
def find_flat_dir(dbname)
dir = File.join(bioflat_dir, dbname.to_s.strip)
if File.exist?(dir)
return dir
else
return nil
end
end
|
#load_config_file(file) ⇒ Object
224
225
226
227
228
229
230
231
232
|
# File 'lib/bio/shell/core.rb', line 224
def load_config_file(file)
if File.exist?(file)
STDERR.print "Loading config (#{file}) ... "
if hash = YAML.load(File.read(file))
@config.update(hash)
end
STDERR.puts "done"
end
end
|
#load_history_file(file) ⇒ Object
409
410
411
412
413
414
415
416
417
418
419
|
# File 'lib/bio/shell/core.rb', line 409
def load_history_file(file)
if File.exist?(file)
STDERR.print "Loading history (#{file}) ... "
File.open(file).each do |line|
unless line[/^# /]
Readline::HISTORY.push line.chomp
end
end
STDERR.puts "done"
end
end
|
#load_object_file(file) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/bio/shell/core.rb', line 327
def load_object_file(file)
if File.exist?(file)
STDERR.print "Loading object (#{file}) ... "
begin
bind = Bio::Shell.cache[:binding]
hash = Marshal.load(File.read(file))
hash.each do |k, v|
begin
Thread.current[:restore_value] = v
eval("#{k} = Thread.current[:restore_value]", bind)
rescue
STDERR.puts "Warning: object '#{k}' couldn't be loaded : #{$!}"
end
end
rescue
warn "Error: Failed to load (#{file}) : #{$!}"
end
STDERR.puts "done"
end
end
|
#load_plugin_dir(dir) ⇒ Object
300
301
302
303
304
305
306
307
308
|
# File 'lib/bio/shell/core.rb', line 300
def load_plugin_dir(dir)
if File.directory?(dir)
Dir.glob("#{dir}/*.rb").sort.each do |file|
STDERR.print "Loading plugin (#{file}) ... "
load file
STDERR.puts "done"
end
end
end
|
#open_history ⇒ Object
385
386
387
388
|
# File 'lib/bio/shell/core.rb', line 385
def open_history
@cache[:histfile] = File.open(history_file, "a")
@cache[:histfile].sync = true
end
|
#opening_splash ⇒ Object
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
# File 'lib/bio/shell/core.rb', line 557
def opening_splash
STDERR.puts
if @config[:splash]
if @config[:color]
splash_message_action_color
else
splash_message_action
end
end
if @config[:color]
STDERR.print splash_message_color
else
STDERR.print splash_message
end
STDERR.puts
STDERR.puts
STDERR.print " Version : BioRuby #{Bio::BIORUBY_VERSION_ID}"
STDERR.print " / Ruby #{RUBY_VERSION}"
STDERR.puts
STDERR.puts
end
|
#save_config_file(file) ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/bio/shell/core.rb', line 238
def save_config_file(file)
begin
STDERR.print "Saving config (#{file}) ... "
File.open(file, "w") do |f|
f.puts @config.to_yaml
end
STDERR.puts "done"
rescue
warn "Error: Failed to save (#{file}) : #{$!}"
end
end
|
#save_history ⇒ Object
not used (use open_history/close_history instead)
#save_history_file(file) ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
|
# File 'lib/bio/shell/core.rb', line 428
def save_history_file(file)
begin
STDERR.print "Saving history (#{file}) ... "
File.open(file, "w") do |f|
f.puts Readline::HISTORY.to_a
end
STDERR.puts "done"
rescue
warn "Error: Failed to save (#{file}) : #{$!}"
end
end
|
#save_object_file(file) ⇒ Object
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/bio/shell/core.rb', line 352
def save_object_file(file)
begin
STDERR.print "Saving object (#{file}) ... "
File.rename(file, "#{file}.old") if File.exist?(file)
File.open(file, "w") do |f|
bind = Bio::Shell.cache[:binding]
list = eval("local_variables", bind)
list.collect! { |x| x.to_s }
list -= ["_"]
hash = {}
list.each do |elem|
value = eval(elem, bind)
if value
begin
Marshal.dump(value)
hash[elem] = value
rescue
end
end
end
Marshal.dump(hash, f)
@config[:marshal] = MARSHAL
end
STDERR.puts "done"
rescue
File.rename("#{file}.old", file) if File.exist?("#{file}.old")
warn "Error: Failed to save (#{file}) : #{$!}"
end
end
|
#save_script ⇒ Object
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
# File 'lib/bio/shell/core.rb', line 473
def save_script
if @script_begin and @script_end and @script_begin <= @script_end
if File.exist?(script_file)
message = "Overwrite script file (#{script_file})? [y/n] "
else
message = "Save script file (#{script_file})? [y/n] "
end
if ask_yes_or_no(message)
save_script_file(script_file)
else
STDERR.puts " ... save aborted."
end
elsif @script_begin and @script_end and @script_begin - @script_end == 1
STDERR.puts " ... script aborted."
else
STDERR.puts "Error: Script range #{@script_begin}..#{@script_end} is invalid"
end
end
|
#save_script_file(file) ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
# File 'lib/bio/shell/core.rb', line 492
def save_script_file(file)
begin
STDERR.print "Saving script (#{file}) ... "
File.open(file, "w") do |f|
f.puts "#!/usr/bin/env bioruby"
f.puts
f.puts Readline::HISTORY.to_a[@script_begin..@script_end]
f.puts
end
STDERR.puts "done"
rescue
@script_begin = nil
warn "Error: Failed to save (#{file}) : #{$!}"
end
end
|
#script(mode = nil) ⇒ Object
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
# File 'lib/bio/shell/core.rb', line 442
def script(mode = nil)
case mode
when :begin, "begin", :start, "start"
@cache[:script] = true
script_begin
when :end, "end", :stop, "stop"
@cache[:script] = false
script_end
save_script
else
if @cache[:script]
@cache[:script] = false
script_end
save_script
else
@cache[:script] = true
script_begin
end
end
end
|
#script_begin ⇒ Object
463
464
465
466
|
# File 'lib/bio/shell/core.rb', line 463
def script_begin
STDERR.puts "-- 8< -- 8< -- 8< -- Script -- 8< -- 8< -- 8< --"
@script_begin = Readline::HISTORY.size
end
|
#script_end ⇒ Object
468
469
470
471
|
# File 'lib/bio/shell/core.rb', line 468
def script_end
STDERR.puts "-- >8 -- >8 -- >8 -- Script -- >8 -- >8 -- >8 --"
@script_end = Readline::HISTORY.size - 2
end
|
#splash_message ⇒ Object
510
511
512
513
|
# File 'lib/bio/shell/core.rb', line 510
def splash_message
@config[:message] ||= MESSAGE
@config[:message].to_s.split(//).join(" ")
end
|
#splash_message_action(message = nil) ⇒ Object
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
# File 'lib/bio/shell/core.rb', line 522
def splash_message_action(message = nil)
s = message || splash_message
l = s.length
x = " "
0.step(l,2) do |i|
l1 = l-i; l2 = l1/2; l4 = l2/2
STDERR.print "#{s[0,i]}#{x*l1}#{s[i,1]}\r"
sleep(0.001)
STDERR.print "#{s[0,i]}#{x*l2}#{s[i,1]}#{x*(l1-l2)}\r"
sleep(0.002)
STDERR.print "#{s[0,i]}#{x*l4}#{s[i,1]}#{x*(l2-l4)}\r"
sleep(0.004)
STDERR.print "#{s[0,i+1]}#{x*l4}\r"
sleep(0.008)
end
end
|
#splash_message_action_color(message = nil) ⇒ Object
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
# File 'lib/bio/shell/core.rb', line 539
def splash_message_action_color(message = nil)
s = message || splash_message
l = s.length
c = colors
x = " "
0.step(l,2) do |i|
l1 = l-i; l2 = l1/2; l4 = l2/2
STDERR.print "#{c[:n]}#{s[0,i]}#{x*l1}#{c[:y]}#{s[i,1]}\r"
sleep(0.001)
STDERR.print "#{c[:n]}#{s[0,i]}#{x*l2}#{c[:g]}#{s[i,1]}#{x*(l1-l2)}\r"
sleep(0.002)
STDERR.print "#{c[:n]}#{s[0,i]}#{x*l4}#{c[:r]}#{s[i,1]}#{x*(l2-l4)}\r"
sleep(0.004)
STDERR.print "#{c[:n]}#{s[0,i+1]}#{x*l4}\r"
sleep(0.008)
end
end
|
#splash_message_color ⇒ Object
515
516
517
518
519
520
|
# File 'lib/bio/shell/core.rb', line 515
def splash_message_color
str = splash_message
ruby = colors[:ruby]
none = colors[:none]
return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
end
|
#store_history(line) ⇒ Object
390
391
392
393
|
# File 'lib/bio/shell/core.rb', line 390
def store_history(line)
Bio::Shell.cache[:histfile].puts "# #{Time.now}"
Bio::Shell.cache[:histfile].puts line
end
|