こんばんは.matusnekoです.
これはなにかの Advent Calendar 2018の16日目の記事です. https://adventar.org/calendars/3092
はてなのほうをまだ復旧させていないのでこちらに投稿します.
eshell使い始めてhelmを使って履歴検索など公開されているパッケージを組み合わせて楽しく過ごしていたのですが, 唐突にプロンプトに今いるブランチの名前表示したいなと思いついたので,関数型プログラミング入門もかねて書いてみました.
書いたコードはこんな感じです.
(defun is-repository ()
(vc-find-root (string-trim (shell-command-to-string "pwd")) ".git"))
(defun current-branch()
(string-trim (shell-command-to-string "git symbolic-ref --short HEAD")))
(setq eshell-prompt-function
(lambda nil
(concat
(propertize (concat "\s" (user-login-name) "\s") 'face `(:foreground "black" :background "#3b83f7"))
(propertize (concat "\s" (eshell/pwd) "\s") 'face `(:foreground "black" :background "orange"))
(if (is-repository)
(propertize (concat "<" (current-branch) ">") 'face `(:foreground "orange")))
(propertize "\n\s$" 'face `(:foreground "green")) "\s")))
eshell-prompt-function がpromptを表示をする関数で,その処理を4行目から定義しています.と思ったけど変数だ.
is-repository はカレントディレクトリがgitのレポジトリか判断する関数です.簡単に説明するとカレントディレクトリから/に向かって.gitを探し,
見つからなければnilを返してくれます.
current-branchはgitのカレントブランチを取得する関数です.
それらをeshell-prompt-function内でこんな感じに実行してあげればおわり.
is-repositoryでレポジトリを確認.nilだったら何もしない.current-branchでブランチ名を取得.- プロンプトの文字列に結合.
mneko /home/mneko/Workspace/node/express_app <master>
$
いい感じになったはず.
急いで書いたので説明適当になりましたね...
後日書き直します