Sha256: 8f36fdf68cd7ecdd36a21ef642ff3d930b841b4746215fd57246eb475463aec6

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

module CodeKindly
  module Utils
    class File
      class << self
        def all (path)
          CodeKindly::Utils::Dir.all path
        end

        def choose_from_options (directory_path, h = nil)
          require "highline"
          h ||= HighLine.new
          file_opts = file_options(directory_path)
          return nil if file_opts.blank?
          msg = "Select an existing file:"
          file_opts.each do |k,v|
            msg += "\n  #{k}: #{v}"
          end
          msg += "\n  0: None of the above"
          option = h.ask(msg, Integer)
          file_path = file_opts.fetch(option, nil)
          if file_path.present?
            file_path = ::File.join(directory_path, file_path)
          end
          file_path
        end

        def file_options (path)
          require "map"
          options = Map.new
          key = 0
          find(path).each do |file|
            options[key+=1] = file
          end
          options
        end

        def find (path)
          require "fileutils"
          all(path).select { |entry| ::File.file?("#{path}/#{entry}") }
        end

        def trash! (file_string)
          require "open3"
          stdin, stdout, stderr = Open3.popen3("ls #{file_string}")
          if stdout.gets
            # move to trash (or delete) existing downloaded files
            # sudo gem install osx-trash (http://www.dribin.org/dave/blog/archives/2008/05/24/osx_trash/)
            stdin, stdout, stderr = Open3.popen3("which trash")
            trash = stdout.gets
            command = case
              when trash then "#{trash.strip} #{file_string}" # output of `which` has ending \n
              when ::File.directory?("~/.Trash") then "mv #{file_string} ~/.Trash"
              else "rm #{file_string}"
            end
            Kernel.system(command)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codekindly-utils-0.0.5 lib/code_kindly/utils/file.rb
codekindly-utils-0.0.4 lib/code_kindly/utils/file.rb
codekindly-utils-0.0.3 lib/code_kindly/utils/file.rb
codekindly-utils-0.0.2 lib/code_kindly/utils/file.rb