#!/usr/bin/env ruby ################################## ## CHANGE THESE LINES AS NEEDED ## ################################## $DFDIR = "/Users/hinmanm/.zg" # $ZGCONF = $DFDIR + "/zg.conf" # $FINDCMD = "find" # ################################## class ScanDir attr_reader :name, :rtime, :glob def initialize(name,rtime,glob) @name = name @rtime = rtime.to_i @glob = glob end end def read_configuration(filename) @@conf = [] if File.exist?(filename) #puts "Reading configuration from #{filename}..." File.new(filename).readlines.each { |line| (d,rtime,glob) = line.chomp.split(":",3) @@conf.push(ScanDir.new(d,rtime,glob)) } else STDERR.puts "Configuration file #{filename} does not exist." exit(0) end end def rescan_directory(dfname,dirname,glob) STDERR.puts "rescanning #{dirname}..." #puts "#{$FINDCMD} #{dirname}/ -name '#{glob}' -type d -print 1>#{dfname} 2>/dev/null" `#{$FINDCMD} #{dirname}/ -name "#{glob}" -type d -print 1>#{dfname} 2>/dev/null` #puts "done." end def get_directory_path(dirname) results = [] #puts "Searching paths for '#{dirname}'..." @@conf.each { |sd| dfname = String.new(sd.name.gsub("~","")) dfname.gsub!("/","") dfname = $DFDIR + "/" + dfname + ".dat" #puts "datafilename: #{dfname}" if !File.exist?(dfname) ## If we don't have a datafile, create one #puts "datafile: #{dfname} does not exist, rescanning..." rescan_directory(dfname,sd.name,sd.glob) else ## If the datafile is too old, recreate it ctime = File.ctime(dfname) if ((Time.now - ctime) > (60*sd.rtime)) #puts "time limit exceeded, rescanning..." rescan_directory(dfname,sd.name,sd.glob) end end File.new(dfname).readlines.each { |line| if line =~ /#{dirname}[^\/]$/ #puts "Found #{line.chomp}..." results.push(line.chomp.gsub!(/\/\//,"/")) end } } return results end if ARGV.length < 1 STDERR.puts "Usage:" STDERR.puts " zg [--rescan] " puts "." exit(0) end read_configuration($ZGCONF) if ARGV.to_s =~ /--rescan/ @@conf.each { |sd| dfname = String.new(sd.name.gsub("~","")) dfname.gsub!("/","") dfname = $DFDIR + "/" + dfname + ".dat" rescan_directory(dfname,sd.name,sd.glob) } puts "." exit(0) end dirs = get_directory_path(ARGV[0]) if dirs.length == 0 STDERR.puts "No directories found" puts "." exit(0) end ## If there's only 1 result, we know what to do if dirs.length == 1 ## Move directly to "Go", collect $200 STDERR.puts "Zooming directly to #{dirs[0]}..." puts dirs[0] exit(0) end STDERR.puts "#{dirs.length} directories were returned." i = 0 dirs.each { |d| i = i + 1 STDERR.puts " (#{i})\t#{d}" } begin c = IO.select([STDIN],nil,nil)[0][0].read(1).to_i c = 0 if c < 0 puts dirs[c-1] rescue Interrupt puts "." rescue STDERR.puts "Error!" exit(0) end