Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed-up bash completion by using sql cache again #1100

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 55 additions & 37 deletions etc/bash_completion.d/dnf
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,47 @@ _dnf_commands_helper()

_dnf_is_path()
{
if [[ "$1" == \.* ]] || [[ "$1" == \/* ]] || [[ "$1" == ~* ]]; then
if [[ "$1" == \.* ]] || [[ "$1" == *\/* ]] || [[ "$1" == ~* ]]; then
return 0
else
return 1
fi
}

# executes SQL query and appends resulting records to COMPREPLY
# @param $1 sql query
_dnf_completion_cache()
{
[[ -z $1 ]] && return 1
local cache_file="/var/cache/dnf/packages.db"
local sqlite3="sqlite3 -batch -init /dev/null"
local result
if result=( $( $sqlite3 $cache_file "$1" 2>/dev/null ) ); then
COMPREPLY+=( $( compgen -W '${result[@]}' ) )
return 0
else
return 1
fi
}


# @param $1 sql query for generating results from cache
# @param $2 command
_dnf_show_packages()
{
local args cmd=$1
_count_args
# do not print packages if command wasnt chosen yet
# eg.: dnf inTAB
[[ "$args" -eq 1 ]] && return
local query="$1" cmd=$2

# return when "-" follows command
# eg.: dnf up -
[[ "$cur" == -* ]] && return

shift
shift 2

if ! _dnf_is_path "$cur"; then
COMPREPLY+=( $(compgen -W "$( _dnf_commands_helper $cmd "$@" "$cur" )") )
if ! _dnf_completion_cache "$query"; then
COMPREPLY+=( $(compgen -W "$( _dnf_commands_helper $cmd "$@" "$cur" )") )
fi
fi

[[ $COMPREPLY ]] && return

COMPREPLY+=( $(compgen -o default -f -X "!*.@(rpm)" -- "$cur" ) )
}

_dnf_get_first_command()
Expand All @@ -179,6 +192,10 @@ _dnf()
local cur prev words cword split
_init_completion -s || return

local query
local query_installed="SELECT pkg FROM installed WHERE pkg LIKE \"$cur%\""
local query_available="SELECT pkg FROM available WHERE pkg LIKE \"$cur%\""

cmd_list="$( _dnf_commands_helper "_cmds" "" )"
command="$( _dnf_get_first_command "$cmd_list" )"

Expand Down Expand Up @@ -225,7 +242,7 @@ _dnf()

case "$command" in
autoremove|autoremove-n*)
_dnf_show_packages list installed
_dnf_show_packages "$query_installed" list installed
;;

check)
Expand All @@ -234,19 +251,19 @@ _dnf()
;;

check-update|check-upgrade)
_dnf_show_packages list updates
_dnf_show_packages "" list updates
;;

clean)
complete_commands="$__dnf_clean_subcmds"
;;

dup|dist-upgrade|dsync|distro*sync|distribution-synchronization)
_dnf_show_packages list installed
dist-upgrade|dsync|distro*sync|distribution-synchronization)
_dnf_show_packages "$query_installed" list installed
;;

dg|downgrade)
_dnf_show_packages $command
downgrade)
_dnf_show_packages "$query_installed" $command
;;

group|groups|grouperase|groupinfo|groupinstall|grouplist|groupremove|groupupdate)
Expand All @@ -267,16 +284,16 @@ _dnf()
--available --installed --updates --upgrades"
case "$( _dnf_get_first_command "$subcmd" )" in
--installed|installed)
_dnf_show_packages list installed ;;
_dnf_show_packages "$query_installed" list installed ;;

--available|available)
_dnf_show_packages list available ;;
_dnf_show_packages "$query_available" list available ;;

--updates|--upgrades|updates|upgrades)
_dnf_show_packages list updates ;;
_dnf_show_packages "" list updates ;;

*)
_dnf_show_packages list
_dnf_show_packages "$query_available" list
;;

esac
Expand All @@ -291,14 +308,16 @@ _dnf()
;;

in|install*|localinstall)
_dnf_show_packages $command
COMPREPLY=( $(compgen -o default -f -X "!*.@(rpm)" -- "$cur" ) )
query="SELECT available.pkg FROM available LEFT JOIN installed USING(pkg) WHERE available.pkg LIKE \"$cur%\" AND installed.pkg IS NULL"
_dnf_show_packages "$query" $command
;;

mark)
_dnf_show_packages list installed
_dnf_show_packages "$query_installed" list installed
;;

mc|ref|makecache|refresh)
makecache|refresh)
[[ "$prev" == *"timer" ]] && return
extra_options="--timer"
complete_commands="timer"
Expand All @@ -307,12 +326,12 @@ _dnf()
provides)
;;

ri|rei|reinstall)
_dnf_show_packages $command
reinstall)
_dnf_show_packages "$query_installed" $command
;;

rm|remove*|erase*)
_dnf_show_packages $command
remove*|erase*)
_dnf_show_packages "$query_installed" $command
extra_options="--oldinstallonly --duplicates --duplicated"
;;

Expand All @@ -338,22 +357,22 @@ _dnf()
complete_commands="$__dnf_repopkgs_subcmds"
;;

se|search)
search)
[[ "$prev" == *"all" ]] && return
extra_options="--all"
complete_commands="all"
;;

sh|shell) ;;
shell) ;;

swap) ;;

um|u-m|upgrade|upgrade-n*)
_dnf_show_packages $command
upgrade|upgrade-n*)
_dnf_show_packages "$query_available" $command
;;

up|up-min|update|update-n*)
_dnf_show_packages $command
update|update-n*)
_dnf_show_packages "$query_available" $command
;;

whatprovides) ;;
Expand Down Expand Up @@ -382,6 +401,5 @@ _dnf()

[[ $COMPREPLY == *= ]] && compopt -o nospace
return 0
}

} &&
complete -F _dnf dnf dnf-2 dnf-3