Saturday, September 1, 2007

scratch file

To save my the trouble of screwing with SCP/SMB to continue work tomorrow from another computer...


Heres a scratch file I've been toying with tonight. For the heck of it I thought I'd toy around with trying to break a $PATH like string apart into some thing a program or file could be looked up and then try to add a primitive file name completion to after tinkering awhile.

Todo tomorrow:

clean up comments, finish playing with the Abbrev class, toy with trying to match up a regex to an abbrev to a filename, worry about rest afterwards.

#!/usr/local/bin/ruby -w
# Build a hash table from a $PATH like string that we can use for calling
# commands.

require 'abbrev'
require 'pp'

PDEBUG=1  # Programmers DEBUG on

path = '/bin:/sbin:/usr/bin:/usr/sbin'

# Create a hash for $PATH
#
# Arguments:
#   path => string containing a list of directories delimited by separator
#   separator => Any delimiter suitable for String#each
#
# Return value:
#   A hash where keys are directory names and values are handles to those
#   directories.
def make_pathhash( path, separator=':' )

  hash = Hash.new()

  # Parse path for directories
  path.each( separator ) do |dir|
    dir.gsub!( /:/, '' )
    # Creates a key from path containing a directory name
    hash[dir] = dir #Dir.open( dir ) 
  end

  return( hash )

end

# Create a hash from the current directory to assist filename completion
#
# Arguments:
#   directory => A directory to create the hash from
#
# Return value:
#   An hash of filename => filenames from directory
#
def make_chdirhash( directory )

  hash = Hash.new()

  dirh = Dir.open( directory )
  dirh.each do |file|
    hash[file] = file
  end

  return( hash )

end

if PDEBUG 
  h = make_pathhash( path )
  cd = make_chdirhash( '.' )
  pp Abbrev::abbrev( cd.to_a.flatten ).sort  
end

Not bad for an idiot just screwing around if you ask me.



Also to do tomorrow, finish that thing I was doing before dinner.

No comments:

Post a Comment