【Vimperator】GitHubからgit cloneするコマンド:GitCloneを作った


「Clone or download」をポチって、コピーして、git clone コピペするのが面倒なので、Vimpertorで:GitCloneと打てばブラウザで今見ているリポジトリをgit cloneできるコマンドを作った。
環境はMacを想定。

「Clone or download」に書いてあるURLを取得する関数を書く。

.vimperatorrc
javascript <<EOT
function getGHUrl() {
  var input = content.document.querySelector('input.input-mini.input-monospace.js-url-field.js-zeroclipboard-target') || content.document.querySelector('input.form-control.input-monospace.input-sm.js-zeroclipboard-target.js-url-field');
  return input.value;
}
EOT

現状のGitHubでも、更新が遅いGitHub Enterpriseで対応できるように、querySelectorを書いておく。

後は、コマンドを書く。

.vimperatorrc
command! -nargs=0 -description="git clone" GitClone js io.system('(output=$((test -d ~/git || mkdir ~/git) && cd ~/git && git clone ' + getGHUrl() + ' 2>&1) && osascript -e "display notification \\\"$output\\\" with title \\\"Vimperator\\\"") &')

~/git以下にgit cloneする設定。git cloneが終わったら出力を通知する。ブラウザが固まらない様に全体を()で囲み、&を付けてバックグラウンドで実行している。
:GitCloneで実行する。

自分はghqを使っているので、:Ghqというコマンドも作っておく。

.vimperatorrc
command! -nargs=0 -description="ghq" Ghq js io.system('(output=$(~/bin/ghq get ' + getGHUrl() + ' | /usr/local/bin/gsed -r "s/\\x1B\\[([0-9]{1,3}(;[0-9]{1,3})*)?[mK]//g") && osascript -e "display notification \\\"$output\\\" with title \\\"Vimperator\\\"") &')

ghqは~/bin/ghqとする。ghqの出力はカラーなので、そのシーケンスを

/usr/local/bin/gsed -r "s/\\x1B\\[([0-9]{1,3}(;[0-9]{1,3})*)?[mK]//g"

の部分でとっている。Macに標準で入っているsedは拡張正規表現を使う-rオプションがないので、brew install gnu-sedで入れておく。

関連