module Aliasify

Constants

VERSION

Public Class Methods

add_alias(shortcut) click to toggle source
# 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) click to toggle source

Helper Functions

# 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) click to toggle source

Citation: stackoverflow.com/questions/2070010/how-to-output-my-ruby-commandline-text-in-different-colours/2070405#2070405 Didn’t want to include a dependency on another gem

# File lib/aliasify.rb, line 48
def self.colorize(text, color_code)
        "\e[#{color_code}m#{text}\e[0m"
end
file_exists(filename) click to toggle source
# File lib/aliasify.rb, line 42
def self.file_exists(filename)
        File.exist?("#{Dir.home}/#{filename}")
end
green(text) click to toggle source
# File lib/aliasify.rb, line 53
def self.green(text); colorize(text, 32); end
red(text) click to toggle source
# File lib/aliasify.rb, line 52
def self.red(text); colorize(text, 31); end
yellow(text) click to toggle source
# File lib/aliasify.rb, line 54
def self.yellow(text); colorize(text, 33); end