fish_prompt.fish 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # name: syl20bnr
  2. # ----------------------------------------------------------------------------
  3. # Utils
  4. # ----------------------------------------------------------------------------
  5. set -g __syl20bnr_display_rprompt 1
  6. function toggle_right_prompt -d "Toggle the right prompt of the syl20bnr theme"
  7. if test $__syl20bnr_display_rprompt -eq 0
  8. echo "enable right prompt"
  9. set __syl20bnr_display_rprompt 1
  10. else
  11. echo "disable right prompt"
  12. set __syl20bnr_display_rprompt 0
  13. end
  14. end
  15. function __syl20bnr_git_branch_name -d "Return the current branch name"
  16. echo (command git symbolic-ref HEAD 2> /dev/null | sed -e 's|^refs/heads/||')
  17. end
  18. function __syl20bnr_git_repo_name -d "Return the current repository name"
  19. echo (command basename (git rev-parse --show-toplevel 2> /dev/null))
  20. end
  21. function __syl20bnr_git_repo_base -d "Return the current repository name"
  22. echo (command git rev-parse --show-toplevel 2> /dev/null)
  23. end
  24. function __syl20bnr_git_status -d "git status command"
  25. git status -b -s --ignore-submodules=dirty
  26. end
  27. function __syl20bnr_unpushed_commit_count -d "Return the number of unpushed commits"
  28. echo $argv[1] | grep -E -o "ahead\ [0-9]+" | awk '{print $2}'
  29. end
  30. function __syl20bnr_unmerged_commit_count -d "Return the number of unmerged commits"
  31. echo $argv[1] | grep -E -o "behind\ [0-9]+" | awk '{print $2}'
  32. end
  33. # ----------------------------------------------------------------------------
  34. # Aliases
  35. # ----------------------------------------------------------------------------
  36. alias trp toggle_right_prompt
  37. # ----------------------------------------------------------------------------
  38. # Prompts
  39. # ----------------------------------------------------------------------------
  40. function fish_prompt -d "Write out the left prompt of the syl20bnr theme"
  41. set -l last_status $status
  42. set -l basedir_name (basename (prompt_pwd))
  43. # Init colors
  44. set -l colcyan (set_color cyan)
  45. set -l colbcyan (set_color -o cyan)
  46. set -l colgreen (set_color green)
  47. set -l colbgreen (set_color -o green)
  48. set -l colnormal (set_color normal)
  49. set -l colred (set_color red)
  50. set -l colbred (set_color -o red)
  51. set -l colwhite (set_color white)
  52. set -l colbwhite (set_color -o white)
  53. # Segments
  54. # git
  55. # If inside a git repo then the pwd segment is replaced by the git
  56. # segment.
  57. # The git segment format is X:YI@Z:P(N) where:
  58. # X is git
  59. # Y is the current branch
  60. # I is the information about the current repo state
  61. # Z is the name of the repo
  62. # P is the current working path basename (name of the current directory)
  63. # C is the depth of the path starting from base directory of the repo
  64. # The displayed information is:
  65. # Unpushed commits are indicated with up arrows
  66. # The number of unpushed commits is indicated right after the up arrows
  67. # Note:
  68. # Dirtiness is indicated by the color of the branch name, red is dirty,
  69. # green is up-to-date.
  70. # If P = Z then P(C) is not displayed
  71. set -l ps_git ""
  72. set -l git_branch_name (__syl20bnr_git_branch_name)
  73. if test -n "$git_branch_name"
  74. set -l git_repo_name (__syl20bnr_git_repo_name)
  75. set -l git_info ""
  76. set -l git_status (__syl20bnr_git_status)
  77. if echo $git_status | grep ahead > /dev/null
  78. set git_info "["$colbgreen"↑"(__syl20bnr_unpushed_commit_count $git_status)$colnormal"]"
  79. end
  80. if echo $git_status | grep behind > /dev/null
  81. set git_info "$git_info""["$colbred"↓"(__syl20bnr_unmerged_commit_count $git_status)$colnormal"]"
  82. end
  83. set -l colbranch $colbgreen
  84. if echo $git_status | grep -E "\s\?\?\s|\sM\s|\sD\s" > /dev/null
  85. set colbranch $colbred
  86. end
  87. set ps_git $colbwhite"git:"$colbcyan$git_branch_name$git_info$colnormal"@"$colbranch$git_repo_name
  88. if test "$basedir_name" != "$git_repo_name"
  89. set -l basedir_depth (echo (__syl20bnr_git_repo_base) | sed "s/\// /g" | wc -w)
  90. set -l depth (echo (pwd) | sed "s/\// /g" | wc -w)
  91. set depth (math $depth - $basedir_depth)
  92. set ps_git $ps_git$colnormal":"$colbwhite$basedir_name$colnormal"("$depth")"
  93. end
  94. end
  95. # pwd
  96. # The pwd segment format is X:P(C) where:
  97. # X is either home or /
  98. # P is the current working path basename (name of the current directory)
  99. # C is the depth of the path starting from X
  100. # If the pwd is home or / then the prompt format is simplified to 'home' or
  101. # '/' without the current directory and depth.
  102. set -l ps_pwd ""
  103. if test -z "$ps_git"
  104. set -l depth (echo (pwd) | sed "s/\// /g" | wc -w)
  105. set -l in_home (echo (pwd) | grep ~)
  106. if test -n "$in_home"
  107. set ps_pwd $colbwhite"home"
  108. else
  109. set ps_pwd $colbwhite"/"
  110. end
  111. if test (echo (pwd)) != ~ -a (echo (pwd)) != /
  112. set ps_pwd $ps_pwd":"$colgreen$basedir_name
  113. if test -n "$in_home"
  114. set depth (math $depth - 2)
  115. end
  116. set ps_pwd $ps_pwd$colnormal"("$depth")"
  117. end
  118. end
  119. # vi mode
  120. # If vi_mode plugin or native vi mode is activated then print the vi mode
  121. # in the prompt.
  122. set -l ps_vi ""
  123. if test -n "$vi_mode"
  124. set ps_vi $colnormal"["$vi_mode$colnormal"]"
  125. end
  126. if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "my_fish_key_bindings"
  127. switch $fish_bind_mode
  128. case default
  129. set ps_vi $colnormal"("$colred"N"$colnormal")"
  130. case insert
  131. set ps_vi $colnormal"("$colgreen"I"$colnormal")"
  132. case visual
  133. set ps_vi $colnormal"("$colwhite"V"$colnormal")"
  134. end
  135. end
  136. # end of prompt
  137. # The color of the end of the prompt depends on the $status value of the
  138. # last executed command. It is green or red depending on the last command
  139. # success or failure respectively.
  140. # Since I often use ranger and use its 'shift+s' key binding to bring a shell
  141. # session, there is discreet indicator when the parent process of the current
  142. # shell pid is a ranger process. In this case the end of the prompt is written
  143. # twice.
  144. # With this indicator I can quickly remember that I can "ctrl+d" to end the
  145. # the current shell process and get back to the ranger process.
  146. set -l ps_end ">"
  147. # indicator for ranger parent process
  148. set -l ranger ""
  149. set -l os (uname)
  150. if test "$os" = "Darwin"
  151. if pstree -s ranger | grep (echo %self) | grep -v grep > /dev/null
  152. set ranger 1
  153. end
  154. end
  155. if test "$os" = "Linux"
  156. if pstree -p -l | grep "fish("(echo %self)")" | grep 'ranger([0-9]*)' > /dev/null
  157. set ranger 1
  158. end
  159. end
  160. if test -n "$ranger"
  161. set ps_end $ps_end$ps_end
  162. end
  163. # last status give the color of the right arrows at the end of the prompt
  164. if test $last_status -ne 0
  165. set ps_end $colnormal$colbred$ps_end
  166. else
  167. set ps_end $colnormal$colgreen$ps_end
  168. end
  169. # Left Prompt
  170. echo -n -s $ps_git $ps_pwd $ps_vi $ps_git_dirty $ps_end ' '
  171. end
  172. function fish_right_prompt -d "Write out the right prompt of the syl20bnr theme"
  173. set -l colnormal (set_color normal)
  174. # Segments
  175. # The where segment format is X@Y where:
  176. # X is the username
  177. # Y is the hostname
  178. set -l ps_where $colnormal(whoami)@(hostname|cut -d . -f 1)
  179. # Right Prompt
  180. if test $__syl20bnr_display_rprompt -eq 1
  181. echo -n -s $ps_where
  182. end
  183. end