pockestrap

Programmer's memo

tmuxinator の代替ツールを作っている話

github.com

ptmux の p は pocke の p です。

何故作っているのか

私はずっとtmuxを使っているのですが、tmuxinatorなどのツールは使わずにいました。 最近、遅ればせながらtmuxinatorへの興味が高まったので試してみたところ、下記の不満点があることが分かりました。

  • windowの名前を設定されてしまう
    • 実行プロセス名が出てほしいので困る
  • ~/.tmuxinator/下に設定ファイルが作られる(~/.config/下を使って欲しい)
  • 設定ファイルがなんか気持ち悪い
    • 感情
  • コマンドラインツールにわざわざgem installしたくない
    • 気分

上記の不満を解消するために新しくツールを自作することにしました。

つくりかた

tmuxinatorの実装を軽く見てみたところ、設定ファイルからいい感じにシェルスクリプトを吐いているようでした。 例えば、以下のように設定ファイルがシェルスクリプトに変換されます。

設定ファイル

name: hoge
root: ~/

windows:
  - editor:
      panes:
        - vim
        - bundle exec guard
  - server: 
      panes:
        - bundle exec rails s
        - bundle exec sidekiq
  - logs: tail -f log/development.log

吐き出されるスクリプト

#!/bin/zsh

# Clear rbenv variables before starting tmux
unset RBENV_VERSION
unset RBENV_DIR

tmux start-server;

  cd /home/pocke

  # Run pre command.
  

  # Create the session and the first window. Manually switch to root
  # directory if required to support tmux < 1.9
  TMUX= tmux new-session -d -s hoge -n editor
  tmux send-keys -t hoge:1 cd\ /home/pocke C-m


  # Create other windows.
  tmux new-window -c /home/pocke -t hoge:2 -n server
  tmux new-window  -t hoge:3 -n logs


  # Window "editor"
  tmux send-keys -t hoge:1.1 vim C-m

  tmux splitw -c /home/pocke -t hoge:1
  tmux select-layout -t hoge:1 tiled
  tmux send-keys -t hoge:1.2 bundle\ exec\ guard C-m

  tmux select-layout -t hoge:1 tiled

  tmux select-layout -t hoge:1 
  tmux select-pane -t hoge:1.1


  # Window "server"
  tmux send-keys -t hoge:2.1 bundle\ exec\ rails\ s C-m

  tmux splitw -c /home/pocke -t hoge:2
  tmux select-layout -t hoge:2 tiled
  tmux send-keys -t hoge:2.2 bundle\ exec\ sidekiq C-m

  tmux select-layout -t hoge:2 tiled

  tmux select-layout -t hoge:2 
  tmux select-pane -t hoge:2.1


  # Window "logs"
  tmux send-keys -t hoge:3 tail\ -f\ log/development.log C-m


  tmux select-window -t 1
  tmux select-pane -t 0

  if [ -z "$TMUX" ]; then
    tmux -u attach-session -t hoge
  else
    tmux -u switch-client -t hoge
  fi

上記のスクリプトKernel.exec()されます。

ptmuxでもこの方式を取ることにしました。

ptmuxの使い方

ptmuxはGoで書かれているので、go getでインストールすることが出来ます。

go get github.com/pocke/ptmux

また、設定はtmuxinatorと同様にYAMLで記述しますが、設定ファイルの設置場所は~/.config/ptmux/PROFILE_NAME.yamlになります。

先ほどと同様の設定ファイルは、以下のようになります。

root: ~/

windows:
  - panes:
    - command: 'vim'
    - command: 'bundle exec guard'
  - panes:
    - command: 'bundle exec rails s'
    - command: 'bundle exec sidekiq'
  - panes:
    - command: 'tail -f log/development.log'

これを~/.config/ptmux/hoge.yamlとして保存し、以下のコマンドを実行すると設定通りにtmuxが起動します。

ptmux hoge

ptmuxが今後歩む道

とりあえず実装が雑な部分があるので、直そうと思っています。

また、本家tmuxinatorから機能の移植はしようかな、とも思いつつ、現状で私が満足しているのでしないかもしれません。 windowやsessionに名前を付ける機能は、(自分が必要としているかはかなり微妙ですが)つけるかもしれません。

ptmux独自の路線としては、YAML以外の設定ファイルのサポートなども少し考えています。 もしかしたらTOMLで書きやすい設定ファイルなのでは…?とかちょっと思っているので、試すだけ試して見るかも知れません。m

そして、現状テストを一切書けていないので書きたいですね。

まとめ

Rubyが嫌いな方、ホームディレクトリ直下にディレクトリを切られるなんてありえない!という方、tmuxinatorの設定ファイルにアレルギー反応が出てしまう方などはptmuxの使用を検討してみてはいかがでしょうか?

もし使用してみて感想などありましたら、フィードバックを頂けるとめちゃ嬉しいです。