fastfile.plugin.zsh
2668 bytes
1###########################
2# Settings
3
4# These can be overwritten any time.
5# If they are not set yet, they will be
6# overwritten with their default values
7
8fastfile_dir="${fastfile_dir:-${HOME}/.fastfile}"
9fastfile_var_prefix="${fastfile_var_prefix:-§}"
10
11###########################
12# Impl
13
14#
15# Generate a shortcut
16#
17# Arguments:
18# 1. name - The name of the shortcut (default: name of the file)
19# 2. file - The file or directory to make the shortcut for
20# STDOUT:
21# => fastfile_print
22#
23function fastfile() {
24 test "$2" || 2="."
25 file=$(readlink -f "$2")
26
27 test "$1" || 1="$(basename "$file")"
28 name=$(echo "$1" | tr " " "_")
29
30
31 mkdir -p "${fastfile_dir}"
32 echo "$file" > "$(fastfile_resolv "$name")"
33
34 fastfile_sync
35 fastfile_print "$name"
36}
37
38#
39# Resolve the location of a shortcut file (the database file, where the value is written!)
40#
41# Arguments:
42# 1. name - The name of the shortcut
43# STDOUT:
44# The path to the shortcut file
45#
46function fastfile_resolv() {
47 echo "${fastfile_dir}/${1}"
48}
49
50#
51# Get the real path of a shortcut
52#
53# Arguments:
54# 1. name - The name of the shortcut
55# STDOUT:
56# The path
57#
58function fastfile_get() {
59 cat "$(fastfile_resolv "$1")"
60}
61
62#
63# Print a shortcut
64#
65# Arguments:
66# 1. name - The name of the shortcut
67# STDOUT:
68# Name and value of the shortcut
69#
70function fastfile_print() {
71 echo "${fastfile_var_prefix}${1} -> $(fastfile_get "$1")"
72}
73
74#
75# List all shortcuts
76#
77# STDOUT:
78# (=> fastfile_print) for each shortcut
79#
80function fastfile_ls() {
81 for f in "${fastfile_dir}"/*(N); do
82 file=$(basename "$f") # To enable simpler handling of spaces in file names
83 varkey=$(echo "$file" | tr " " "_")
84
85 # Special format for columns
86 echo "${fastfile_var_prefix}${varkey}|->|$(fastfile_get "$file")"
87 done | column -t -s "|"
88}
89
90#
91# Remove a shortcut
92#
93# Arguments:
94# 1. name - The name of the shortcut (default: name of the file)
95# STDOUT:
96# => fastfile_print
97#
98function fastfile_rm() {
99 fastfile_print "$1"
100 rm "$(fastfile_resolv "$1")"
101 unalias "${fastfile_var_prefix}${1}"
102}
103
104#
105# Generate the aliases for the shortcuts
106#
107function fastfile_sync() {
108 for f in "${fastfile_dir}"/*(N); do
109 file=$(basename "$f") # To enable simpler handling of spaces in file names
110 varkey=$(echo "$file" | tr " " "_")
111
112 alias -g "${fastfile_var_prefix}${varkey}"="'$(fastfile_get "$file")'"
113 done
114}
115
116##################################
117# Shortcuts
118
119alias ff=fastfile
120alias ffp=fastfile_print
121alias ffrm=fastfile_rm
122alias ffls=fastfile_ls
123alias ffsync=fastfile_sync
124
125##################################
126# Init
127
128fastfile_sync