Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

zsh-completionsの基礎の学びながらBF系言語生成ツール「Orenono」を使って「けものフレンズ言語」を作り、補完機能を使いながら呼び出す

f:id:tbpg:20170216014407p:plain

zsh-completionsの基礎について。
※この記事は自分用のメモ。既出情報+けもの情報です

zsh-completionsとは?

シェルの入力文字列の補完を行うためのZshの関数。

補完関数の作成について

ファイル名の規約

関数を保存するファイル名はアンダースコア( _ )で開始する。

変数の規約

変数 fpath に関数ファイルへのパスを追加する。
一般的には ~/.zshrc などに追加しておく。

fpath=(~/some_dir $fpath)

ファイルの記載ルール

  • 1行目に #compdef command_name を記載する
#compdef command_name

テスト

Brainf*ck 系の言語ジェネレータ「Orenono」を補完可能にしてみます。

補完スクリプト

  • _orenono
#compdef orenono

local state

_orenono() {
  __orenono
}

__orenono() {
  readonly local DEPTH=2

  case $CURRENT in
    $DEPTH)
      _arguments \
        '*: :->subcommands'

      case $state in
        subcommands)
          _values \
            'subcommand' \
            'convert' \
            'execute' \
            'generate' \
            'init' \
            'help' \
            'version' \
            ;
          ;;
      esac
      ;;
    *)
      case $words[$DEPTH] in
        convert)
          __orenono_convert
          ;;
        execute)
          __orenono_execute
          ;;
        generate)
          __orenono_generate
          ;;
        init)
          __orenono_init
          ;;
        help)
          __orenono_help
          ;;
        version)
          __orenono_version
          ;;
        *)
          # if does not match any subcommand
          # complete rest arguments
          _files
          ;;
      esac
      ;;
  esac
}



__orenono_convert() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_convert() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_execute() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_init() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_generate() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_help() {
  _arguments \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}

__orenono_version() {
  _arguments \
    {--help,--h}help message. \
    {--version}version \
    '*: :->rest'

  case $state in
    rest)
      # complete rest arguments
      _files
      ;;
  esac
}


compdef _orenono orenono

デモ

Orenonoを使って consomme/kemono_friends_lang と同じ動作をする けものフレンズ言語 を生成します

事前準備として以下のファイルを用意しておきます

  • hello.kemono
たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!なにこれなにこれ!たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!たーのしー!なにこれなにこれ!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!なにこれなにこれ!なにこれなにこれ!たーのしー!たーのしー!たーのしー!なにこれなにこれ!うわー!すっごーい!わーい!たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!なにこれなにこれ!たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!なにこれなにこれ!たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!なにこれなにこれ!たーのしー!たーのしー!たーのしー!なにこれなにこれ!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!なにこれなにこれ!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!すっごーい!なにこれなにこれ!うわー!すっごーい!わーい!たのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!うわー!すごーい!たーのしー!たーのしー!たーのしー!たーのしー!たのしー!すっごーい!わーい!すごーい!たーのしー!なにこれなにこれ!うわー!すっごーい!わーい!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!たーのしー!なにこれなにこれ!
  1. orenono init で設定ファイルを生成
  2. 設定ファイルを「けものフレンズ言語」仕様に編集
increment "たーのしー!"
decrement "すっごーい!"
start_loop "うわー!"
end_loop "わーい!"
next_cursol "たのしー!"
previous_cursol "すごーい!"
display "なにこれなにこれ!"
read "おもしろーい!"
  1. orenono execute hello.kemono で「けものフレンズ言語」を実行する

f:id:tbpg:20170216014328g:plain

関連資料