NixOS-Configs/home/common/bash_complition.nix
2024-01-06 14:02:54 -06:00

437 lines
15 KiB
Nix

{ pkgs, ... }: {
home.file = {
doascomplete = {
enable = true;
target = ".bash_completion.d/doas.sh";
text = ''
_doas()
{
local cur prev words cword split
_init_completion -s || return
local i mode=normal
[[ ''$1 == *doasedit ]] && mode=edit
[[ ''$mode == normal ]] &&
for ((i = 1; i <= cword; i++)); do
if [[ ''${words[i]} != -* ]]; then
local PATH=''$PATH:/sbin:/usr/sbin:/usr/local/sbin
local root_command=''${words[i]}
_command_offset ''$i
return
fi
[[ ''${words[i]} == -@(!(-*)[uUgCp]) ]] &&
((i++))
done
case "''$prev" in
-u)
COMPREPLY=(''$(compgen -u -- "''$cur"))
return
;;
esac
''$split && return
if [[ ''$cur == -* ]]; then
local opts=''$(_parse_help "''$1")
COMPREPLY=(''$(compgen -W ' ''${opts:-''$(_parse_usage "''$1")}' -- "''$cur"))
[[ ''${COMPREPLY-} == *= ]] && compopt -o nospace
return
fi
if [[ ''$mode == edit ]]; then
_filedir
fi
} &&
complete -F _doas doas
'';
};
taskcomplete = {
enable = true;
target = ".bash_completion.d/task.sh";
text = ''
################################################################################
#
# Copyright 2006 - 2016, Paul Beckingham, Federico Hernandez.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# http://www.opensource.org/licenses/mit-license.php
#
################################################################################
#
# The routines will do completion of:
#
# *) task subcommands
# *) project names
# *) tag names
# *) aliases
#
# To use these routines:
#
# 1) Copy this file to somewhere (e.g. ~/.bash_completion.d/task.sh).
# 2) Add the following line to your .bashrc:
# source ~/.bash_completion.d/task.sh
#
# OR
#
# 3) Copy the file to /etc/bash_completion.d
# 4) source /etc/bash_completion
#
# To submit patches/bug reports:
#
# *) Go to the project's website at
#
# http://taskwarrior.org
#
################################################################################
#the following variable is substituted for by ../../test/bash_completion.t
taskcommand='task rc.verbose:nothing rc.confirmation:no rc.hooks:off'
_task_get_tags() {
''$taskcommand _tags
}
_task_get_config() {
''$taskcommand _config
}
_task_offer_priorities() {
COMPREPLY=( ''$(compgen -W "L M H" -- ''${cur/*:/}) )
}
_task_offer_projects() {
COMPREPLY=( ''$(compgen -W "''$(''$taskcommand _projects)" -- ''${cur/*:/}) )
}
_task_offer_contexts() {
COMPREPLY=( ''$(compgen -W "''$(''$taskcommand _context) define delete list none show" -- ''$cur) )
}
_task_context_alias=''$(''$taskcommand show | grep alias.*context | cut -d' ' -f1 | cut -d. -f2)
_task()
{
local cur prev opts base
COMPREPLY=()
cur="''${COMP_WORDS[COMP_CWORD]}"
prev="''${COMP_WORDS[COMP_CWORD-1]}"
if [ ''${#COMP_WORDS[*]} -gt 2 ]
then
prev2="''${COMP_WORDS[COMP_CWORD-2]}"
else
prev2=""
fi
# useful for debugging:
# echo -e "\ncur='''$cur'"
# echo "prev='''$prev'"
# echo "prev2='''$prev2'"
abbrev_min=''$(''$taskcommand show | grep "abbreviation.minimum" | awk {'print ''$2'})
commands_aliases=''$(echo ''$(''$taskcommand _commands; ''$taskcommand _aliases) | tr " " "\n"|sort|tr "\n" " ")
opts="''$commands_aliases ''$(''$taskcommand _columns)"
case "''${prev}" in
''$_task_context_alias|cont|conte|contex|context)
_task_offer_contexts
return 0
;;
:)
case "''${prev2}" in
pri|prior|priori|priorit|priority)
if [ ''${#prev2} -ge ''$abbrev_min ]; then
_task_offer_priorities
fi
return 0
;;
pro|proj|proje|projec|project)
if [ ''${#prev2} -ge ''$abbrev_min ]; then
_task_offer_projects
fi
return 0
;;
rc)
# not activated when only "rc:" but is activated if anything after "rc:"
_filedir
return 0
;;
rc.data.location)
_filedir -d
return 0
;;
esac
;;
*)
case "''${cur}" in
pro:*|proj:*|proje:*|projec:*|project:*)
_task_offer_projects
return 0
;;
:)
case "''${prev}" in
pri|prior|priori|priorit|priority)
if [ ''${#prev} -ge ''$abbrev_min ]; then
_task_offer_priorities
fi
return 0
;;
pro|proj|proje|projec|project)
if [ ''${#prev} -ge ''$abbrev_min ]; then
_task_offer_projects
fi
return 0
;;
rc)
# activated only when "rc:"
cur="" # otherwise ":" is passed.
_filedir
return 0
;;
rc.data.location)
cur=""
_filedir -d
return 0
;;
esac
;;
+*)
local tags=''$(_task_get_tags | sed 's/^/+/')
COMPREPLY=( ''$(compgen -W "''${tags}" -- ''${cur}) )
return 0
;;
-*)
local tags=''$(_task_get_tags | sed 's/^/-/')
COMPREPLY=( ''$(compgen -W "''${tags}" -- ''${cur}) )
return 0
;;
rc.*)
local config=''$(_task_get_config | sed -e 's/^/rc\./' -e 's/''$/:/')
COMPREPLY=( ''$(compgen -W "''${config}" -- ''${cur}) )
return 0
;;
*)
case "''${prev}" in
import)
COMPREPLY=( ''$(compgen -o "default" -- ''${cur}) )
return 0
;;
esac
;;
esac
;;
esac
COMPREPLY=( ''$(compgen -W "''${opts}" -- ''${cur}) )
return 0
}
complete -o nospace -F _task task
complete -o nospace -F _task t
'';
};
timecomplete = {
enable = true;
target = ".bash_completion.d/timew.sh";
text = ''
# Bash completion for TimeWarrior
#
# Copyright (C) 2017 - 2021, Thomas Lauf
#
function __get_commands()
{
echo "annotate cancel config continue day delete diagnostics export extensions gaps get help join lengthen modify month move report resize shorten show split start stop summary tag tags track undo untag week"
}
function __get_subcommands()
{
case "''${1}" in
modify)
echo -e "end start"
;;
*)
echo ""
;;
esac
}
function __get_help_items()
{
echo -e "''$( __get_commands ) interval hints date duration dom"
}
function __get_options()
{
echo -e "--help --verbose --version"
}
function __get_ids()
{
local count
count="''$( timew get dom.tracked.count )"
if [[ "''${count}" -eq "0" ]] ; then
echo ""
else
seq -f "@%g" 1 "''${count}"
fi
}
function __get_tags()
{
timew tags | tail -n +4 -- | sed -e "s|[[:space:]]*-''$||"
}
function __get_extensions()
{
timew extensions | awk '{if(NR>6)print ''$1}'
}
function __has_entered_id()
{
for word in "''${COMP_WORDS[@]}" ; do
if [[ "''${word}" =~ ^@[0-9] ]] ; then
return 0
fi
done
return 1
}
function __has_entered_subcommand()
{
local subcommands=''$( __get_subcommands "''${1}" )
for word in "''${COMP_WORDS[@]}" ; do
for cmd in ''${subcommands} ; do
if [[ "''${word}" == "''${cmd}" ]] ; then
return 0
fi
done
done
return 1
}
function __has_entered_help_item()
{
local items=''$( __get_help_items )
for word in "''${COMP_WORDS[@]:2}" ; do
for item in ''${items} ; do
if [[ "''${word}" == "''${item}" ]] ; then
return 0
fi
done
done
return 1
}
function __is_entering_id()
{
if [[ "''${COMP_WORDS[COMP_CWORD]}" =~ @[0-9]* ]] ; then
return 0
else
return 1
fi
}
function __complete_tag()
{
COMPREPLY=()
cur="''${COMP_WORDS[COMP_CWORD]}"
local line wordlist
declare -a wordlist
while IFS=''$'\n' read -r line ; do
wordlist+=( "''${line}" )
done <<< "''$( __get_tags )"
declare -a completions
while read -r line ; do
completions+=( "''${line}" )
done < <( compgen -W "''$(printf '%q ' "''${wordlist[@]}")" -- "''${cur}" 2>/dev/null )
for completion in "''${completions[@]}" ; do
COMPREPLY+=( "''$(printf "%q" "''${completion}")" )
done
}
function _timew()
{
local cur first wordlist
COMPREPLY=()
cur="''${COMP_WORDS[COMP_CWORD]}"
first="''${COMP_WORDS[1]}"
case "''${first}" in
cancel|config|diagnostics|day|extensions|get|month|show|undo|week)
wordlist=""
;;
annotate|continue|delete|join|lengthen|move|resize|shorten|split)
wordlist=''$( __get_ids )
;;
export|gaps|start|stop|summary|tags|track)
__complete_tag
return
;;
modify)
if __has_entered_subcommand "''${first}" ; then
wordlist=''$( __get_ids )
else
wordlist=''$( __get_subcommands "''${first}" )
fi
;;
tag|untag)
if __is_entering_id ; then
wordlist=''$( __get_ids )
elif __has_entered_id ; then
__complete_tag
return
else
wordlist=''$( __get_ids )
fi
;;
report)
wordlist=''$( __get_extensions )
;;
help)
if __has_entered_help_item ; then
wordlist=""
else
wordlist=''$( __get_help_items )
fi
;;
-*)
wordlist=''$( __get_options )
;;
*)
wordlist=''$( __get_commands )
;;
esac
COMPREPLY=(''$( compgen -W "''${wordlist}" -- "''${cur}" ))
}
complete -F _timew timew
complete -F _timew tt
'';
};
};
}