aboutsummaryrefslogtreecommitdiff
path: root/g.sh
blob: 4887cf1582b89182a7fc10b2512494f42c950132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#
# Copyright (C) 2013-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


# Print the usage string.
usage() {
    echo "usage: g [-h | --help] [-v | --version] <command> <args>"
}

# Print the help message.
help() {
    usage
    echo -e "\nThe g command are:"
    echo -e "   add\tAdd a new shortcut."
    echo -e "   rm\tRemove a shorcut."
    echo -e "   list\tList all the available shortcuts."
}

# Fill the hash of shortcuts with the contents of the gfile.
get_shortcuts() {
    local i=0 word=""

    gshortcuts=()
    while read line; do
        if [ ! -z "$line" ]; then
            if [ "$word" == "" ]; then
                word=$line
            else
                gshortcuts[$word]=$(eval echo $line)
                word=""
            fi
            i=$(($i+1))
        fi
    done < $gfile
}

# Save the computed shortcuts into the gfile.
save_shortcuts() {
    # Erase the contents of the gfile.
    >$gfile

    # Finally write the hash into the gfile.
    for i in "${!gshortcuts[@]}"; do
        echo "$i" >> $gfile
        echo "${gshortcuts[$i]}" >> $gfile
    done
}

# The main function for this script.
g() {
    version="0.3"
    cmd="$1"
    gfile=$HOME/.gfile
    declare -A gshortcuts

    # Make sure that the gfile actually exists.
    if [ ! -z "$GFILE" ]; then
        gfile="$GFILE"
    fi
    touch $gfile

    # Parse the command.
    case "$cmd" in
    -v | --version)
        echo "g version: $version"
        ;;
    -h | --help)
        help
        ;;
    add)
        path=`pwd`
        if [ "$#" != "2" ] && [ "$#" != "3" ]; then
            echo "usage: g add <name> [path]"
            return
        fi
        if [ "$#" == "3" ]; then
            path=$3
        fi
        get_shortcuts
        gshortcuts[$2]=$path
        save_shortcuts
        ;;
    rm)
        if [ "$#" != "2" ]; then
            echo "usage: g rm <name>"
            return
        fi
        get_shortcuts
        unset gshortcuts[$2]
        save_shortcuts
        ;;
    list)
        get_shortcuts
        if [[ "$#" == "2" && "$2" == "--keys" ]]; then
            str=""
            for i in "${!gshortcuts[@]}"; do
                str="$str $i"
            done
            echo $str
        else
            for i in "${!gshortcuts[@]}"; do
                echo -e "$i\t=> ${gshortcuts[$i]}"
            done | sort | column -t -s $'\t'
        fi
        ;;
    *)
        get_shortcuts
        if [ -z ${gshortcuts[$cmd]} ]; then
            echo -e "Unknown shortcut \`$cmd'.\n"
            usage
        else
            cd ${gshortcuts[$cmd]}
        fi
        ;;
    esac
}