From ab60bc8f9037d65dcca8d079b95cecb47a8da45e Mon Sep 17 00:00:00 2001 From: chimay Date: Fri, 26 Feb 2021 12:54:47 +0100 Subject: [PATCH 1/3] nb completion --- etc/nb-completion.zsh | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/etc/nb-completion.zsh b/etc/nb-completion.zsh index 1021b26a0..573dfea15 100644 --- a/etc/nb-completion.zsh +++ b/etc/nb-completion.zsh @@ -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 @@ -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 : completions. for __notebook in "${_notebooks[@]}" @@ -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 } @@ -142,25 +155,27 @@ _nb_subcommands() { if [[ -e "${_cache_path}" ]] then + _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}" &) + #(_cache_completions "${_cache_path}" &) fi if [[ "${?}" -eq 0 ]] then - compadd -- "${_completions_cached[@]}" - return 0 + compadd -- "${_completions_cached[@]}" + return 0 else return 1 fi From f77e42d94e19afc47b290cf775948035f1a08ccd Mon Sep 17 00:00:00 2001 From: chimay Date: Fri, 26 Feb 2021 12:57:58 +0100 Subject: [PATCH 2/3] tabs -> spaces --- etc/nb-completion.zsh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/etc/nb-completion.zsh b/etc/nb-completion.zsh index 573dfea15..7a32c6c07 100644 --- a/etc/nb-completion.zsh +++ b/etc/nb-completion.zsh @@ -50,13 +50,13 @@ _nb_subcommands() { 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}')) + #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[@]} ${_filenames[@]}) + IFS=$'\n' _completions=(${_commands[@]} ${_filenames[@]}) local _commands_cached= local _notebooks_cached= @@ -72,13 +72,13 @@ _nb_subcommands() { if [[ "${_counter}" == 1 ]] then - IFS=$' ' _commands_cached=("${__line}") + IFS=$' ' _commands_cached=("${__line}") elif [[ "${_counter}" == 2 ]] then - IFS=$' ' _notebooks_cached=("${__line}") + IFS=$' ' _notebooks_cached=("${__line}") elif [[ "${_counter}" == 3 ]] then - IFS=$' ' _filenames_cached=("${__line}") + IFS=$' ' _filenames_cached=("${__line}") else break fi @@ -174,8 +174,8 @@ _nb_subcommands() { if [[ "${?}" -eq 0 ]] then - compadd -- "${_completions_cached[@]}" - return 0 + compadd -- "${_completions_cached[@]}" + return 0 else return 1 fi From 0d9b5b138be281de66920f62efa64d537202fd78 Mon Sep 17 00:00:00 2001 From: chimay Date: Fri, 26 Feb 2021 12:59:28 +0100 Subject: [PATCH 3/3] _nb_cache_completions --- etc/nb-completion.zsh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/etc/nb-completion.zsh b/etc/nb-completion.zsh index 7a32c6c07..3fb35f41f 100644 --- a/etc/nb-completion.zsh +++ b/etc/nb-completion.zsh @@ -50,13 +50,13 @@ _nb_subcommands() { 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}')) + #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[@]} ${_filenames[@]}) + IFS=$'\n' _completions=(${_commands[@]} ${_filenames[@]}) local _commands_cached= local _notebooks_cached= @@ -72,13 +72,13 @@ _nb_subcommands() { if [[ "${_counter}" == 1 ]] then - IFS=$' ' _commands_cached=("${__line}") + IFS=$' ' _commands_cached=("${__line}") elif [[ "${_counter}" == 2 ]] then - IFS=$' ' _notebooks_cached=("${__line}") + IFS=$' ' _notebooks_cached=("${__line}") elif [[ "${_counter}" == 3 ]] then - IFS=$' ' _filenames_cached=("${__line}") + IFS=$' ' _filenames_cached=("${__line}") else break fi @@ -155,7 +155,7 @@ _nb_subcommands() { if [[ -e "${_cache_path}" ]] then - _cache_completions "${_cache_path}" + _nb_cache_completions "${_cache_path}" local _counter=0 @@ -169,13 +169,13 @@ _nb_subcommands() { fi done < "${_cache_path}" - #(_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