Riyyi
3 years ago
1 changed files with 126 additions and 18 deletions
@ -1,25 +1,133 @@
|
||||
#compdef dotfiles.sh |
||||
|
||||
_dotfiles.sh() { |
||||
integer ret=1 |
||||
local -a args |
||||
typeset -A opt_args |
||||
|
||||
# Argument definition |
||||
# ------------------------------------------ |
||||
|
||||
args+=( |
||||
'*'{-a,--add=}'[add file to the dotfiles directory]:file:_files' |
||||
'(-f --files)'{-f,--files}'[display all files added to the dotfiles directory]' |
||||
_dotfiles_operations=( |
||||
{-F-,--file}'[display all files added to the dotfiles directory]' |
||||
{-P-,--package}'[instal, print or store packages]' |
||||
'(-)'{-h,--help}'[display usage message and exit]' |
||||
'(-p --packages)'{-p,--packages=}'[instal, list or store packages]:package functions:(( |
||||
install\:"install all core packages of the stored list" |
||||
install-aur\:"install all AUR packages of the stored list" |
||||
list\:"display all packages installed on the system (default)" |
||||
store\:"stores a list of all installed packages" |
||||
))' |
||||
'(-l --pull)'{-l,--pull}'[pull each added file from system to dotfiles directory]' |
||||
'(-s --push)'{-s,--push}'[push each added file to its location on the system]' |
||||
) |
||||
|
||||
_arguments -s -S $args[@] && ret=0 |
||||
return ret |
||||
_dotfiles_file_options=( |
||||
'(-F --file)'{-F,--file} |
||||
'(-a --add)'{-a,--add}'[add selected file paths to the dotfiles directory]' |
||||
'(-l --pull)'{-l,--pull}'[pull file(s) from the system to the dotfiles directory]' |
||||
'(-s --push)'{-s,--push}'[push file(s) from the dotfiles directory to the system]' |
||||
) |
||||
|
||||
_dotfiles_package_options=( |
||||
'(-P --package)'{-P,--package} |
||||
'(-a --aur-install)'{-a,--aur-install}'[install all AUR packages of the stored list]' |
||||
'(-i --install)'{-i,--install}'[install all official packages of the stored list]' |
||||
'(-s --store)'{-s,--store}'[stores a list of all installed packages on the system]' |
||||
) |
||||
|
||||
_dotfiles_dotfile_directory_target=( |
||||
'*:file:_files' |
||||
) |
||||
|
||||
_dotfiles_home_directory_target=( |
||||
'*:file:_files -W ~/' |
||||
) |
||||
|
||||
# Argument handling |
||||
# ------------------------------------------ |
||||
|
||||
function _dotfiles_file_argument_handling() |
||||
{ |
||||
case "$tmp" in |
||||
-*) |
||||
_arguments -s : "$_dotfiles_file_options[@]" && result=0 |
||||
;; |
||||
*) |
||||
# Enable matching of dotfiles without explicitly specifying the dot |
||||
setopt globdots |
||||
|
||||
case "$args" in |
||||
*a*) |
||||
_arguments -s : "$_dotfiles_home_directory_target[@]" && result=0 |
||||
;; |
||||
*) |
||||
_arguments -s : "$_dotfiles_dotfile_directory_target[@]" && result=0 |
||||
;; |
||||
esac |
||||
;; |
||||
esac |
||||
} |
||||
|
||||
function _dotfiles_package_argument_handling() |
||||
{ |
||||
case "$tmp" in |
||||
-*) |
||||
_arguments -s : "$_dotfiles_package_options[@]" && result=0 |
||||
;; |
||||
*) |
||||
case "$args" in |
||||
P) |
||||
_arguments -s : '*:package:_dotfiles_completions_tracked_packages' && result=0 |
||||
;; |
||||
*) |
||||
;; |
||||
esac |
||||
;; |
||||
esac |
||||
} |
||||
|
||||
if ! zmodload -s zsh/mapfile; then |
||||
function _dotfiles_completions_tracked_packages() {} |
||||
else |
||||
function _dotfiles_completions_tracked_packages() |
||||
{ |
||||
local -a packages |
||||
packages=( "${(f@)${mapfile[packages]%$'\n'}}" ) |
||||
readonly packages |
||||
compadd "$@" -a packages |
||||
} |
||||
fi |
||||
|
||||
# Completion control flow |
||||
# ------------------------------------------ |
||||
|
||||
_dotfiles.sh() |
||||
{ |
||||
local result=1 |
||||
local -a args |
||||
local tmp |
||||
|
||||
# Get all short flags |
||||
args=( ${${${(M)words:#-*}#-}:#-*} ) |
||||
# Get the current flag |
||||
tmp="$words[-1]" |
||||
|
||||
case "$args" in |
||||
h*) |
||||
_message -e arguments "no more arguments" |
||||
;; |
||||
F*) |
||||
_dotfiles_file_argument_handling |
||||
;; |
||||
P*) |
||||
_dotfiles_package_argument_handling |
||||
;; |
||||
*) |
||||
case ${(M)words:#--*} in |
||||
*--file*) |
||||
_dotfiles_file_argument_handling |
||||
;; |
||||
*--package*) |
||||
_dotfiles_package_argument_handling |
||||
;; |
||||
*) |
||||
_arguments -s : "$_dotfiles_operations[@]" && result=0 |
||||
;; |
||||
esac |
||||
;; |
||||
esac |
||||
|
||||
return result |
||||
} |
||||
|
||||
_dotfiles.sh |
||||
|
Loading…
Reference in new issue