module Main::Util::Methods

Public Instance Methods

columnize(buf, opts = {}) click to toggle source
# File lib/main/util.rb, line 40
def columnize buf, opts = {}
  width = Util.getopt 'width', opts, 80
  indent = Util.getopt 'indent', opts
  indent = Fixnum === indent ? (' ' * indent) : "#{ indent }"
  column = []
  words = buf.split /\s+/
  row = "#{ indent }"
  while((word = words.shift))
    if((row.size + word.size) < (width - 1))
      row << word
    else
      column << row
      row = "#{ indent }"
      row << word
    end
    row << ' ' unless row.size == (width - 1)
  end
  column << row unless row.strip.empty?
  column.join "\n"
end
getopt(opt, hash, default = nil) click to toggle source
# File lib/main/util.rb, line 61
def getopt opt, hash, default = nil
  keys = opt.respond_to?('each') ? opt : [opt]

  keys.each do |key|
    return hash[key] if hash.has_key? key
    key = "#{ key }"
    return hash[key] if hash.has_key? key
    key = key.intern
    return hash[key] if hash.has_key? key
  end

  return default
end
home() click to toggle source
# File lib/main/util.rb, line 75
def home
  home =
    catch :home do
      ["HOME", "USERPROFILE"].each do |key|
        throw(:home, ENV[key]) if ENV[key]
      end
      if ENV["HOMEDRIVE"] and ENV["HOMEPATH"]
        throw(:home, "#{ ENV['HOMEDRIVE'] }:#{ ENV['HOMEPATH'] }")
      end
      File.expand_path("~") rescue(File::ALT_SEPARATOR ? "C:/" : "/")
    end
  File.expand_path(home)
end
indent(chunk, n = 2) click to toggle source
# File lib/main/util.rb, line 14
def indent chunk, n = 2
  lines = chunk.split /\n/
  re = nil
  s = ' ' * n
  lines.map! do |line|
    unless re
      margin = line[/^\s*/]
      re = /^#{ margin }/
    end
    line.gsub re, s 
  end.join("\n")
end
mcp(obj) click to toggle source
# File lib/main/util.rb, line 10
def mcp obj
  Marshal.load(Marshal.dump(obj))
end
unindent(chunk) click to toggle source
# File lib/main/util.rb, line 27
def unindent chunk
  lines = chunk.split /\n/
  indent = nil
  re = /^/ 
  lines.map! do |line|
    unless indent 
      indent = line[/^\s*/]
      re = /^#{ indent }/
    end
    line.gsub re, ''
  end.join("\n")
end