cli.zsh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/usr/bin/env zsh
  2. function omz {
  3. [[ $# -gt 0 ]] || {
  4. _omz::help
  5. return 1
  6. }
  7. local command="$1"
  8. shift
  9. # Subcommand functions start with _ so that they don't
  10. # appear as completion entries when looking for `omz`
  11. (( $+functions[_omz::$command] )) || {
  12. _omz::help
  13. return 1
  14. }
  15. _omz::$command "$@"
  16. }
  17. function _omz {
  18. local -a cmds subcmds
  19. cmds=(
  20. 'help:Usage information'
  21. 'plugin:Commands for Oh My Zsh plugins management'
  22. 'pr:Commands for Oh My Zsh Pull Requests management'
  23. 'theme:Commands for Oh My Zsh themes management'
  24. 'update:Update Oh My Zsh'
  25. )
  26. if (( CURRENT == 2 )); then
  27. _describe 'command' cmds
  28. elif (( CURRENT == 3 )); then
  29. case "$words[2]" in
  30. plugin) subcmds=('list:List plugins')
  31. _describe 'command' subcmds ;;
  32. pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
  33. _describe 'command' subcmds ;;
  34. theme) subcmds=('use:Load a theme' 'list:List themes')
  35. _describe 'command' subcmds ;;
  36. esac
  37. elif (( CURRENT == 4 )); then
  38. case "$words[2]::$words[3]" in
  39. theme::use) compadd "$ZSH"/themes/*.zsh-theme(.N:t:r) \
  40. "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::) ;;
  41. esac
  42. fi
  43. return 0
  44. }
  45. compdef _omz omz
  46. function _omz::help {
  47. cat <<EOF
  48. Usage: omz <command> [options]
  49. Available commands:
  50. help Print this help message
  51. plugin <command> Manage plugins
  52. pr <command> Manage Oh My Zsh Pull Requests
  53. theme <command> Manage themes
  54. update Update Oh My Zsh
  55. EOF
  56. }
  57. function _omz::confirm {
  58. # If question supplied, ask it before reading the answer
  59. # NOTE: uses the logname of the caller function
  60. if [[ -n "$1" ]]; then
  61. _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  62. fi
  63. # Read one character
  64. read -r -k 1
  65. # If no newline entered, add a newline
  66. if [[ "$REPLY" != $'\n' ]]; then
  67. echo
  68. fi
  69. }
  70. function _omz::log {
  71. # if promptsubst is set, a message with `` or $()
  72. # will be run even if quoted due to `print -P`
  73. setopt localoptions nopromptsubst
  74. # $1 = info|warn|error|debug
  75. # $2 = text
  76. # $3 = (optional) name of the logger
  77. local logtype=$1
  78. local logname=${3:-${${functrace[1]#_}%:*}}
  79. # Don't print anything if debug is not active
  80. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  81. return
  82. fi
  83. # Choose coloring based on log type
  84. case "$logtype" in
  85. prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
  86. debug) print -P "%F{white}$logname%f: $2" ;;
  87. info) print -P "%F{green}$logname%f: $2" ;;
  88. warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
  89. error) print -P "%S%F{red}$logname%f%s: $2" ;;
  90. esac >&2
  91. }
  92. function _omz::plugin {
  93. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  94. cat <<EOF
  95. Usage: omz plugin <command> [options]
  96. Available commands:
  97. list List all available Oh My Zsh plugins
  98. EOF
  99. return 1
  100. }
  101. local command="$1"
  102. shift
  103. _omz::plugin::$command "$@"
  104. }
  105. function _omz::plugin::list {
  106. local -a custom_plugins builtin_plugins
  107. custom_plugins=("$ZSH_CUSTOM"/plugins/*(/N:t))
  108. builtin_plugins=("$ZSH"/plugins/*(/N:t))
  109. {
  110. (( ${#custom_plugins} )) && {
  111. print -Pn "%U%BCustom plugins%b%u: "
  112. print -l ${(q-)custom_plugins}
  113. }
  114. (( ${#builtin_plugins} )) && {
  115. # add a line of separation
  116. (( ${#custom_plugins} )) && echo
  117. print -Pn "%U%BBuilt-in plugins%b%u: "
  118. print -l ${(q-)builtin_plugins}
  119. }
  120. } | fmt -w $COLUMNS | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC
  121. }
  122. function _omz::pr {
  123. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  124. cat <<EOF
  125. Usage: omz pr <command> [options]
  126. Available commands:
  127. clean Delete all PR branches (ohmyzsh/pull-*)
  128. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  129. EOF
  130. return 1
  131. }
  132. local command="$1"
  133. shift
  134. _omz::pr::$command "$@"
  135. }
  136. function _omz::pr::clean {
  137. (
  138. set -e
  139. builtin cd -q "$ZSH"
  140. # Check if there are PR branches
  141. local fmt branches
  142. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  143. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  144. # Exit if there are no PR branches
  145. if [[ -z "$branches" ]]; then
  146. _omz::log info "there are no Pull Request branches to remove."
  147. return
  148. fi
  149. # Print found PR branches
  150. echo "$branches\n"
  151. # Confirm before removing the branches
  152. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  153. # Only proceed if the answer is a valid yes option
  154. [[ "$REPLY" != [yY$'\n'] ]] && return
  155. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  156. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  157. command git branch -D "$branch"
  158. done
  159. )
  160. }
  161. function _omz::pr::test {
  162. # Allow $1 to be a URL to the pull request
  163. if [[ "$1" = https://* ]]; then
  164. 1="${1:t}"
  165. fi
  166. # Check the input
  167. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  168. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  169. return 1
  170. fi
  171. # Save current git HEAD
  172. local branch
  173. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  174. _omz::log error "error when getting the current git branch. Aborting..."
  175. return 1
  176. }
  177. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  178. # If any of these operations fail, undo the changes made
  179. (
  180. set -e
  181. builtin cd -q "$ZSH"
  182. # Get the ohmyzsh git remote
  183. command git remote -v | while read remote url _; do
  184. case "$url" in
  185. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  186. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  187. esac
  188. done
  189. (( $found )) || {
  190. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  191. return 1
  192. }
  193. # Fetch pull request head
  194. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  195. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  196. _omz::log error "error when trying to fetch PR #$1."
  197. return 1
  198. }
  199. # Rebase pull request branch against the current master
  200. _omz::log info "rebasing PR #$1..."
  201. command git rebase master ohmyzsh/pull-$1 || {
  202. command git rebase --abort &>/dev/null
  203. _omz::log warn "could not rebase PR #$1 on top of master."
  204. _omz::log warn "you might not see the latest stable changes."
  205. _omz::log info "run \`zsh\` to test the changes."
  206. return 1
  207. }
  208. _omz::log info "fetch of PR #${1} successful."
  209. )
  210. # If there was an error, abort running zsh to test the PR
  211. [[ $? -eq 0 ]] || return 1
  212. # Run zsh to test the changes
  213. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  214. command zsh -l
  215. # After testing, go back to the previous HEAD if the user wants
  216. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  217. # Only proceed if the answer is a valid yes option
  218. [[ "$REPLY" != [yY$'\n'] ]] && return
  219. (
  220. set -e
  221. builtin cd -q "$ZSH"
  222. command git checkout "$branch" -- || {
  223. _omz::log error "could not go back to the previous branch ('$branch')."
  224. return 1
  225. }
  226. )
  227. }
  228. function _omz::theme {
  229. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  230. cat <<EOF
  231. Usage: omz theme <command> [options]
  232. Available commands:
  233. list List all available Oh My Zsh themes
  234. use <theme> Load an Oh My Zsh theme
  235. EOF
  236. return 1
  237. }
  238. local command="$1"
  239. shift
  240. _omz::theme::$command "$@"
  241. }
  242. function _omz::theme::list {
  243. local -a custom_themes builtin_themes
  244. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  245. builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r))
  246. {
  247. (( ${#custom_themes} )) && {
  248. print -Pn "%U%BCustom themes%b%u: "
  249. print -l ${(q-)custom_themes}
  250. }
  251. (( ${#builtin_themes} )) && {
  252. # add a line of separation
  253. (( ${#custom_themes} )) && echo
  254. print -Pn "%U%BBuilt-in themes%b%u: "
  255. print -l ${(q-)builtin_themes}
  256. }
  257. } | fmt -w $COLUMNS | sed -E $'s/\e?(\\[[0-9]*m)/\e\\1/g' # deal with fmt removing ESC
  258. }
  259. function _omz::theme::use {
  260. if [[ -z "$1" ]]; then
  261. echo >&2 "Usage: omz theme use <theme>"
  262. return 1
  263. fi
  264. # Respect compatibility with old lookup order
  265. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  266. source "$ZSH_CUSTOM/$1.zsh-theme"
  267. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  268. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  269. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  270. source "$ZSH/themes/$1.zsh-theme"
  271. else
  272. _omz::log error "theme '$1' not found"
  273. return 1
  274. fi
  275. }
  276. function _omz::update {
  277. # Run update script
  278. env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
  279. # Update last updated file
  280. zmodload zsh/datetime
  281. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  282. # Remove update lock if it exists
  283. command rm -rf "$ZSH/log/update.lock"
  284. }