Class: TTY::Terminal::Home

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/terminal/home.rb

Overview

A class responsible for locating user home

Instance Method Summary collapse

Constructor Details

#initialize(platform = nil) ⇒ Home

Returns a new instance of Home.



8
9
10
# File 'lib/tty/terminal/home.rb', line 8

def initialize(platform = nil)
  @platform = platform || TTY::Platform
end

Instance Method Details

#find_homeObject

Find user home



15
16
17
18
# File 'lib/tty/terminal/home.rb', line 15

def find_home
  path = @platform.windows? ? windows_home : unix_home
  File.expand_path(path)
end

#unix_homeObject



20
21
22
23
24
25
# File 'lib/tty/terminal/home.rb', line 20

def unix_home
  require 'etc'
  "~#{Etc.getlogin}"
rescue
  ENV['HOME']
end

#windows_homeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tty/terminal/home.rb', line 27

def windows_home
  if (home = ENV['HOME'])
    home.tr('\\', '/')
  elsif ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    File.join(ENV['HOMEDRIVE'], ENV['HOMEPATH'])
  elsif ENV['USERPROFILE']
    ENV['USERPROFILE']
  elsif ENV['HOMEDRIVE'] || ENV['SystemDrive']
    File.join(ENV['HOMEDRIVE'] || ENV['SystemDrive'], '/')
  else
    'C:/'
  end
end