Module: AI::Paths

Defined in:
lib/paths.rb

Overview

── Path helpers ─────────────────────────────────────────────────────

Class Method Summary collapse

Class Method Details

.complete(arg) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/paths.rb', line 19

def complete(arg)
  if arg.empty?
    dir_text = ""; dir_real = "."; base = ""
  elsif arg.end_with?("/")
    dir_text = arg
    dir_real = arg.start_with?("~") ? File.expand_path(arg) : arg
    base     = ""
  else
    slash = arg.rindex("/")
    if slash
      dir_text = arg[0..slash]
      dir_real = dir_text.start_with?("~") ? File.expand_path(dir_text) : dir_text
      base     = arg[(slash + 1)..]
    else
      dir_text = ""; dir_real = "."; base = arg
    end
  end
  return [] unless File.directory?(dir_real)
  Dir.children(dir_real).select { |e| e.start_with?(base) }.sort.map do |e|
    full   = File.join(dir_real, e)
    suffix = File.directory?(full) ? "/" : ""
    dir_text + e + suffix
  end
end

.short(abs) ⇒ Object



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

def short(abs)
  abs    = File.expand_path(abs)
  home   = Dir.home
  tilde  = (abs == home || abs.start_with?(home + "/")) ? "~" + abs[home.length..] : nil
  relcwd = begin
             r = Pathname.new(abs).relative_path_from(Pathname.pwd).to_s
             r == "." ? "./" : (r.start_with?("..") ? r : "./" + r)
           rescue StandardError
             nil
           end
  [abs, tilde, relcwd].compact.min_by(&:length)
end