-
-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1136 from koraa/pull_req_fastfile
On-the-fly creation and usage for file/dir shortcuts
- Loading branch information
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
################################################################################ | ||
# FILE: fastfile.plugin.zsh | ||
# DESCRIPTION: oh-my-zsh plugin file. | ||
# AUTHOR: Michael Varner (musikmichael@web.de) | ||
# VERSION: 1.0.0 | ||
# | ||
# This plugin adds the ability to on the fly generate and access file shortcuts. | ||
# | ||
################################################################################ | ||
|
||
########################### | ||
# Settings | ||
|
||
# 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="." | ||
file=$(readlink -f "$2") | ||
|
||
test "$1" || 1="$(basename "$file")" | ||
name=$(echo "$1" | tr " " "_") | ||
|
||
|
||
mkdir -p "${fastfile_dir}" | ||
echo "$file" > "$(fastfile_resolv "$name")" | ||
|
||
fastfile_sync | ||
fastfile_print "$name" | ||
} | ||
|
||
# | ||
# Resolve the location of a shortcut file (the database file, where the value is written!) | ||
# | ||
# Arguments: | ||
# 1. name - The name of the shortcut | ||
# STDOUT: | ||
# The path | ||
# | ||
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() { | ||
for f in "${fastfile_dir}"/*; do | ||
file=`basename "$f"` # To enable simpler handeling of spaces in file names | ||
varkey=`echo "$file" | tr " " "_"` | ||
|
||
# Special format for colums | ||
echo "${fastfile_var_prefix}${varkey}|->|$(fastfile_get "$file")" | ||
done | column -t -s "|" | ||
} | ||
|
||
# | ||
# Remove 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_rm() { | ||
fastfile_print "$1" | ||
rm "$(fastfile_resolv "$1")" | ||
} | ||
|
||
# | ||
# Generate the aliases for the shortcuts | ||
# | ||
function fastfile_sync() { | ||
for f in "${fastfile_dir}"/*; do | ||
file=`basename "$f"` # To enable simpler handeling of spaces in file names | ||
varkey=`echo "$file" | tr " " "_"` | ||
|
||
alias -g "${fastfile_var_prefix}${varkey}"="'$(fastfile_get "$file")'" | ||
done | ||
} | ||
|
||
################################## | ||
# Shortcuts | ||
|
||
alias ff=fastfile | ||
alias ffp=fastfile_print | ||
alias ffrm=fastfile_rm | ||
alias ffls=fastfile_ls | ||
alias ffsync=fastfile_sync | ||
|
||
################################## | ||
# Init | ||
|
||
fastfile_sync |