mirror of
https://github.com/oh-my-fish/oh-my-fish
synced 2024-11-03 15:40:32 +00:00
Merge pull request #291 from bucaran/feat-import
[feat-import] + import command, ⁑ bad wildcard expansion bugfix in _prepend_tree.
This commit is contained in:
commit
d88084719a
@ -11,10 +11,7 @@
|
||||
# OPTIONS
|
||||
# <path>
|
||||
# Required. Specify the path to add to the list.
|
||||
#
|
||||
# [<glob> [<operator> <glob>..]]
|
||||
# Glob pattern to match when traversing the path path.
|
||||
#
|
||||
|
||||
# OPERATORS
|
||||
# -d <DESTINATION PATH>
|
||||
# Should appear at the end if used. Specifies the name of the
|
||||
@ -28,17 +25,16 @@
|
||||
# _prepend_path $path -d $fish_function_path
|
||||
# Add $path to $fish_function_path
|
||||
#/
|
||||
|
||||
function _prepend_path
|
||||
set -l destination_path PATH #$PATH is the default destination path
|
||||
set -l len (count $argv)
|
||||
# $PATH is the default destination path
|
||||
set -l destination_path PATH
|
||||
set -l path $argv
|
||||
|
||||
if test $len -gt 2
|
||||
switch $argv[-2]
|
||||
if test (count $argv) -gt 2
|
||||
switch $path[-2]
|
||||
case -d --destination
|
||||
set destination_path $argv[-1]
|
||||
set path $argv[1..-3]
|
||||
set destination_path $path[-1]
|
||||
set path $path[1..-3]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1,72 +1,44 @@
|
||||
#
|
||||
# NAME
|
||||
# _prepend_tree - add a dependency tree to a path
|
||||
# _prepend_tree - add a dependency tree to fish_function_path
|
||||
#
|
||||
# SYNOPSIS
|
||||
# _prepend_tree [-P --preview] <source path> [<glob>..]
|
||||
# [-d --destination <destination path>]
|
||||
# _prepend_tree [-v --verbose] <path> [<glob>..]
|
||||
#
|
||||
# DESCRIPTION
|
||||
# Traverse the source path and prepend each directory matching the
|
||||
# glob list to the destination path or to the FISH function path.
|
||||
# If no globs are specified, match any directories with `.fish`
|
||||
# files. Return 1 if the source path is not specified.
|
||||
# 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.
|
||||
#
|
||||
# OPTIONS
|
||||
# -P --preview
|
||||
# Use to skip adding anything to the path and echo all matched
|
||||
# directories to stdout. Useful for debugging/testing.
|
||||
# [-v --verbose]
|
||||
# Optional. Print directories that match the glob. Must be the
|
||||
# first argument if used.
|
||||
#
|
||||
# <SOURCE PATH>
|
||||
# Required. Specify the path to find glob matches to prepend
|
||||
# to the destination path.
|
||||
# <path>
|
||||
# Required. Specify the path to search for glob patterns.
|
||||
#
|
||||
# <glob>...
|
||||
# Glob pattern to match when traversing the source path. If a
|
||||
# directory is found, it is prepended to the destination path.
|
||||
# `.fish` is assumed by default if not globs are specified.
|
||||
# [<glob> [<operator> <glob>..]]
|
||||
# Glob pattern to match when traversing the path path.
|
||||
#
|
||||
# It is also possible to use logical OR / AND operators to list
|
||||
# multiple globs. If none is used, OR is assumed by default.
|
||||
# The OR operator
|
||||
# OPERATORS
|
||||
# [! -not glob]
|
||||
# Negates the following glob.
|
||||
#
|
||||
# The following operators are available:
|
||||
# [<glob> -o -or <glob>..]
|
||||
# Default. Must meet at least one listed criteria.
|
||||
#
|
||||
# ! -not
|
||||
# Negates the following exression.
|
||||
#
|
||||
# ! glob1 glob2 → NOT glob1 or glob2
|
||||
#
|
||||
# -o -or
|
||||
# Any matches should meet at least one listed criteria.
|
||||
#
|
||||
# glob1 -o glob2 → glob1 OR glob2
|
||||
#
|
||||
# -a -and
|
||||
# Any matches must meet all listed criteria.
|
||||
#
|
||||
# glob1 -a glob2 → glob1 AND glob2
|
||||
#
|
||||
# -d <DESTINATION PATH>
|
||||
# Should appear at the end if used. Specifies the name of the
|
||||
# global path variable to prepend the matched directories to.
|
||||
# If not used, the $fish_function_path is assumed by default.
|
||||
# [<glob> [-a -and <glob>..]]
|
||||
# Must meet *all* listed criteria.
|
||||
#
|
||||
# EXAMPLES
|
||||
# _prepend_tree $lib_path
|
||||
# Prepends all directories inside $lib_path containing `.fish`
|
||||
# files to $fish_function_path.
|
||||
# _prepend_tree $path
|
||||
# Match directories in $path containing `.fish` files.
|
||||
#
|
||||
# _prepend_tree $lib_path -d PATH
|
||||
# Prepends all directories inside $lib_path containing .fish
|
||||
# files to a global variable named PATH.
|
||||
# _prepend_tree $path \*.fish \*.sh
|
||||
# Match directories in $path with either `.fish` OR `.sh` files.
|
||||
#
|
||||
# _prepend_tree $lib_path \*.fish \*.sh
|
||||
# Prepends sub directories with either `.fish` OR `.sh` files.
|
||||
#
|
||||
# _prepend_tree $lib_path \*.css -a ! _\*.\* -d PATH
|
||||
# Prepends sub directories that have `.css` extension, but do
|
||||
# not start with `_`.
|
||||
# _prepend_tree $path \*.fish -a ! _\*.\*
|
||||
# Match directories with `.fish` files that do not start with `_`.
|
||||
#
|
||||
# AUTHORS
|
||||
# Jorge Bucaran <jbucaran@me.com>
|
||||
@ -74,68 +46,64 @@
|
||||
# SEE ALSO
|
||||
# .oh-my-fish/functions/_prepend_path.fish
|
||||
#
|
||||
# v.0.1.0
|
||||
# v.0.2.0
|
||||
#/
|
||||
function _prepend_tree -d "Load a dependency tree, matching `.fish` files by default."
|
||||
set -l source
|
||||
set -l destination fish_function_path
|
||||
set -l glob
|
||||
set -l depth 1
|
||||
set -l len (count $argv)
|
||||
set -l preview false
|
||||
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 verbose ""
|
||||
|
||||
if [ $len -gt 0 ]
|
||||
switch $argv[1]
|
||||
case -P --preview
|
||||
set preview true
|
||||
set -e argv[1]
|
||||
set len (count $argv)
|
||||
end
|
||||
# Retrieve first argument, either the path or the -v option.
|
||||
set -l path $argv[1]
|
||||
if contains -- $path -v --verbose
|
||||
set verbose -v
|
||||
# Option first, path should be next.
|
||||
set path $argv[2]
|
||||
end
|
||||
|
||||
[ $len -gt 0 ]
|
||||
and set source $argv[1]
|
||||
or return 1
|
||||
|
||||
if [ $len -gt 2 ]
|
||||
# At least 3 arguments must exist
|
||||
# to use the destination path.
|
||||
switch $argv[-2]
|
||||
case -d --destination
|
||||
set destination $argv[-1]
|
||||
end
|
||||
end
|
||||
|
||||
if [ $len -gt 1 ]
|
||||
set -l operator
|
||||
for match in $argv[2..-1]
|
||||
switch $match
|
||||
# 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
|
||||
case ! -not
|
||||
set operator $operator !
|
||||
case -o -or
|
||||
set operator -o
|
||||
case -a -and
|
||||
set operator -a
|
||||
case -d --destination
|
||||
break # No more globs after this point.
|
||||
case "*"
|
||||
[ operator = ! ]
|
||||
and set glob $operator $glob
|
||||
or set glob $glob $operator
|
||||
set glob $glob -name $match
|
||||
set operator -o
|
||||
if [ operator = ! ]
|
||||
set glob $operator $glob
|
||||
else
|
||||
set glob $glob $operator
|
||||
end
|
||||
set glob $glob -name $option
|
||||
set operator -o # Default
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
[ -z "$glob" ]
|
||||
and set glob -name \*.fish
|
||||
# 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/**/
|
||||
|
||||
for directory in $source/**/
|
||||
if not [ -z (find $directory $glob -maxdepth $depth | head -1) ]
|
||||
eval $preview # skip when on preview mode
|
||||
and printf "%s " $directory
|
||||
or _prepend_path $directory $destination
|
||||
# Traverse $path and $subs prepending only directories with matches.
|
||||
for dir in $path $subs
|
||||
# Use head to retrieve at least one match. Skip not found errors
|
||||
# for directories that do not exist.
|
||||
if [ -z (find "$dir" $glob -maxdepth 1 ^/dev/null | head -1) ]
|
||||
continue
|
||||
end
|
||||
|
||||
# Print matched directories if the -v option is set.
|
||||
if not [ -z $verbose ]
|
||||
printf "%s\n" $dir
|
||||
end
|
||||
|
||||
# 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
|
||||
end
|
||||
end
|
||||
|
48
functions/import.fish
Normal file
48
functions/import.fish
Normal file
@ -0,0 +1,48 @@
|
||||
# NAME
|
||||
# import - load libraries, plugins, themes, etc.
|
||||
#
|
||||
# SYNOPSIS
|
||||
# import <path/library>[<path/library>..]
|
||||
#
|
||||
# DESCRIPTION
|
||||
# Import libraries, plugins, themes, completions. Prepend existing
|
||||
# user custom/<library> directories to the path to allow users to
|
||||
# override specific functions in themes/plugins.
|
||||
#
|
||||
# NOTES
|
||||
# $fish_path and $fish_custom point to oh-my-fish home and the user
|
||||
# dotfiles folder respectively. Both globals are usually configured
|
||||
# in ~/.config/fish/config.fish
|
||||
#
|
||||
# EXAMPLES
|
||||
# import plugins/<plugin>
|
||||
# import plugins/{dpaste,cask} themes/bobthefish
|
||||
#
|
||||
# AUTHORS
|
||||
# Jorge Bucaran <jbucaran@me.com>
|
||||
#
|
||||
# SEE ALSO
|
||||
# functions/_prepend_path.fish
|
||||
# functions/_prepend_tree.fish
|
||||
#
|
||||
# v.0.1.0
|
||||
#/
|
||||
function import -d "Load libraries, plugins, themes, etc."
|
||||
for library in $argv
|
||||
# Prepend plugins, themes and completions, traversing library
|
||||
# trees and prepending directories with fish code.
|
||||
_prepend_tree $fish_path/$library
|
||||
_prepend_tree $fish_custom/$library
|
||||
_prepend_path $fish_path/$library/completions -d fish_complete_path
|
||||
|
||||
# Set path to load files.
|
||||
set -l path $library/(basename $library).load
|
||||
|
||||
# Source each plugin, theme, etc., configuration load file.
|
||||
for load in $fish_path/$path $fish_custom/$path
|
||||
if [ -e $load ]
|
||||
. $load
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,74 +1,31 @@
|
||||
###
|
||||
# Helper functions
|
||||
###
|
||||
|
||||
function _fish_add_plugin
|
||||
set -l plugin $argv[1]
|
||||
set -l plugin_path "plugins/$plugin"
|
||||
|
||||
_prepend_path $fish_path/$plugin_path -d fish_function_path
|
||||
_prepend_path $fish_custom/$plugin_path -d fish_function_path
|
||||
end
|
||||
|
||||
function _fish_add_completion
|
||||
set -l plugin $argv[1]
|
||||
set -l completion_path "plugins/$plugin/completions"
|
||||
|
||||
_prepend_path $fish_path/$completion_path -d fish_complete_path
|
||||
_prepend_path $fish_custom/$completion_path -d fish_complete_path
|
||||
end
|
||||
|
||||
function _fish_source_plugin_load_file
|
||||
set -l plugin $argv[1]
|
||||
set -l load_file_path "plugins/$plugin/$plugin.load"
|
||||
|
||||
if test -e $fish_path/$load_file_path
|
||||
. $fish_path/$load_file_path
|
||||
end
|
||||
|
||||
if test -e $fish_custom/$load_file_path
|
||||
. $fish_custom/$load_file_path
|
||||
end
|
||||
end
|
||||
|
||||
function _fish_load_theme
|
||||
_prepend_path $fish_path/themes/$fish_theme -d fish_function_path
|
||||
_prepend_path $fish_custom/themes/$fish_theme -d fish_function_path
|
||||
end
|
||||
|
||||
###
|
||||
# Configuration
|
||||
###
|
||||
|
||||
# Set fish_custom to the path where your custom config files
|
||||
# and plugins exists, or else we will use the default custom.
|
||||
# and plugins exist, or use the default custom instead.
|
||||
if not set -q fish_custom
|
||||
set -g fish_custom $fish_path/custom
|
||||
set -g fish_custom $fish_path/custom
|
||||
end
|
||||
|
||||
# Extracting user's functions – will be added later.
|
||||
# Extract user defined functions from path and prepend later to
|
||||
# avoid collisions with oh-my-fish internal functions and allow
|
||||
# users to override/customize plugins, themes, etc.
|
||||
set user_function_path $fish_function_path[1]
|
||||
set -e fish_function_path[1]
|
||||
|
||||
# Add all functions
|
||||
# Add functions defined in oh-my-fish/functions to the path.
|
||||
if not contains $fish_path/functions/ $fish_function_path
|
||||
set fish_function_path $fish_path/functions/ $fish_function_path
|
||||
end
|
||||
|
||||
# Add all defined plugins
|
||||
for plugin in $fish_plugins
|
||||
_fish_add_plugin $plugin
|
||||
_fish_add_completion $plugin
|
||||
_fish_source_plugin_load_file $plugin
|
||||
# Add imported plugins, completions and themes. Customize imported
|
||||
# commands via the $fish_path/custom directory, for example create
|
||||
# a directory under $fish_path/custom/themes with the same name as
|
||||
# the theme and override any functions/variables there. Rinse and
|
||||
# repeat for plugins.
|
||||
import plugins/$fish_plugins themes/$fish_theme
|
||||
|
||||
# Source all files inside custom directory.
|
||||
for load in $fish_custom/*.load
|
||||
. $load
|
||||
end
|
||||
|
||||
# Load user defined theme
|
||||
_fish_load_theme $fish_theme
|
||||
|
||||
# Source all files inside custom folder
|
||||
for config_file in $fish_custom/*.load
|
||||
. $config_file
|
||||
end
|
||||
|
||||
# Re-adding user's functions so they have the highest priority
|
||||
# Prepend extracted user functions so they have the highest priority.
|
||||
set fish_function_path $user_function_path $fish_function_path
|
||||
|
Loading…
Reference in New Issue
Block a user