class FileSelector

Public Class Methods

explore() click to toggle source
# File lib/file_selector.rb, line 5
def self.explore
  new.explore
end
new() click to toggle source
# File lib/file_selector.rb, line 9
def initialize
  @files = Set[]
  @prompt = TTY::Prompt.new
end

Public Instance Methods

explore() click to toggle source
# File lib/file_selector.rb, line 14
def explore
  explore_mode
  @files
end
explore_mode(path = Dir.pwd) click to toggle source
# File lib/file_selector.rb, line 19
def explore_mode(path = Dir.pwd)
  system 'clear'
  files = files_in path
  options = ['finish', 'include', File.dirname(path)] + files
  prompt = 'Files and directoies to explore',
  choice = @prompt.select(prompt, options, per_page: 20)
  if choice == 'include'
    include_mode(path)
    explore_mode(path)
  elsif choice != 'finish'
    if File.directory?(choice)
      explore_mode(choice) if File.directory?(choice)
    else
      system "less #{choice}" unless File.directory?(choice)
      explore_mode(path)
    end
  end
end

Private Instance Methods

files_in(dir) click to toggle source
# File lib/file_selector.rb, line 65
def files_in(dir)
  (Dir[dir + '/.*'] + Dir[dir + '/*']) - ["#{dir}/.", "#{dir}/.."]
end
include_mode(dir) click to toggle source
# File lib/file_selector.rb, line 40
def include_mode(dir)
  system 'clear'
  cwd_files = files_in dir
  prompt = 'Files and directoies to include'
  choices = @prompt.multi_select(prompt, [dir] + cwd_files, per_page: 20)
  @files.merge(paths_to_files(choices))
end
paths_to_files(paths) click to toggle source
# File lib/file_selector.rb, line 48
def paths_to_files(paths)
  # Converts a set of paths to non-directory files and directories to a set
  # of non-directory files by replacing directories with the files therein
  files = Set[]
  paths.each do |path|
    if !File.directory?(path)
      files.add(path)
    else
      files_to_add = Dir.glob("#{path}/**/*").reject do |f|
        File.directory?(f) || ['.', '..'].include?(f)
      end
      files.merge(files_to_add)
    end
  end
  files
end