#!/usr/bin/ruby -Ku ##-------------------------------------------------- # these lines allow the use of "source ranger ranger" # to change to the last directory after exiting ranger if false then =begin fi cd "`$1 --workaround $@ 3>&1 1>&2`" return =end end ##-------------------------------------------------- # Ranger version = '0.2.5' require 'pathname' MYDIR = File.dirname( Pathname.new( __FILE__ ).realpath ) $LOAD_PATH.unshift( MYDIR ) ##-------------------------------------------------- # parse arguments ARGV.shift(2) if ARGV.first == '--workaround' pwd = nil while arg = ARGV.shift case arg when '-h', '--help', '-help' puts "usage: ranger [options] [file/directory]" puts "options are:" puts " -h, --help show this message" puts " -v, --version print the version of ranger" puts " -d, --debug set the debug level to maximum" puts " -- stop option parsing" exit when '-v', '--version', '-version' puts "ranger #{version}" exit when '-d', '--debug', '-debug' arg_debug = true when '--' break else if ARGV.size > 0 abort "ranger: Unrecognized option: #{arg}" else pwd = arg end end end pwd ||= ARGV.last if pwd if pwd =~ /^file:\/\// pwd = $' end unless File.exists?(pwd) abort "ranger: No such file or directory: #{pwd}" end end ##-------------------------------------------------- # require files pattern = File.join( MYDIR, 'code', '**', '*.rb' ) extract = MYDIR.size+1 .. -4 for file in Dir.glob( pattern ) require file [ extract ] # keep the names like "code/action" end ##-------------------------------------------------- # default options opt = { :show_hidden => false, :sort => :name, :list_dir_first => true, :sort_reverse => false, :bookmark_file => '~/.ranger_bookmarks', :ascii_only => true, :wide_bar => true, :confirm_string => "yes I am!", :confirm => true, :file_preview => true, :preview => true, :search_method => :ctime, :mouse => true, :mouse_interval => 200, :debug_level => 0, :debug_file => '/tmp/errorlog', :colorscheme => 'default', :cd => true, :evil => false } ##-------------------------------------------------- # initialization class OptionClass < Struct.new(*opt.keys) def confirm_string confirm ? super : "" end end Option = OptionClass.new(*opt.values) opt = nil begin load File.exists?( x = File.expand_path( '~/.rangerrc' ) ) ? x : 'ranger.conf' rescue LoadError end begin path = File.join(MYDIR, 'data', 'colorscheme', Option.colorscheme + '.rb') load path rescue LoadError abort "Can't find colorscheme at #{path}" end if defined? arg_debug Option.debug_level = 3 end include Debug Debug.setup( :name => 'ranger', :file => Option.debug_file, :level => Option.debug_level ) ## run the file in arg1 and exit if pwd and !File.directory?(pwd) Fm.reload_types file = Directory::Entry.new(pwd) file.get_data Action.run(RunContext.new(file, 0, 'ca')) exit end include CLI Signal.trap(Scheduler::UPDATE_SIGNAL) do Fm.refresh end CLI.init_mouse( Option.mouse_interval ) ##-------------------------------------------------- # run the shit & clean up afterwards begin Fm.initialize( pwd ) Fm.main_loop ensure log "exiting!\n\n" closei if CLI.running? CLI.stop_mouse Fm.dump Fm.dump_pwd_to_3 if Option.cd rescue nil # Kill all other threads for thr in Thread.list unless thr == Thread.current thr.kill end end end