bin/diru in diru-0.0.5 vs bin/diru in diru-0.0.6

- old
+ new

@@ -79,10 +79,11 @@ [ :switch, 'list', '-l', "S: List servers." ], [ :opt_single, 'options', '-o', "SC: Options File." ], [ :opt_single, 'port', '-p', "SC: Server port for client (default: DIRU_PORT or ~/.diru.prt)." ], [ :switch, 'change', '-c', "C: Change dir (i.e. target is to change dir)." ], [ :exclusive, 'template', '-t', "C: Display Options File template." ], + [ :exclusive, 'cmddoc', '-d', "C: Display command documentation." ], [ :default, nil, nil, "C: Client commands (try: diru i)." ], ] ) @@ -129,15 +130,17 @@ # List the dir content (default: pwd). def Diru.list( dir = '.', glob = '*' ) Dir.glob( "#{dir}/#{glob}" ) end + # List the dir files (default: pwd). def Diru.list_files( dir = '.', glob = '*' ) Diru.list( dir, glob ).select do |i| File.file?(i) == true end end + # Find file entry from directory hierarchy upwards. def Diru.find_upper_file( file, dir = Dir.pwd ) found = Diru.list_files( dir, file ) if found.empty? dir = File.dirname( dir ) @@ -274,11 +277,11 @@ # Add bookmark (if not already). def abook( dir ) idx = @book.index dir unless idx - @book.push dir + @book.unshift dir end end # Delete bookmark. @@ -790,10 +793,63 @@ } exit( false ) end +# Keep in sync with README. +if Opt['cmddoc'].given + STDERR.puts %q{ +Search commands: + +* "dr r" - change to Project root dir. +* "dr r <dir>" - change to <dir> (somewhere) under Project root dir + (glob). +* "dr t <dir>" - change to <dir> (somewhere) under current dir (glob). +* "dr e <dir>" - change to <dir> (somewhere) under Project root dir + (regexp). + +Bookmark commands: + +* "dr b" - display bookmarks. +* "dr b ." - add current dir to bookmarks. +* "dr b !" - reset (clear) bookmarks. +* "dr b s <file>" - store bookmarks to <file>. +* "dr b l <file>" - load bookmarks from <file>. +* "dr b d <num>" - delete bookmark with <num>. +* "dr b <num>" - change dir to bookmark <num>. + +History commands: + +* "dr h" - display history. +* "dr h ." - add current dir to history. +* "dr h !" - reset (clear) history. +* "dr h ," - reference last history item. +* "dr h <num>" - change dir to history <num>. + +Scratch Pad commands: + +* "dr s ." - store current dir to Scratch Pad. +* "dr s <dir>" - store <dir> to Scratch Pad. +* "dr s" - change dir to Scratch Pad dir. + +Misc commands: + +* "dr p" - jump to peer dir, i.e. the peer of current (from options). +* "dr c <cmd>" - issue RPC command to server (reset etc., see below). +* "dr f" - list favorites dirs (from options). +* "dr f <dir>" - change dir to favorite <dir>. +* "dr d m <dir>" - make directory. +* "dr d r <dir>" - remove directory. +* "dr i" - show command info. +* "dr <dir>" - change to given dir (must be under current). +* "dr" - change to next "Left-over" directory. + +} + exit( false ) +end + + hport = nil if Opt['hport'].given hport = Opt['hport'].value.to_i else hport = Diru::DIRU_HUB_PORT @@ -927,25 +983,43 @@ class Client include Diru + attr_reader :pwd + attr_accessor :enable_out + def initialize( port ) @port = port # Create a DRbObject instance that is connected to server. All methods # executed on this object will be executed to the remote one. @search = DRbObject.new( nil, "druby://localhost:#{@port}" ) load_conf( @search.opts_file ) + + @pwd = Dir.pwd + + @enable_out = true + end # Perform cd (i.e. return true) and store current dir to history. - def do_cd - @search.ahist( Dir.pwd ) + def do_cd( dir ) + + # Store current to history before change. + @search.ahist( @pwd ) + + # Local copy of new dir for command-sequence mode. + @pwd = dir + + # Directory info for shell, unless disabled for + # command-sequence mode. + puts dir if @enable_out + true end # No directory change, i.e. return false. @@ -958,12 +1032,11 @@ def command( input ) if input.empty? # Refer to last search list. - puts @search.next - do_cd + do_cd @search.next else cmd = input[0] arg = input[1..-1] @@ -977,29 +1050,29 @@ # Decode user command. ret = \ case cmd - when '/', 'r'; + when 'r'; if arg.empty? disp @search.root else disp @search.match( nil, "*"+arg.join('*')+"*" ) end - when ':', 't'; disp @search.match( Dir.pwd, "*"+arg.join('*')+"*" ) + when 't'; disp @search.match( @pwd, "*"+arg.join('*')+"*" ) when 'e'; disp @search.rematch( nil, arg.join('.*') ) when 'q'; disp @search.rematch( nil, arg.join('.*') ) - when '.', 'b'; + when 'b'; if arg[0] == nil lbook( @search.book ) no_cd elsif arg[0] == '.' - @search.abook( Dir.pwd ) + @search.abook( @pwd ) no_cd elsif arg[0] == '!' @search.rbook no_cd elsif arg[0] == 's' @@ -1013,44 +1086,44 @@ no_cd else disp @search.gbook( arg[0].to_i ) end - when ',', 'h'; + when 'h'; if arg[0] == nil lbook( @search.hist ) no_cd elsif arg[0] == '.' - @search.ahist( Dir.pwd ) + @search.ahist( @pwd ) no_cd elsif arg[0] == '!' @search.rhist no_cd elsif arg[0] == ',' disp @search.ghist( 0 ) else disp @search.ghist( arg[0].to_i ) end - when '_', 's'; + when 's'; if arg[0] == nil disp @search.gettmp elsif arg[0] if arg[0] == '.' - dir = Dir.pwd + dir = @pwd else dir = arg[0] end disp @search.settmp( dir ) no_cd else no_cd end - when '=', 'p'; disp peer + when 'p'; disp peer - when '@', 'c'; rpc( arg ) + when 'c'; rpc( arg ) when 'f'; if arg[0] == nil @search.fav.each do |k,v| STDERR.puts format( "%-6s %s", k, v ) @@ -1091,19 +1164,19 @@ else if Opt['change'].given all = [cmd] + arg - puts "#{all.join(' ')}" - do_cd + do_cd "#{all.join(' ')}" else no_cd end end - exit( ret ) + # exit( ret ) + return ret end end @@ -1111,24 +1184,22 @@ def disp( resp ) if resp == nil no_cd elsif resp.kind_of? Array if resp.any? - puts resp[0] if resp.length > 1 resp[1..-1].each do |i| STDERR.puts i end end - do_cd + do_cd resp[0] else Diru.warn "Dir not found!" no_cd end else - puts resp - do_cd + do_cd resp end end # Display directories without CD. @@ -1150,11 +1221,12 @@ # Get peer directory. Search list of re-pairs. Match either of the # pair, and switch to the pair dir. # def peer - cur = Dir.pwd + # cur = Dir.pwd + cur = @pwd peers = @conf[ :peers ] peers.each do |pair| re = Regexp.new( pair[0] ) if ( re.match( cur ) ) return cur.sub( re, pair[1] ) @@ -1165,27 +1237,27 @@ # Display short command doc. def doc STDERR.puts " - r / - search from root (or to root) - t : - search from this (current) - e - search from root (with regexp) - q - query from root (with regexp) - b . - bookmark access - h , - history access - s _ - scratch pad access - p = - peer of current - c @ - command (reset, doc etc.) - f - favorites - d - directory mutation - i - short info + r - search from root (or to root) + t - search from this (current) + e - search from root (with regexp) + q - query from root (with regexp) + b - bookmark access + h - history access + s - scratch pad access + p - peer of current + c - command (reset, doc etc.) + f - favorites + d - directory mutation + i - short info " end - # List bookmarks with index number (in reverse order). + # List bookmarks with index number, in reverse order. def lbook( book ) idx = book.length-1 (0..idx).to_a.reverse.each do |i| STDERR.puts format( "%2d: %s", i, book[i] ) end @@ -1198,11 +1270,11 @@ # shell> dr @ rbook # def rpc( arg ) case arg[0] when 'reset'; @search.reset - when 'abook'; @search.abook( Dir.pwd ) + when 'abook'; @search.abook( @pwd ) when 'lbook'; lbook( @search.book ) when 'rbook'; @search.rbook when 'doc'; doc when 'sync'; @search.update_opts_sync when 'dsync'; @search.update_data_sync @@ -1215,11 +1287,10 @@ # Direct directory jump. def fav_map( arg ) ret = @search.gfav( arg[0] ) if ret disp ret - do_cd else no_cd end end @@ -1245,14 +1316,44 @@ rescue Diru.error "Server not available!" exit( false ) end + +# Collect command-sequence (if any). +cmds = [] +si = 0 +ei = 0 +while ( ei = input.index "," ) + cmds.push input[si...ei] + si = ei+1 + input = input[si..-1] +end + +# Add the tail command, i.e. the only if no "+" used. +cmds.push input + ret = false -begin - ret = client.command( input ) -rescue - Diru.error "Command failure!" - exit( false ) + + +# Disable shell output for all dir changes except last. +if cmds.length > 1 + client.enable_out = false +end + +cmds.each_with_index do |cmd, i| + + if i == cmds.length-1 + # Enable output for shell for the last dir change. + client.enable_out = true + end + + begin + ret = client.command( cmd ) + rescue + Diru.error "Command failure!" + exit( false ) + end + end exit( ret )