aboutsummaryrefslogtreecommitdiff
path: root/g.sh
blob: a9610b86192931ba54a249f91a87a0ed23ff9147 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# Copyright (C) 2013-2020 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.
__g_usage() {
    echo "usage: g [-h | --help] [-v | --version] <command> [<args>]"
}

# Print the help message.
__g_help() {
    __g_usage
    echo -e "\nThe g commands are:"
    tab=$'\t'
    column -t -s $'\t' <<HERE
   add${tab}Add a new shortcut.
   rm${tab}Remove a shorcut.
   list${tab}List all the available shortcuts.
HERE
}

# Fill the hash of shortcuts with the contents of the __g_file.
__g_get_shortcuts() {
    local word=""

    __g_shortcuts=()
    # shellcheck disable=SC2162
    while read line; do
        if [ -n "$line" ]; then
            if [ -z "$word" ]; then
                word=$line
            else
                __g_shortcuts[$word]=$(eval echo "$line")
                word=""
            fi
        fi
    done < "$__g_file"
}

# Echoes the keys of the current shortcuts. This is needed to cope with some
# differences between GNU Bash and ZSH.
__g_fetch_shortcut_keys() {
    if [ -n "$ZSH_VERSION" ]; then
        # shellcheck disable=SC2154
        echo "${(@k)__g_shortcuts}"
    else
        # shellcheck disable=SC2124
        echo "${!__g_shortcuts[@]}"
    fi
}

# Save the computed shortcuts into the __g_file.
__g_save_shortcuts() {
    # Erase the contents of the __g_file.
    :>"$__g_file"

    # And write the hash into the __g_file.
    keys=$(__g_fetch_shortcut_keys)
    for i in $keys; do
        echo "$i" >> "$__g_file"
        echo "${__g_shortcuts[$i]}" >> "$__g_file"
    done
}

# Returns true if the given parameter is a keyword, false otherwise.
__g_is_keyword() {
    case "$1" in
    add | rm | list | -v | --version | -h | --help)
        return 0
        ;;
    *)
        return 1
    esac
}

# It returns a string with all the elements of the given array joined by slash
# characters.
__g_join_path() {
    local IFS="/"; echo "$*";
}

# Replacement for the non-standard `realpath` command. Implemented taken from
# https://github.com/travis-ci/gimme.
__g_realpath() {
    # shellcheck disable=SC2005
    [ -d "$1" ] && echo "$(cd "$1" && pwd)" || echo "$(cd "$(dirname "$1")" \
        && pwd)/$(basename "$1")"
}

# The main function for this script.
g() {
    version="0.3.9"
    cmd="$1"
    if [ -z "$cmd" ]; then
        __g_help
        return 1
    fi

    __g_file=$HOME/.gfile
    declare -A __g_shortcuts

    # Make sure that the __g_file actually exists.
    if [ -n "$GFILE" ]; then
        __g_file="$GFILE"
    fi
    :>> "$__g_file"

    # Parse the command.
    case "$cmd" in
    -v | --version)
        cat <<HERE
g version $version
Copyright (C) 2013-2020 Miquel Sabaté Solà <mikisabate@gmail.com>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
HERE
        ;;
    -h | --help)
        __g_help
        ;;
    add)
        if [ "$#" = "2" ]; then
            p=$(pwd)
        else
            if [ "$#" = "3" ]; then
                p="$3"
            else
                echo "usage: g add <name> [path]"
                return 1
            fi
        fi
        if __g_is_keyword "$2"; then
            echo "Cannot use '$2': keyword."
            return 1
        fi
        __g_get_shortcuts
        __g_shortcuts[$2]=$(__g_realpath "$p")
        __g_save_shortcuts
        ;;
    rm)
        if [ "$#" != "2" ]; then
            echo "usage: g rm <name>"
            return 1
        fi
        __g_get_shortcuts
        unset "__g_shortcuts[$2]"
        __g_save_shortcuts
        ;;
    list)
        __g_get_shortcuts
        keys=$(__g_fetch_shortcut_keys)
        if [[ "$#" = "2" && "$2" = "--keys" ]]; then
            str=""
            for i in $keys; do
                str="$str $i"
            done
            echo "$str"
        else
            for i in $keys; do
                if [ -d "${__g_shortcuts[$i]}" ]; then
                    echo -e "$i\t=> ${__g_shortcuts[$i]}"
                else
                    echo -e "$i\t=> ${__g_shortcuts[$i]} (broken)"
                fi
            done | sort | column -t -s $'\t'
        fi
        ;;
    *)
        __g_get_shortcuts

        # Split the path and check whether the first element is a shortcut or
        # not.
        if [ -n "$ZSH_VERSION" ]; then
            # shellcheck disable=SC2154
            p=( "${(@s|/|)cmd}" )
            init="${p[1]}"
        else
            # shellcheck disable=SC2124
            IFS='/' read -r -a p <<< "$cmd"
            init="${p[0]}"
        fi

        if [ -z "${__g_shortcuts[$init]}" ]; then
            echo -e "Unknown shortcut \`$init'.\n"
            __g_usage
            return 1
        else
            # Expand the shortcut and append the remaining parts of the path.
            if [ -n "$ZSH_VERSION" ]; then
                p[1]="${__g_shortcuts[$init]}"
            else
                p[0]="${__g_shortcuts[$init]}"
            fi

            cd "$(__g_join_path "${p[@]}")" || return 1
        fi
        ;;
    esac

    return 0
}