lib/bio/shell/core.rb in bio-0.7.1 vs lib/bio/shell/core.rb in bio-1.0.0
- old
+ new
@@ -1,46 +1,27 @@
#
# = bio/shell/core.rb - internal methods for the BioRuby shell
#
-# Copyright:: Copyright (C) 2005
-# Toshiaki Katayama <k@bioruby.org>
-# License:: LGPL
+# Copyright:: Copyright (C) 2005, 2006
+# Toshiaki Katayama <k@bioruby.org>
+# License:: Ruby's
#
-# $Id: core.rb,v 1.16 2005/12/07 10:54:23 k Exp $
+# $Id: core.rb,v 1.21 2006/02/27 09:09:57 k Exp $
#
-#--
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-#
-#++
-#
+
module Bio::Shell::Ghost
+ SAVEDIR = "session/"
CONFIG = "config"
OBJECT = "object"
HISTORY = "history"
SCRIPT = "script.rb"
PLUGIN = "plugin/"
+ DATADIR = "data/"
BIOFLAT = "bioflat/"
- SITEDIR = "/etc/bioinformatics/bioruby/"
- USERDIR = "#{ENV['HOME']}/.bioinformatics/bioruby/"
- SAVEDIR = ".bioruby/"
-
MARSHAL = [ Marshal::MAJOR_VERSION, Marshal::MINOR_VERSION ]
MESSAGE = "...BioRuby in the shell..."
ESC_SEQ = {
@@ -53,10 +34,18 @@
:c => "\e[36m", :cyan => "\e[36m",
:w => "\e[37m", :white => "\e[37m",
:n => "\e[00m", :none => "\e[00m", :reset => "\e[00m",
}
+ def history
+ SAVEDIR + HISTORY
+ end
+
+ def datadir
+ DATADIR
+ end
+
def esc_seq
ESC_SEQ
end
### save/restore the environment
@@ -74,52 +63,23 @@
attr_accessor :config
# A hash to store temporal (per session) configurations
attr_accessor :cache
- def load
+ def load_session
load_object
load_history
opening_splash
end
- def save
+ def save_session
closing_splash
- save_history
- save_object
- save_config
- end
-
- #--
- # *TODO* How to prevent terminal collapse and suppress loading messages?
- #++
- def load_thread
- message = ''
- begin
- t1 = Thread.new do
- require 'stringio'
- sio = StringIO.new('')
- begin
- stdout_save = STDOUT.clone
- STDOUT.reopen(sio)
- load_object
- load_history
- ensure
- STDOUT.reopen(stdout_save)
- stdout_save.close
- message = sio.read
- sio.close
- end
- end
- t2 = Thread.new do
- opening_splash
- end
- t1.join
- t2.join
- rescue
+ if create_save_dir_ask
+ #save_history # changed to use our own...
+ save_object
+ save_config
end
- puts message
end
### setup
def check_version
@@ -133,79 +93,87 @@
raise "Marshal version mismatch"
end
end
def create_save_dir
- dir = ask_save_dir
- create_real_dir(dir)
- create_real_dir(dir + PLUGIN)
- create_real_dir(dir + BIOFLAT)
- return dir
+ create_real_dir(SAVEDIR)
+ create_real_dir(DATADIR)
+ create_real_dir(PLUGIN)
end
- # 1. ask to save in SAVEDIR directory in the current directory
- # 2. otherwise save in USERDIR directory
- # 3. remember the choice in @cache[:savedir] once per session
- def ask_save_dir
- if @cache[:savedir]
- dir = @cache[:savedir]
- else
- dir = SAVEDIR
- if ! File.directory?(dir)
- loop do
- print "Save in \"#{dir}\" directory? [y/n]: "
- answer = gets
- if /^\s*[Yy]/.match(answer)
- break
- elsif /^\s*[Nn]/.match(answer)
- dir = USERDIR
- break
- end
- end
+ def create_save_dir_ask
+ if File.directory?(SAVEDIR)
+ @cache[:save] = true
+ end
+ if @cache[:save].nil?
+ if ask_yes_or_no("Save session in '#{SAVEDIR}' directory? [y/n] ")
+ create_real_dir(SAVEDIR)
+ create_real_dir(DATADIR)
+ create_real_dir(PLUGIN)
+# create_real_dir(BIOFLAT)
+ @cache[:save] = true
+ else
+ @cache[:save] = false
end
- @cache[:savedir] = dir
end
- return dir
+ return @cache[:save]
end
+ def ask_yes_or_no(message)
+ loop do
+ print "#{message}"
+ answer = gets
+ if answer.nil?
+ # readline support might be broken
+ return false
+ elsif /^\s*[Nn]/.match(answer)
+ return false
+ elsif /^\s*[Yy]/.match(answer)
+ return true
+ else
+ # loop
+ end
+ end
+ end
+
def create_real_dir(dir)
unless File.directory?(dir)
begin
print "Creating directory (#{dir}) ... "
Dir.mkdir(dir)
puts "done"
rescue
- warn "Error: Failed to create #{dir} : #{$!}"
+ warn "Error: Failed to create directory (#{dir}) : #{$!}"
end
end
end
### bioflat
def create_flat_dir(dbname)
- if prefix = create_save_dir
- return prefix + BIOFLAT + dbname.to_s.strip
- else
- return nil
+ dir = BIOFLAT + dbname.to_s.strip
+ unless File.directory?(BIOFLAT)
+ Dir.mkdir(BIOFLAT)
end
+ unless File.directory?(dir)
+ Dir.mkdir(dir)
+ end
+ return dir
end
def find_flat_dir(dbname)
- dir = SAVEDIR + BIOFLAT + dbname.to_s.strip
- dir = USERDIR + BIOFLAT + dbname.to_s.strip unless File.exists?(dir)
+ dir = BIOFLAT + dbname.to_s.strip
if File.exists?(dir)
return dir
else
return nil
end
end
### config
def load_config
- load_config_file(SITEDIR + CONFIG)
- load_config_file(USERDIR + CONFIG)
load_config_file(SAVEDIR + CONFIG)
end
def load_config_file(file)
if File.exists?(file)
@@ -216,12 +184,11 @@
puts "done"
end
end
def save_config
- dir = create_save_dir
- save_config_file(dir + CONFIG)
+ save_config_file(SAVEDIR + CONFIG)
end
def save_config_file(file)
begin
print "Saving config (#{file}) ... "
@@ -263,21 +230,27 @@
def config_pager(cmd = nil)
@config[:pager] = cmd
end
+ def config_splash
+ flag = ! @config[:splash]
+ @config[:splash] = flag
+ puts "Splash #{flag ? 'on' : 'off'}"
+ opening_splash
+ end
+
def config_message(str = nil)
str ||= MESSAGE
@config[:message] = str
+ opening_splash
end
### plugin
def load_plugin
- load_plugin_dir(SITEDIR + PLUGIN)
- load_plugin_dir(USERDIR + PLUGIN)
- load_plugin_dir(SAVEDIR + PLUGIN)
+ load_plugin_dir(PLUGIN)
end
def load_plugin_dir(dir)
if File.directory?(dir)
Dir.glob("#{dir}/*.rb").sort.each do |file|
@@ -289,12 +262,10 @@
end
### object
def load_object
- load_object_file(SITEDIR + OBJECT)
- load_object_file(USERDIR + OBJECT)
load_object_file(SAVEDIR + OBJECT)
end
def load_object_file(file)
if File.exists?(file)
@@ -316,12 +287,11 @@
puts "done"
end
end
def save_object
- dir = create_save_dir
- save_object_file(dir + OBJECT)
+ save_object_file(SAVEDIR + OBJECT)
end
def save_object_file(file)
begin
print "Saving object (#{file}) ... "
@@ -356,30 +326,29 @@
### history
def load_history
if @cache[:readline]
- load_history_file(SITEDIR + HISTORY)
- load_history_file(USERDIR + HISTORY)
load_history_file(SAVEDIR + HISTORY)
end
end
def load_history_file(file)
if File.exists?(file)
print "Loading history (#{file}) ... "
File.open(file).each do |line|
- Readline::HISTORY.push line.chomp
+ #Readline::HISTORY.push line.chomp
+ date, hist = line.chomp.split("\t")
+ Readline::HISTORY.push hist if hist
end
puts "done"
end
end
def save_history
if @cache[:readline]
- dir = create_save_dir
- save_history_file(dir + HISTORY)
+ save_history_file(SAVEDIR + HISTORY)
end
end
def save_history_file(file)
begin
@@ -426,25 +395,35 @@
@script_end = Readline::HISTORY.size - 2
end
def save_script
if @script_begin and @script_end and @script_begin <= @script_end
- dir = create_save_dir
- save_script_file(dir + SCRIPT)
+ if File.exists?(SCRIPT)
+ message = "Overwrite script file (#{SCRIPT})? [y/n] "
+ else
+ message = "Save script file (#{SCRIPT})? [y/n] "
+ end
+ if ask_yes_or_no(message)
+ save_script_file(SCRIPT)
+ else
+ puts " ... save aborted."
+ end
+ elsif @script_begin and @script_end and @script_begin - @script_end == 1
+ puts " ... script aborted."
else
- puts "Error: script range #{@script_begin}..#{@script_end} is invalid"
+ puts "Error: Script range #{@script_begin}..#{@script_end} is invalid"
end
end
def save_script_file(file)
begin
print "Saving script (#{file}) ... "
File.open(file, "w") do |f|
- f.print "#!/usr/bin/env ruby\n\n"
- f.print "require 'bioruby'\n\n"
+ f.puts "#!/usr/bin/env bioruby"
+ f.puts
f.puts Readline::HISTORY.to_a[@script_begin..@script_end]
- f.print "\n\n"
+ f.puts
end
puts "done"
rescue
@script_begin = nil
warn "Error: Failed to save (#{file}) : #{$!}"
@@ -463,13 +442,30 @@
ruby = ESC_SEQ[:ruby]
none = ESC_SEQ[:none]
return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
end
- def splash_message_action
- s = splash_message
+ 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
+
+ def splash_message_action_color(message = nil)
+ s = message || splash_message
+ l = s.length
c = ESC_SEQ
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"
@@ -482,31 +478,40 @@
sleep(0.008)
end
end
def opening_splash
- print "\n"
- if @config[:color]
- splash_message_action
+ puts
+ if @config[:splash]
+ if @config[:color]
+ splash_message_action_color
+ else
+ splash_message_action
+ end
end
if @config[:color]
print splash_message_color
else
print splash_message
end
- print "\n\n"
+ puts
+ puts
print " Version : BioRuby #{Bio::BIORUBY_VERSION.join(".")}"
- print " / Ruby #{RUBY_VERSION}\n\n"
+ print " / Ruby #{RUBY_VERSION}"
+ puts
+ puts
end
def closing_splash
- print "\n\n"
+ puts
+ puts
if @config[:color]
print splash_message_color
else
print splash_message
end
- print "\n\n"
+ puts
+ puts
end
end