2014-12-17 06:12:17 +08:00
|
|
|
# NAME
|
2015-01-07 13:00:00 +08:00
|
|
|
# _prepend_tree - add a dependency tree to fish_function_path
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# SYNOPSIS
|
2015-01-08 01:21:51 +08:00
|
|
|
# _prepend_tree [-v --verbose] <path> [<glob>..]
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# DESCRIPTION
|
2015-01-08 01:21:51 +08:00
|
|
|
# Search a path tree and prepend directories with fish files. Use a glob
|
|
|
|
# list to include or exclude other file extensions. Use -v --verbose to
|
|
|
|
# output directories to be added to the path.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# OPTIONS
|
2015-01-08 01:21:51 +08:00
|
|
|
# [-v --verbose]
|
|
|
|
# Optional. Print directories that match the glob. Must be the
|
|
|
|
# first argument if used.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# <path>
|
|
|
|
# Required. Specify the path to search for glob patterns.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# [<glob> [<operator> <glob>..]]
|
|
|
|
# Glob pattern to match when traversing the path path.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# OPERATORS
|
|
|
|
# [! -not glob]
|
|
|
|
# Negates the following glob.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# [<glob> -o -or <glob>..]
|
|
|
|
# Default. Must meet at least one listed criteria.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# [<glob> [-a -and <glob>..]]
|
|
|
|
# Must meet *all* listed criteria.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# EXAMPLES
|
2015-01-07 13:00:00 +08:00
|
|
|
# _prepend_tree $path
|
|
|
|
# Match directories in $path containing `.fish` files.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# _prepend_tree $path \*.fish \*.sh
|
|
|
|
# Match directories in $path with either `.fish` OR `.sh` files.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
2015-01-07 13:00:00 +08:00
|
|
|
# _prepend_tree $path \*.fish -a ! _\*.\*
|
|
|
|
# Match directories with `.fish` files that do not start with `_`.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# AUTHORS
|
|
|
|
# Jorge Bucaran <jbucaran@me.com>
|
|
|
|
#
|
|
|
|
# SEE ALSO
|
|
|
|
# .oh-my-fish/functions/_prepend_path.fish
|
|
|
|
#
|
2015-01-08 01:21:51 +08:00
|
|
|
# v.0.2.0
|
2014-12-17 06:12:17 +08:00
|
|
|
#/
|
2015-01-07 13:00:00 +08:00
|
|
|
function _prepend_tree -d "Add a dependency tree to the Fish path."
|
|
|
|
# Match directories with .fish files always.
|
|
|
|
set -l glob -name \*.fish
|
2015-01-08 01:21:51 +08:00
|
|
|
set -l verbose ""
|
|
|
|
|
|
|
|
# Retrieve first argument, either the path or the -v option.
|
2015-01-07 13:00:00 +08:00
|
|
|
set -l path $argv[1]
|
2015-01-08 01:21:51 +08:00
|
|
|
if contains -- $path -v --verbose
|
|
|
|
set verbose -v
|
|
|
|
# Option first, path should be next.
|
2015-01-07 13:00:00 +08:00
|
|
|
set path $argv[2]
|
2014-12-17 06:12:17 +08:00
|
|
|
end
|
2015-01-08 01:21:51 +08:00
|
|
|
|
2015-01-07 13:00:00 +08:00
|
|
|
# Parse glob options to create the main glob pattern.
|
|
|
|
if [ (count $argv) -gt 2 ]
|
|
|
|
set -l operator -o
|
|
|
|
for option in $argv[3..-1]
|
|
|
|
switch $option
|
2014-12-17 06:12:17 +08:00
|
|
|
case ! -not
|
|
|
|
set operator $operator !
|
|
|
|
case -o -or
|
|
|
|
set operator -o
|
|
|
|
case -a -and
|
|
|
|
set operator -a
|
|
|
|
case "*"
|
2015-01-07 13:00:00 +08:00
|
|
|
if [ operator = ! ]
|
|
|
|
set glob $operator $glob
|
|
|
|
else
|
|
|
|
set glob $glob $operator
|
|
|
|
end
|
|
|
|
set glob $glob -name $option
|
|
|
|
set operator -o # Default
|
2014-12-17 06:12:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-07 13:00:00 +08:00
|
|
|
# Null wildcard expansion will break the for loop even if $path is valid.
|
|
|
|
# $subs will become an empty list for directories without sub directories
|
|
|
|
# which is safe to use in the loop.
|
|
|
|
set -l subs $path/**/
|
2015-01-08 01:21:51 +08:00
|
|
|
|
2015-01-07 13:00:00 +08:00
|
|
|
# Traverse $path and $subs prepending only directories with matches.
|
|
|
|
for dir in $path $subs
|
2015-01-08 01:21:51 +08:00
|
|
|
# Use head to retrieve at least one match. Ignore errors for non
|
|
|
|
# existing directories
|
|
|
|
if [ -z (find "$dir" $glob -maxdepth 1 ^/dev/null | head -1) ]
|
2015-01-07 13:00:00 +08:00
|
|
|
continue
|
|
|
|
end
|
2015-01-08 01:21:51 +08:00
|
|
|
|
|
|
|
# Print matched directories if the -v option is set.
|
|
|
|
if not [ -z $verbose ]
|
|
|
|
printf "%s\n" $dir
|
2014-12-17 07:21:13 +08:00
|
|
|
end
|
2015-01-08 01:21:51 +08:00
|
|
|
|
|
|
|
# Prepend matched directory to the the global fish function path.
|
|
|
|
# Note path duplicates are already handled by _prepend_path.
|
|
|
|
_prepend_path $dir -d fish_function_path
|
2014-12-17 06:12:17 +08:00
|
|
|
end
|
|
|
|
end
|