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-07 13:00:00 +08:00
|
|
|
# _prepend_tree [-p --preview] <path> [<glob>..]
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# DESCRIPTION
|
2015-01-07 13:00:00 +08:00
|
|
|
# Search a path tree and prepend directories with fish files
|
|
|
|
# printing any matches by default. Use a glob list to include
|
|
|
|
# or exclude other file extensions. Use -p --preview to just
|
|
|
|
# print matches withouth modifying the path.
|
2014-12-17 06:12:17 +08:00
|
|
|
#
|
|
|
|
# OPTIONS
|
2015-01-07 13:00:00 +08:00
|
|
|
# [-p --preview]
|
|
|
|
# Do not modify the path. Print directories that match the glob.
|
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-07 13:00:00 +08:00
|
|
|
# v.0.2.1
|
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
|
|
|
|
set -l path $argv[1]
|
|
|
|
if contains -- $path -p --preview
|
|
|
|
set path $argv[2]
|
2014-12-17 06:12:17 +08:00
|
|
|
end
|
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/**/
|
|
|
|
|
|
|
|
# Traverse $path and $subs prepending only directories with matches.
|
|
|
|
for dir in $path $subs
|
|
|
|
# Use head to retrieve at least the first match.
|
|
|
|
if [ -z (find $dir $glob -maxdepth 1 | head -1) ]
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
printf "%s" $dir
|
|
|
|
if not contains -- $argv[1] -p --preview
|
|
|
|
_prepend_path $dir -d fish_function_path
|
2014-12-17 07:21:13 +08:00
|
|
|
end
|
2014-12-17 06:12:17 +08:00
|
|
|
end
|
|
|
|
end
|