Module: Aliasify

Defined in:
lib/aliasify.rb,
lib/aliasify/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.add_alias(shortcut) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aliasify.rb', line 7

def self.add_alias(shortcut)
  cwd = Dir.pwd
  @@toAdd = "alias #{shortcut}=\"cd #{cwd}\""
  @@shortcut = shortcut
  puts green("[aliasify]") + " Trying to add the alias to some kind of login script..."
  if file_exists(".bash_aliases")
    add_to_file(".bash_aliases")
  elsif file_exists(".bash_profile")
    add_to_file(".bash_profile")
  elsif file_exists(".bashrc")
    add_to_file(".bashrc")
  elsif file_exists(".bash_login")
    add_to_file(".bash_login")
  elsif file_exists(".profile")
    add_to_file(".profile")
  elsif file_exists(".environment")
    add_to_file(".environment")
  else print green("[aliasify]") + red(" Tried to find .bash_aliases, .bash_profile, .bashrc, .bash_login , .login, and .environment. None of them were found, so won't be able to add alias :(")
  end
end

.add_to_file(filename) ⇒ Object

Helper Functions



30
31
32
33
34
35
36
# File 'lib/aliasify.rb', line 30

def self.add_to_file(filename)
  File.open("#{Dir.home}/#{filename}", 'a') do |file|
    file.puts @@toAdd
    puts green("[aliasify]") + " Found " + yellow("#{filename}")
    print green("[aliasify]") + " Success! Now, run `" + green("source ~/#{filename}") + "` to make the alias functional. Then, go to the current directory from wherever by simply typing in `" + green("#{@@shortcut}") + "`."
  end
end

.colorize(text, color_code) ⇒ Object



44
45
46
# File 'lib/aliasify.rb', line 44

def self.colorize(text, color_code)
  "\e[#{color_code}m#{text}\e[0m"
end

.file_exists(filename) ⇒ Object



38
39
40
# File 'lib/aliasify.rb', line 38

def self.file_exists(filename)
  File.exist?("#{Dir.home}/#{filename}")
end

.green(text) ⇒ Object



49
# File 'lib/aliasify.rb', line 49

def self.green(text); colorize(text, 32); end

.red(text) ⇒ Object



48
# File 'lib/aliasify.rb', line 48

def self.red(text); colorize(text, 31); end

.yellow(text) ⇒ Object



50
# File 'lib/aliasify.rb', line 50

def self.yellow(text); colorize(text, 33); end