fastfile.plugin.zsh 2.54 KB
Newer Older
mapc's avatar
mapc committed
1
###########################
Marc Cornellà's avatar
Marc Cornellà committed
2
# Settings
mapc's avatar
mapc committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# These can be overwritten any time.
# If they are not set yet, they will be
# overwritten with their default values

default fastfile_dir        "${HOME}/.fastfile/"
default fastfile_var_prefix "§"

###########################
# Impl

#
# Generate a shortcut
#
# Arguments:
#    1. name - The name of the shortcut (default: name of the file)
#    2. file - The file or directory to make the shortcut for
# STDOUT:
#    => fastfle_print
#
function fastfile() {
    test "$2" || 2="."
25
    file=$(readlink -f "$2")
Marc Cornellà's avatar
Marc Cornellà committed
26

27
28
29
    test "$1" || 1="$(basename "$file")"
    name=$(echo "$1" | tr " " "_")

mapc's avatar
mapc committed
30
31

    mkdir -p "${fastfile_dir}"
32
    echo "$file" > "$(fastfile_resolv "$name")"
mapc's avatar
mapc committed
33
34

    fastfile_sync
35
    fastfile_print "$name"
mapc's avatar
mapc committed
36
37
38
39
40
41
42
43
}

#
# Resolve the location of a shortcut file (the database file, where the value is written!)
#
# Arguments:
#    1. name - The name of the shortcut
# STDOUT:
Marc Cornellà's avatar
Marc Cornellà committed
44
#   The path to the shortcut file
mapc's avatar
mapc committed
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
#
function fastfile_resolv() {
    echo "${fastfile_dir}${1}"
}

#
# Get the real path of a shortcut
#
# Arguments:
#    1. name - The name of the shortcut
# STDOUT:
#    The path
#
function fastfile_get() {
    cat "$(fastfile_resolv "$1")"
}

#
# Print a shortcut
#
# Arguments:
#    1. name - The name of the shortcut
# STDOUT:
#    Name and value of the shortcut
#
function fastfile_print() {
    echo "${fastfile_var_prefix}${1} -> $(fastfile_get "$1")"
}

#
# List all shortcuts
#
# STDOUT:
#    (=> fastfle_print) for each shortcut
#
function fastfile_ls() {
Marc Cornellà's avatar
Marc Cornellà committed
81
82
83
    for f in "${fastfile_dir}"/*; do
        file=`basename "$f"` # To enable simpler handeling of spaces in file names
        varkey=`echo "$file" | tr " " "_"`
84

Marc Cornellà's avatar
Marc Cornellà committed
85
86
        # Special format for colums
        echo "${fastfile_var_prefix}${varkey}|->|$(fastfile_get "$file")"
87
    done | column -t -s "|"
mapc's avatar
mapc committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}

#
# Remove a shortcut
#
# Arguments:
#    1. name - The name of the shortcut (default: name of the file)
# STDOUT:
#    => fastfle_print
#
function fastfile_rm() {
    fastfile_print "$1"
    rm "$(fastfile_resolv "$1")"
}

#
# Generate the aliases for the shortcuts
#
function fastfile_sync() {
Marc Cornellà's avatar
Marc Cornellà committed
107
108
109
    for f in "${fastfile_dir}"/*; do
        file=`basename "$f"` # To enable simpler handeling of spaces in file names
        varkey=`echo "$file" | tr " " "_"`
110

Marc Cornellà's avatar
Marc Cornellà committed
111
        alias -g "${fastfile_var_prefix}${varkey}"="'$(fastfile_get "$file")'"
mapc's avatar
mapc committed
112
113
114
115
116
117
118
119
120
121
122
123
124
    done
}

##################################
# Shortcuts

alias ff=fastfile
alias ffp=fastfile_print
alias ffrm=fastfile_rm
alias ffls=fastfile_ls
alias ffsync=fastfile_sync

##################################
Marc Cornellà's avatar
Marc Cornellà committed
125
# Init
mapc's avatar
mapc committed
126

Marc Cornellà's avatar
Marc Cornellà committed
127
fastfile_sync