Module: Melon::Helpers

Included in:
CLI, Commands::Base
Defined in:
lib/melon/helpers.rb

Instance Method Summary collapse

Instance Method Details

#blockquote(string, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/melon/helpers.rb', line 25

def blockquote(string, options = {})
  options = {
    :margin => 4,
    :wrap => 70
  }.update(options)

  options[:width] = options.delete(:margin)
  options[:margin] = 0
  format_command('', string.gsub(/\s+/,' ').
                 gsub(/\. /, '.  ').
                 gsub(/^ /, '  '), options)
end

#error(error_obj_or_str, code = 1) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/melon/helpers.rb', line 38

def error(error_obj_or_str, code = 1)
  if error_obj_or_str.respond_to?('to_s')
    error_str = error_obj_or_str.to_s
  else
    error_str = error_obj_or_str.inspect
  end

  $stderr.puts "melon: #{error_str}"
  exit code
end

#expand_dir(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/melon/helpers.rb', line 60

def expand_dir(path)
  path.children.collect do |child|
    if child.file?
      child
    elsif child.directory?
      expand_dir(child)
    end
  end.select { |x| x }.flatten(1)

end

#format_command(name, desc, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/melon/helpers.rb', line 5

def format_command(name, desc, options = {})
  options = {
    :margin => 4,
    :width => 22,
    :wrap => 80
  }.update(options)

  pad = "\n" + ' ' * options[:width]
  desc = self.wrap_text(desc, options[:wrap] - options[:width])
  desc = desc.split("\n").join(pad)

  ' ' * options[:margin] +
    "#{name.ljust(options[:width] - options[:margin])}#{desc}"
end

#recursively_expand(filelist) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/melon/helpers.rb', line 49

def recursively_expand(filelist)
  filelist.collect do |entry|
    fileish = Pathname.new(entry)
    if fileish.file?
      fileish
    elsif fileish.directory?
      expand_dir(fileish)
    end
  end.flatten(1)
end


71
72
73
# File 'lib/melon/helpers.rb', line 71

def resolve_symlinks(file)
  Pathname.new(file).realpath.to_s
end

#wrap_text(txt, col = 80) ⇒ Object



20
21
22
23
# File 'lib/melon/helpers.rb', line 20

def wrap_text(txt, col = 80)
  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
           "\\1\\3\n") 
end