Module: Space::Core::Paths

Defined in:
lib/space_core/paths.rb

Class Method Summary collapse

Class Method Details

.contract(path, env: ENV) ⇒ Object



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

def contract(path, env: ENV)
  value = path.to_s
  home  = XDG.home(env: env)
  [home, realpath_or_nil(home)].compact.uniq.each do |h|
    return "~"  if value == h
    return "~#{value.delete_prefix(h)}" if value.start_with?("#{h}/")
  end
  value
end

.realpath_or_nil(path) ⇒ Object



17
18
19
20
21
# File 'lib/space_core/paths.rb', line 17

def realpath_or_nil(path)
  File.realpath(path)
rescue SystemCallError
  nil
end