2017-04-06 4 views
3

私はこのbashの補完ファイルを持っているbash自動補完で第1レベルのサブディレクトリのみを表示するには?

#/etc/bash_completion.d/mycommand 

_mycommand() 
{ 
    local cur 
    COMPREPLY=() 

    _init_completion || return 

    #Variable to hold the current word 
    cur="${COMP_WORDS[COMP_CWORD]}" 

    #array variable COMPREPLY 

    dirs=('Rootdir/ 
     Secondrootdir/ 
     Rootdir/Subdir/ 
     Secondrootdir/Anothersubdir/ 
     Secondrootdir/Thirdsubdir/ 
     Secondrootdir/Anothersubdir/Subsubdir/' 
    ) 

    COMPREPLY=($(compgen -W "$dirs" "$cur")) 

} 

#Assign the auto-completion function _mycommand for our command mycommand. 
complete -F _mycommand mycommand 

私は二回、私は以下を参照してください。複数の選択肢を持っているし、TABを打つ:

$ mycommand Secondrootdir/ 
Secondrootdir/ 
Secondrootdir/Anothersubdir/ 
Secondrootdir/Anothersubdir/Subsubdir/ 
Secondrootdir/Thirdsubdir/ 

しかし、私は、入力された現在の唯一のレベルの深いサブディレクトリを見てみたいですこのようなSecondrootdir

$ mycommand Secondrootdir/ 
Anothersubdir/ Thirdsubdir/ 

CDコマンドは、ビジネスだけでなく

$ cd Music/ 
kattymusic/ Playlists/ Relax/ 

を行います。しかし、私は、CDの自動補完がどのように機能するかを理解することはできません。

誰でも助けてください、よろしいですか?

+0

号のように見えますリモートサーバー。私はAPIを使ってそれらのリストを受け取ることができます。 – Oleg

+0

ありがとうございます。良い例ですが、私は反対のことが欲しいです。 – Oleg

+0

私はこれにもっと力を注いで成功することを願っています。 – Oleg

答えて

0

私は解決策を見つけました。それは信じられないほど簡単です。

ただ、1行のコードをポストに魔法

# This forces readline to only display the last item separated by a slash 
compopt -o filenames 
1

多くのありがとうを行い

Bash completion: compgen a wordlist as if they were paths - Only suggest up until next slash

最後にコードは彼らがオンになっている。この

#/etc/bash_completion.d/mycommand 

_mycommand() 
{ 
    local cur i 

    _init_completion || return 

    compopt -o filenames 

    #Variable to hold the current word 
    cur="${COMP_WORDS[COMP_CWORD]}" 


    dirs='Rootdir/Subdir/ Secondrootdir/Thirdsubdir/ Secondrootdir/Anothersubdir/Subsubdir/' 

    # If we include root dirs we have to remove them 
    # e.g we have Secondrootdir/ Secondrootdir/Anothersubdir/ Secondrootdir/Anothersubdir/Subsubdir/' 
    # we need to skip the shorties path and remain only the longest one Secondrootdir/Anothersubdir/Subsubdir/' 
    # dirs='Rootdir/ 
    #  Secondrootdir/ 
    #  Rootdir/Subdir/ 
    #  Secondrootdir/Anothersubdir/ 
    #  Secondrootdir/Thirdsubdir/ 
    #  Secondrootdir/Anothersubdir/Subsubdir/' 

    arr=($(compgen -W "$dirs" -- $cur)) 
    for path in "${arr[@]}" 
    do 

     local trailing_trim 

     # Determine what to trim from the end 
     trailing_trim="${path#${cur%/*}/}/" 
     trailing_trim="${trailing_trim#*/}" 
     trailing_trim="${trailing_trim%/}" 


     # Don't add a space if there is more to complete 
     [[ "$trailing_trim" != "" ]] && compopt -o nospace 

     # Remove the slash if mark-directories is off 
     if ! _rl_enabled mark-directories 
     then 
      # If The current typed path doesnt have a slash in it yet check if 
      # it is the full first portion of a path and ignore everything after 
      # if it is. We don't have to do this once the typed path has a slash 
      # in it as the logic above will pick up on it 
      [[ "$cur" != */* && "$path" == ${cur}/* ]] && path="$cur/$trailing_trim"  

      trailing_trim="/$trailing_trim" 
     fi 

     COMPREPLY[i++]="${path%%${trailing_trim}}" 

    done 



} 

#Assign the auto-completion function _mycommand for our command mycommand. 
complete -F _mycommand mycommand 
+0

あなた自身の答えを受け入れることができます。 – pynexj

関連する問題