Skip to content

Instantly share code, notes, and snippets.

@mneko22
Created December 16, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mneko22/a090708a57b262c0cda5e649a4e5437d to your computer and use it in GitHub Desktop.
Save mneko22/a090708a57b262c0cda5e649a4e5437d to your computer and use it in GitHub Desktop.
eshellのプロンプトにgitのカレントブランチ名を表示したい!

eshellのプロンプトにgitのカレントブランチ名を表示したい!

こんばんは.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内でこんな感じに実行してあげればおわり.

  1. is-repositoryでレポジトリを確認.nilだったら何もしない.
  2. current-branchでブランチ名を取得.
  3. プロンプトの文字列に結合.
 mneko  /home/mneko/Workspace/node/express_app <master>
 $

いい感じになったはず.

@mneko22
Copy link
Author

mneko22 commented Dec 16, 2018

急いで書いたので説明適当になりましたね...
後日書き直します

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment