Module: Melon::Helpers
- Included in:
- CLI, Commands::Base
- Defined in:
- lib/melon/helpers.rb
Instance Method Summary collapse
- #blockquote(string, options = {}) ⇒ Object
- #error(error_obj_or_str, code = 1) ⇒ Object
- #expand_dir(path) ⇒ Object
- #format_command(name, desc, options = {}) ⇒ Object
- #recursively_expand(filelist) ⇒ Object
- #resolve_symlinks(file) ⇒ Object
- #wrap_text(txt, col = 80) ⇒ Object
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, = {}) = { :margin => 4, :wrap => 70 }.update() [:width] = .delete(:margin) [:margin] = 0 format_command('', string.gsub(/\s+/,' '). gsub(/\. /, '. '). gsub(/^ /, ' '), ) 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 (path) path.children.collect do |child| if child.file? child elsif child.directory? (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, = {}) = { :margin => 4, :width => 22, :wrap => 80 }.update() pad = "\n" + ' ' * [:width] desc = self.wrap_text(desc, [:wrap] - [:width]) desc = desc.split("\n").join(pad) ' ' * [:margin] + "#{name.ljust([:width] - [: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 (filelist) filelist.collect do |entry| fileish = Pathname.new(entry) if fileish.file? fileish elsif fileish.directory? (fileish) end end.flatten(1) end |
#resolve_symlinks(file) ⇒ Object
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 |