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

adding filename completion of current notebook (zsh) #96

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 24 additions & 9 deletions etc/nb-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ _nb_subcommands() {
local _notebooks
IFS=$'\n' _notebooks=($(nb notebooks --names --no-color --unarchived))

local _filenames
#IFS=$'\n' _filenames=($(nb ls --filenames | cut -d ' ' -f 2))
# the field number is unknown : plain or encrypted files ?
# you need awk to get the last field
IFS=$'\n' _filenames=($(nb ls --filenames | awk '{print $NF}'))

local _completions=()
IFS=$'\n' _completions=(${_commands[@]})
IFS=$'\n' _completions=(${_commands[@]} ${_filenames[@]})

local _commands_cached=
local _notebooks_cached=
local _filenames_cached=

if [[ -e "${_cache_path}" ]]
then
Expand All @@ -65,18 +72,22 @@ _nb_subcommands() {

if [[ "${_counter}" == 1 ]]
then
_commands_cached="${__line}"
IFS=$' ' _commands_cached=("${__line}")
elif [[ "${_counter}" == 2 ]]
then
_notebooks_cached="${__line}"
IFS=$' ' _notebooks_cached=("${__line}")
elif [[ "${_counter}" == 3 ]]
then
IFS=$' ' _filenames_cached=("${__line}")
else
break
fi
done < "${_cache_path}"
fi

if [[ "${_commands_cached}" != "${_commands[*]:-}" ]] ||
[[ "${_notebooks_cached}" != "${_notebooks[*]:-}" ]]
if [[ "${_commands_cached}" != "${_commands:-}" ]] ||
[[ "${_notebooks_cached}" != "${_notebooks:-}" ]] ||
[[ "${_filenames_cached}" != "${_filenames:-}" ]]
then
# Construct <notebook>:<subcommand> completions.
for __notebook in "${_notebooks[@]}"
Expand Down Expand Up @@ -105,8 +116,10 @@ _nb_subcommands() {
{
(IFS=$' '; printf "%s\\n" "${_commands[*]}")
(IFS=$' '; printf "%s\\n" "${_notebooks[*]}")
(IFS=$' '; printf "%s\\n" "${_filenames[*]}")
printf "%s\\n" "${_completions[@]}"
} >> "${_cache_path}"

fi
}

Expand Down Expand Up @@ -142,25 +155,27 @@ _nb_subcommands() {

if [[ -e "${_cache_path}" ]]
then
_nb_cache_completions "${_cache_path}"

local _counter=0

while IFS= read -r __line
do
_counter=$((_counter+1))

if [[ "${_counter}" -gt 2 ]]
if [[ "${_counter}" -gt 3 ]]
then
_completions_cached+=("${__line}")
fi
done < "${_cache_path}"

(_nb_cache_completions "${_cache_path}" &)
#(_nb_cache_completions "${_cache_path}" &)
fi

if [[ "${?}" -eq 0 ]]
then
compadd -- "${_completions_cached[@]}"
return 0
compadd -- "${_completions_cached[@]}"
return 0
else
return 1
fi
Expand Down