How to fill in file names relative to another directory?

This is the next discussion point in:

https://bugs.launchpad.net/ubuntu/+source/bash-completion/+bug/1394920

Suppose I have a folder ~/tmpwith some files and directories:

$ mkdir a; touch file1.txt; mkdir a/b; mkdir a/c; touch a/d; mkdir a/b/c

Now I am trying to complete the script to populate the file names in ~/tmp, but the parameter complete -o filenamesonly works correctly if the current directory ~/tmp.

See the link above for more background information. This, as I understand it:

$ cat setup
_compTest() {
    local cur baseFolder
    cur="${COMP_WORDS[$COMP_CWORD]}"
    baseFolder=~/tmp
    compopt -o nospace
    COMPREPLY=(  $(
       cd "$baseFolder"
       if [[ ${cur: -1} != "/" && -d $cur ]] ; then
           echo "$cur/"
       else
           compgen -f "$cur"
       fi
      )  )
}
complete -F _compTest aaa

Then I will send it:

$ . setup

and then I can do

$ aaa <tab><tab>
  • Problem 1: slashes are not added at the end of the list of names in the completion list (this is necessary to simplify individual directories from file names in the completion list)

  • 2: aaa a/<tab><tab> a/b a/c a/d, a/ . b/ c/ d.

+4
1

:

_compTest () 
{ 
    local cur; local tmp;  local tmp_escaped; local i;
    _get_comp_words_by_ref cur;
    local _compreply=()
    tmp=~/tmp/
    tmp_escaped=${tmp//\//\\\/}
    cur=$tmp$cur;

    if [ "$1" == "-d" ]; then
        _cd
    else
        _filedir;
    fi;
    for i in "${COMPREPLY[@]}"; do
        [ -d "$i" ] && [ "$i" != "$tmp." ] && [ "$i" != "$tmp.." ] && i="$i/"
        _compreply=("${_compreply[@]}" "$i")
    done

    COMPREPLY=(${_compreply[@]/$tmp_escaped/})
} && complete -o nospace -F _compTest aaa_files

_compTestDir()
{
    _compTest -d
} && complete -o nospace -F _compTestDir aaa_directories

3 ,

  • $cur - ~/tmp.
  • bash _filedir, cd/ls ..
  • ~/tmp COMPREPLY

: , .

  • perforce //....
  • http://localhost/* public_html.
+5
source

All Articles