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

Tab completion support for additional archives for 'borg delete' #5655

Merged
merged 2 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
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
38 changes: 20 additions & 18 deletions scripts/shell_completions/bash/borg
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,36 @@ _borg()
return 0
fi

# Get the repository name if available
# If there is a space before the "::" it means that no repository name was typed,
# so probably $BORG_REPO was set and we can still list the archives.
local repository_name="${COMP_LINE%%::*}"
repository_name=${repository_name##* }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to call an external command and hide its return code in order to do some matching. Can be replaced with local repository_name="${COMP_LINE%%::*}". It would be better to match against the second positional argument, but I don't how to do it using bash-completion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thanks for that!


# Listing archives.
# Since "::" is treated as separate word in bash,
# Since "::" is treated as separate word in Bash,
# it is $cur when the cursor is right behind it
# and $prev if the user has started to type an archive name.
local repository_name="" # If set, we'll list the archives
local typed_word=""
local typed_word=${cur}
local -i list_archives=0
if [[ ${cur} == "::" ]] ; then
repository_name=${prev}
list_archives=1
typed_word=""
fi
if [[ ${prev} == "::" ]] ; then
repository_name=${prevprev}
typed_word=${cur}
list_archives=1
fi
# Second archive listing for borg diff
if [[ ${COMP_LINE} =~ ^.*\ diff\ .*::[^\ ]+\ ${cur}$ ]] ; then
repository_name=`expr match "${COMP_LINE}" "\(.*\)::"`
repository_name=${repository_name##* }
typed_word=${cur}
list_archives=1
fi
# Additional archive listing for borg delete
if [[ ${COMP_LINE} =~ ^.*\ delete\ .*::[^\ ]+.*${cur}$ ]] ; then
list_archives=1
fi
if [[ ${repository_name} != "" ]] ; then
if [[ ${COMP_LINE} == *" ::"* ]] ; then
# There is a space before the "::"
# which means that no repository name was typed,
# so probably $BORG_REPO is set.
repository_name=""
fi
local archive_list=$(borg list --short "${repository_name}" 2>/dev/null)
COMPREPLY=( $(compgen -W "${archive_list}" -- "${typed_word}" ) )
if (( $list_archives )) ; then
local archives=$(borg list --short "${repository_name}" 2>/dev/null)
SanskritFritz marked this conversation as resolved.
Show resolved Hide resolved
COMPREPLY=( $(compgen -W "${archives}" -- "${typed_word}" ) )
return 0
fi

Expand Down
15 changes: 10 additions & 5 deletions scripts/shell_completions/fish/borg.fish
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function __fish_borg_list_repos_or_archives
if string match --quiet --regex '.*::' '"'(commandline --current-token)'"'
# If the current token contains "::" then list the archives:
set -l repository_name (string replace --regex '::.*' '' (commandline --current-token))
borg list --format="$repository_name::{archive}{NEWLINE}" "$repository_name" ^/dev/null
borg list --format="$repository_name::{archive}{TAB}{comment}{NEWLINE}" "$repository_name" ^/dev/null
else
# Otherwise list the repositories, directories and user@host entries:
set -l directories (commandline --cut-at-cursor --current-token)*/
Expand All @@ -377,15 +377,20 @@ end
complete -c borg -f -n "__fish_borg_is_argument_n 2" -a '(__fish_borg_list_repos_or_archives)'


# Second archive listing for borg diff
# Additional archive listings

function __fish_borg_is_diff_second_archive
return (string match --quiet --regex ' diff .*::[^ ]+ '(commandline --current-token)'$' (commandline))
end

function __fish_borg_list_diff_archives
function __fish_borg_is_delete_additional_archive
return (string match --quiet --regex ' delete .*::[^ ]+ ' (commandline))
end

function __fish_borg_list_only_archives
set -l repo_matches (string match --regex '([^ ]*)::' (commandline))
borg list --format="{archive}{NEWLINE}" "$repo_matches[2]" ^/dev/null
borg list --format="{archive}{TAB}{comment}{NEWLINE}" "$repo_matches[2]" ^/dev/null
end

complete -c borg -f -n __fish_borg_is_diff_second_archive -a '(__fish_borg_list_diff_archives)'
complete -c borg -f -n __fish_borg_is_diff_second_archive -a '(__fish_borg_list_only_archives)'
complete -c borg -f -n __fish_borg_is_delete_additional_archive -a '(__fish_borg_list_only_archives)'