Module: Aliasify

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

Constant Summary collapse

VERSION =
"0.1.3"

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
37
38
39
40
# 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}")
		system %{
			source ~/#{filename}
			shopt -s expand_aliases
		}
		print green("[aliasify]") + " Alias successfully added! Now you can go to the current directory from wherever by simply typing in `" + green("#{@@shortcut}") + "`."
	end
end

.colorize(text, color_code) ⇒ Object



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

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

.file_exists(filename) ⇒ Object



42
43
44
# File 'lib/aliasify.rb', line 42

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

.green(text) ⇒ Object



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

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

.red(text) ⇒ Object



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

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

.yellow(text) ⇒ Object



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

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