From b7bad0f69b064225a4557667eb1e95d3df8ad819 Mon Sep 17 00:00:00 2001 From: mapc Date: Sun, 6 May 2012 00:05:22 +0200 Subject: [PATCH 1/2] Add the fastfile plugin --- plugins/fastfile/fastfile.plugin.zsh | 128 +++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 plugins/fastfile/fastfile.plugin.zsh diff --git a/plugins/fastfile/fastfile.plugin.zsh b/plugins/fastfile/fastfile.plugin.zsh new file mode 100644 index 00000000..51e48df5 --- /dev/null +++ b/plugins/fastfile/fastfile.plugin.zsh @@ -0,0 +1,128 @@ +################################################################################ +# 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="." + 2=$(readlink -f "$2") + test "$1" || 1="$(basename "$2")" + + mkdir -p "${fastfile_dir}" + echo "$2" > "$(fastfile_resolv "$1")" + + fastfile_sync + fastfile_print "$1" +} + +# +# 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 $(ls "${fastfile_dir}"); do + fastfile_print "$f" + done | column -t +} + +# +# 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 $(ls "${fastfile_dir}"); do + alias -g "${fastfile_var_prefix}${f}"="$(fastfile_get "$f")" + done +} + +################################## +# Shortcuts + +alias ff=fastfile +alias ffp=fastfile_print +alias ffrm=fastfile_rm +alias ffls=fastfile_ls +alias ffsync=fastfile_sync + +################################## +# Init + +fastfile_sync \ No newline at end of file From 8c18f007131eb242eacae16db85b07f986b08063 Mon Sep 17 00:00:00 2001 From: mapc Date: Sun, 6 May 2012 22:26:12 +0200 Subject: [PATCH 2/2] Enhance handleing of spaces in filenames --- plugins/fastfile/fastfile.plugin.zsh | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/plugins/fastfile/fastfile.plugin.zsh b/plugins/fastfile/fastfile.plugin.zsh index 51e48df5..775e9483 100644 --- a/plugins/fastfile/fastfile.plugin.zsh +++ b/plugins/fastfile/fastfile.plugin.zsh @@ -32,14 +32,17 @@ default fastfile_var_prefix "§" # function fastfile() { test "$2" || 2="." - 2=$(readlink -f "$2") - test "$1" || 1="$(basename "$2")" + file=$(readlink -f "$2") + + test "$1" || 1="$(basename "$file")" + name=$(echo "$1" | tr " " "_") + mkdir -p "${fastfile_dir}" - echo "$2" > "$(fastfile_resolv "$1")" + echo "$file" > "$(fastfile_resolv "$name")" fastfile_sync - fastfile_print "$1" + fastfile_print "$name" } # @@ -85,9 +88,13 @@ function fastfile_print() { # (=> fastfle_print) for each shortcut # function fastfile_ls() { - for f in $(ls "${fastfile_dir}"); do - fastfile_print "$f" - done | column -t + 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 "|" } # @@ -108,8 +115,11 @@ function fastfile_rm() { # Generate the aliases for the shortcuts # function fastfile_sync() { - for f in $(ls "${fastfile_dir}"); do - alias -g "${fastfile_var_prefix}${f}"="$(fastfile_get "$f")" + 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 }