Skip to content

Instantly share code, notes, and snippets.

@yonchu
Last active December 20, 2015 07:29
Show Gist options
  • Select an option

  • Save yonchu/6094089 to your computer and use it in GitHub Desktop.

Select an option

Save yonchu/6094089 to your computer and use it in GitHub Desktop.

Revisions

  1. yonchu revised this gist Sep 3, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions _grunt.zsh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # - 関数はグローバルに定義されるため、重複しないよう名前付けには十分配慮すること
    # − 短い補完関数であれば、関数を使わずに書くと良い

    function _grunt() {
    function __grunt() {
    ### 必要な変数を定義
    # 現在のコンテキスト(curcontext) を保存
    # コンテキストは、現在補完しようとしているコマンドを示しています
    @@ -138,4 +138,4 @@ function __grunt_caching_policy() {
    (( $#oldp ))
    }

    _grunt "$@"
    __grunt "$@"
  2. yonchu revised this gist Jul 27, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _grunt.zsh
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ function _grunt() {
    case $state in
    tasks)
    ######################
    # 3.タスク補完 #
    # 3.タスク補完
    ######################
    # __grunt_tasks 変数と _describe 関数を使用してタスクを補完
    _describe "grunt task$update_msg" __grunt_tasks || return 1
  3. yonchu revised this gist Jul 27, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions _grunt.zsh
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ function _grunt() {
    )

    ###############################
    # 1.cache-policy を設定 #
    # 1.cache-policy を設定
    ###############################
    zstyle -s ":completion:${curcontext}:" cache-policy update_policy
    if [[ -z $update_policy ]]; then
    @@ -58,7 +58,7 @@ function _grunt() {
    _arguments $args && return

    ########################################
    # 2.キャッシュの復元と更新を行う #
    # 2.キャッシュの復元と更新を行う
    ########################################
    ###
    # グローバル変数 __grunt_tasks にタスク情報が設定される
    @@ -81,7 +81,7 @@ function _grunt() {
    }

    ############################################################
    # 4.キャッシュの復元と必要ならキャッシュの更新を行う #
    # 4.キャッシュの復元と必要ならキャッシュの更新を行う
    ############################################################
    # キャッシュファイル名: grunt
    # キャッシュ変数名: __grunt_tasks
    @@ -121,7 +121,7 @@ function __grunt_get_tasks() {
    }

    #######################################
    # 5.cache-policy に設定する関数 #
    # 5.cache-policy に設定する関数
    #######################################
    # $1 にキャッシュファイルのパスが入っています
    #
  4. yonchu revised this gist Jul 27, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion _grunt.zsh
    Original file line number Diff line number Diff line change
    @@ -123,7 +123,6 @@ function __grunt_get_tasks() {
    #######################################
    # 5.cache-policy に設定する関数 #
    #######################################
    ### 5.cache-policy に設定する関数
    # $1 にキャッシュファイルのパスが入っています
    #
    # 戻り値:
  5. yonchu revised this gist Jul 27, 2013. No changes.
  6. yonchu revised this gist Jul 27, 2013. 1 changed file with 16 additions and 4 deletions.
    20 changes: 16 additions & 4 deletions _grunt.zsh
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,9 @@ function _grunt() {
    '*: :->tasks' \
    )

    ### 1.cache-policy を設定
    ###############################
    # 1.cache-policy を設定 #
    ###############################
    zstyle -s ":completion:${curcontext}:" cache-policy update_policy
    if [[ -z $update_policy ]]; then
    zstyle ":completion:${curcontext}:" cache-policy __grunt_caching_policy
    @@ -55,7 +57,10 @@ function _grunt() {
    # タスクの補完なら state 変数に 'tasks' が設定され処理継続
    _arguments $args && return

    ### 2.キャッシュの復元と更新を行う
    ########################################
    # 2.キャッシュの復元と更新を行う #
    ########################################
    ###
    # グローバル変数 __grunt_tasks にタスク情報が設定される
    if __grunt_retrieve_update_cache; then
    update_msg='(cache updated)'
    @@ -64,7 +69,9 @@ function _grunt() {
    ### タスクの補完かどうか判別
    case $state in
    tasks)
    ### 3.タスク補完
    ######################
    # 3.タスク補完 #
    ######################
    # __grunt_tasks 変数と _describe 関数を使用してタスクを補完
    _describe "grunt task$update_msg" __grunt_tasks || return 1
    ;;
    @@ -73,7 +80,9 @@ function _grunt() {
    return
    }

    ### 4.キャッシュの復元と必要ならキャッシュの更新を行う
    ############################################################
    # 4.キャッシュの復元と必要ならキャッシュの更新を行う #
    ############################################################
    # キャッシュファイル名: grunt
    # キャッシュ変数名: __grunt_tasks
    function __grunt_retrieve_update_cache() {
    @@ -111,6 +120,9 @@ function __grunt_get_tasks() {
    echo "${(j:\n:)tasks}"
    }

    #######################################
    # 5.cache-policy に設定する関数 #
    #######################################
    ### 5.cache-policy に設定する関数
    # $1 にキャッシュファイルのパスが入っています
    #
  7. yonchu renamed this gist Jul 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. yonchu created this gist Jul 27, 2013.
    130 changes: 130 additions & 0 deletions _grunt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    #compdef grunt

    ### 変数や関数を使用する場合の注意
    # - 変数を使用する場合は、定義が グローバル に漏れないよう、必ず local or typeset(declare) コマンド使って変数宣言を行うこと
    # - 関数はグローバルに定義されるため、重複しないよう名前付けには十分配慮すること
    # − 短い補完関数であれば、関数を使わずに書くと良い

    function _grunt() {
    ### 必要な変数を定義
    # 現在のコンテキスト(curcontext) を保存
    # コンテキストは、現在補完しようとしているコマンドを示しています
    # 補完によっては _arguments -C などを使用してコンテキストを変更することがあるので、
    # 補完関数の先頭で現在のコンテキストを保存しておくのが習慣のようになっています
    # なお今回の補完ではコンテキストの変更は行いません
    # また、以下の様な値が設定されます
    # :complete:grunt:
    local curcontext="$curcontext"
    # 現在の cache-policy を保存する変数
    local update_policy
    # 補完対象を保存する変数
    local state
    # 補完情報を格納する配列
    local -a args
    # キャッシュが更新されたときのメッセージ
    # 無くても良いが、あった方が分かりやすい
    local update_msg

    ### local は1文で書くこともできる
    # local curcontext="$curcontext" update_policy state update_msg
    # local -a args

    ### 補完情報を定義
    # 詳しい説明は省略
    args=(
    ### オプションの補完
    '(- 1 *)'{-h,--help}'[Display this help text]' \
    '(--version,-V)'{--version,-V}'[Print the grunt version. Combine with --verbose for more info.]' \
    '(--verbose,-v)'{--verbose,-v}'[Verbose mode. A lot more information output.]' \
    '(--completion)--completion[Output shell auto-completion rules. See the grunt-cli documentation for more information.]' \
    ### オプション以外の補完
    # オプション以外を補完する時、state 変数に 文字列 'tasks' が設定されます
    # tasks という名前は好きな名前を設定できます
    # *:->, *::->, *:::-> などの書き方がありますが、詳しい説明は省略
    '*: :->tasks' \
    )

    ### 1.cache-policy を設定
    zstyle -s ":completion:${curcontext}:" cache-policy update_policy
    if [[ -z $update_policy ]]; then
    zstyle ":completion:${curcontext}:" cache-policy __grunt_caching_policy
    fi

    ### 補完
    # オプションの補完なら、オプションが補完され return
    # タスクの補完なら state 変数に 'tasks' が設定され処理継続
    _arguments $args && return

    ### 2.キャッシュの復元と更新を行う
    # グローバル変数 __grunt_tasks にタスク情報が設定される
    if __grunt_retrieve_update_cache; then
    update_msg='(cache updated)'
    fi

    ### タスクの補完かどうか判別
    case $state in
    tasks)
    ### 3.タスク補完
    # __grunt_tasks 変数と _describe 関数を使用してタスクを補完
    _describe "grunt task$update_msg" __grunt_tasks || return 1
    ;;
    esac

    return
    }

    ### 4.キャッシュの復元と必要ならキャッシュの更新を行う
    # キャッシュファイル名: grunt
    # キャッシュ変数名: __grunt_tasks
    function __grunt_retrieve_update_cache() {
    if ( ! (( $+__grunt_tasks )) \
    || _cache_invalid 'grunt' ) \
    && ! _retrieve_cache 'grunt'; then
    ### キャッシュの更新が必要
    # 新たに grunt のタスク情報を取得
    __grunt_tasks=(${(f)"$(__grunt_get_tasks)"})
    ### キャッシュを保存
    _store_cache 'grunt' __grunt_tasks
    return 0
    fi
    return 1
    }

    ### grunt のタスク情報を取得
    # 実装はダミーです
    function __grunt_get_tasks() {
    local tasks
    ### grunt --help を実行
    tasks=$(grunt --help)

    ### タスク情報を加工して以下の形式に変換
    # 形式:
    # タスク名:タスクの説明
    # 例:
    # clean:Clean files and folders
    tasks=( \
    "hoge:hoge task" \
    "fuga:fuga task" \
    "clean:Clean files and folders" \
    )
    # 配列を改行で連結して出力
    echo "${(j:\n:)tasks}"
    }

    ### 5.cache-policy に設定する関数
    # $1 にキャッシュファイルのパスが入っています
    #
    # 戻り値:
    # 真 (return 0) : キャッシュ無効
    # 偽 (return 非ゼロ) : キャッシュ有効
    function __grunt_caching_policy() {
    local -a oldp
    # Nm+7 と指定するとキャッシュの有効期限が 7日 となる
    # Nmw+1 と指定すると 1週間
    # Nmh+1 と指定すると 1時間
    # Nmm+1 と指定すると 1分
    oldp=( "$1"(Nm+7) )
    (( $#oldp ))
    }

    _grunt "$@"