Add gitignore usage information, fix empty api calls

Added general usage information. Made sure gitignore api is never called
without any arguments.

Made script fzf aware -- if it finds fzf it can be invoked without
arguments, if it does not it will display usage information instead.
This commit is contained in:
Marty Oehme 2020-02-06 15:07:55 +01:00
parent 84aae314f5
commit d411be708e

View file

@ -9,11 +9,18 @@
# Will *overwrite* any previous .gitignore file that exists in # Will *overwrite* any previous .gitignore file that exists in
# current directory. # current directory.
# #
# When called without arguments will load all possible arguments
# as fzf searchable list if fzf is in path.
#
# Enables completion for zsh in git/.config/shell/zshrc.d/_gitignore_completions.zsh # Enables completion for zsh in git/.config/shell/zshrc.d/_gitignore_completions.zsh
__get_items() { __get_items() {
if [ "$1" = "" ]; then
echo "gitignore definition generation needs at least one argument."
exit 1
fi
if [ "$savetofile" = "true" ]; then if [ "$savetofile" = "true" ]; then
__call_url "$@" | tee .gitignore __call_url "$@" >>.gitignore
else else
__call_url "$@" __call_url "$@"
fi fi
@ -25,22 +32,48 @@ __call_url() {
} }
gitignore() { gitignore() {
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
# just print to stdout or save locally? # just print to stdout or save locally?
if [ "$1" = "-f" ]; then elif [ "$1" = "-f" ] || [ "$1" = "--file" ]; then
savetofile=true savetofile=true
shift shift
fi fi
IFS="," IFS=","
if [ "$#" -eq 0 ]; then if [ "$#" -eq 0 ]; then
for item in $(__get_items list); do if type fzf >/dev/null 2>&1; then
echo "$item" for item in $(__get_items list); do
done | fzf --multi --ansi | paste -s -d "," - | echo "$item"
{ read -r result && __get_items "$result"; } done | fzf --multi --ansi | paste -s -d "," - |
cat { read -r result && __get_items "$result"; }
else
usage
fi
else else
__get_items "$@" __get_items "$@"
fi fi
} }
usage() {
printf "%s\n" \
"" \
" gitignore Quickly generate a gitignore definition." \
"" \
" Usage: gitignore [-h] [vim] [linux] [javascript] [...]" \
"" \
" Arguments:" \
"" \
" -h | --help Print out this help." \
"" \
" -f | --file Append gitignore definition to .gitignore file" \
" instead of stdout." \
"" \
" Arguments will be passed along to gitignore.io for parsing and " \
" gitignore definition generation. By default only prints to stdout." \
"" \
""
}
gitignore "$@" gitignore "$@"