Module: Sfn::Utils::PathSelector

Included in:
Command::Import
Defined in:
lib/sfn/utils/path_selector.rb

Overview

Helper methods for path selection

Instance Method Summary collapse

Instance Method Details

#humanize_path_basename(path) ⇒ String

Humanize the base name of path

Parameters:

  • path (String)

Returns:

  • (String)


14
15
16
17
18
# File 'lib/sfn/utils/path_selector.rb', line 14

def humanize_path_basename(path)
  File.basename(path).sub(
    File.extname(path), ""
  ).split(/[-_]/).map(&:capitalize).join(" ")
end

#prompt_for_file(directory, opts = {}) ⇒ String

Prompt user for file selection

Parameters:

  • directory (String)

    path to directory

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :ignore_directories (Array<String>)

    directory names

  • :directories_name (String)

    title for directories

  • :files_name (String)

    title for files

  • :filter_prefix (String)

    only return results matching filter

Returns:

  • (String)

    file path



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sfn/utils/path_selector.rb', line 29

def prompt_for_file(directory, opts = {})
  file_list = Dir.glob(File.join(directory, "**", "**", "*")).find_all do |file|
    File.file?(file)
  end
  if opts[:filter_prefix]
    file_list = file_list.find_all do |file|
      file.start_with?(options[:filter_prefix])
    end
  end
  directories = file_list.map do |file|
    File.dirname(file)
  end.uniq
  files = file_list.find_all do |path|
    path.sub(directory, "").split("/").size == 2
  end
  if opts[:ignore_directories]
    directories.delete_if do |dir|
      opts[:ignore_directories].include?(File.basename(dir))
    end
  end
  if directories.empty? && files.empty?
    ui.fatal "No formation paths discoverable!"
  else
    output = ["Please select an entry"]
    output << "(or directory to list):" unless directories.empty?
    ui.info output.join(" ")
    output.clear
    idx = 1
    valid = {}
    unless directories.empty?
      output << ui.color("#{opts.fetch(:directories_name, "Directories")}:", :bold)
      directories.each do |dir|
        valid[idx] = {:path => dir, :type => :directory}
        output << [idx, humanize_path_basename(dir)]
        idx += 1
      end
    end
    unless files.empty?
      output << ui.color("#{opts.fetch(:files_name, "Files")}:", :bold)
      files.each do |file|
        valid[idx] = {:path => file, :type => :file}
        output << [idx, humanize_path_basename(file)]
        idx += 1
      end
    end
    max = idx.to_s.length
    output.map! do |o|
      if o.is_a?(Array)
        "  #{o.first}.#{" " * (max - o.first.to_s.length)} #{o.last}"
      else
        o
      end
    end
    ui.info "#{output.join("\n")}\n"
    response = ui.ask_question("Enter selection: ").to_i
    unless valid[response]
      ui.fatal "How about using a real value"
      exit 1
    else
      entry = valid[response.to_i]
      if entry[:type] == :directory
        prompt_for_file(entry[:path], opts)
      elsif Pathname(entry[:path]).absolute?
        entry[:path]
      else
        "/#{entry[:path]}"
      end
    end
  end
end