cli.zsh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. # If the command is being piped, print all found line by line
  110. if [[ ! -t 1 ]]; then
  111. print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
  112. return
  113. fi
  114. if (( ${#custom_plugins} )); then
  115. print -P "%U%BCustom plugins%b%u:"
  116. print -l ${(q-)custom_plugins} | column
  117. fi
  118. if (( ${#builtin_plugins} )); then
  119. (( ${#custom_plugins} )) && echo # add a line of separation
  120. print -P "%U%BBuilt-in plugins%b%u:"
  121. print -l ${(q-)builtin_plugins} | column
  122. fi
  123. }
  124. function _omz::pr {
  125. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  126. cat <<EOF
  127. Usage: omz pr <command> [options]
  128. Available commands:
  129. clean Delete all PR branches (ohmyzsh/pull-*)
  130. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  131. EOF
  132. return 1
  133. }
  134. local command="$1"
  135. shift
  136. _omz::pr::$command "$@"
  137. }
  138. function _omz::pr::clean {
  139. (
  140. set -e
  141. builtin cd -q "$ZSH"
  142. # Check if there are PR branches
  143. local fmt branches
  144. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  145. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  146. # Exit if there are no PR branches
  147. if [[ -z "$branches" ]]; then
  148. _omz::log info "there are no Pull Request branches to remove."
  149. return
  150. fi
  151. # Print found PR branches
  152. echo "$branches\n"
  153. # Confirm before removing the branches
  154. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  155. # Only proceed if the answer is a valid yes option
  156. [[ "$REPLY" != [yY$'\n'] ]] && return
  157. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  158. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  159. command git branch -D "$branch"
  160. done
  161. )
  162. }
  163. function _omz::pr::test {
  164. # Allow $1 to be a URL to the pull request
  165. if [[ "$1" = https://* ]]; then
  166. 1="${1:t}"
  167. fi
  168. # Check the input
  169. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  170. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  171. return 1
  172. fi
  173. # Save current git HEAD
  174. local branch
  175. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  176. _omz::log error "error when getting the current git branch. Aborting..."
  177. return 1
  178. }
  179. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  180. # If any of these operations fail, undo the changes made
  181. (
  182. set -e
  183. builtin cd -q "$ZSH"
  184. # Get the ohmyzsh git remote
  185. command git remote -v | while read remote url _; do
  186. case "$url" in
  187. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  188. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  189. esac
  190. done
  191. (( $found )) || {
  192. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  193. return 1
  194. }
  195. # Fetch pull request head
  196. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  197. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  198. _omz::log error "error when trying to fetch PR #$1."
  199. return 1
  200. }
  201. # Rebase pull request branch against the current master
  202. _omz::log info "rebasing PR #$1..."
  203. command git rebase master ohmyzsh/pull-$1 || {
  204. command git rebase --abort &>/dev/null
  205. _omz::log warn "could not rebase PR #$1 on top of master."
  206. _omz::log warn "you might not see the latest stable changes."
  207. _omz::log info "run \`zsh\` to test the changes."
  208. return 1
  209. }
  210. _omz::log info "fetch of PR #${1} successful."
  211. )
  212. # If there was an error, abort running zsh to test the PR
  213. [[ $? -eq 0 ]] || return 1
  214. # Run zsh to test the changes
  215. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  216. command zsh -l
  217. # After testing, go back to the previous HEAD if the user wants
  218. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  219. # Only proceed if the answer is a valid yes option
  220. [[ "$REPLY" != [yY$'\n'] ]] && return
  221. (
  222. set -e
  223. builtin cd -q "$ZSH"
  224. command git checkout "$branch" -- || {
  225. _omz::log error "could not go back to the previous branch ('$branch')."
  226. return 1
  227. }
  228. )
  229. }
  230. function _omz::theme {
  231. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  232. cat <<EOF
  233. Usage: omz theme <command> [options]
  234. Available commands:
  235. list List all available Oh My Zsh themes
  236. use <theme> Load an Oh My Zsh theme
  237. EOF
  238. return 1
  239. }
  240. local command="$1"
  241. shift
  242. _omz::theme::$command "$@"
  243. }
  244. function _omz::theme::list {
  245. local -a custom_themes builtin_themes
  246. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  247. builtin_themes=("$ZSH"/themes/*.zsh-theme(.N:t:r))
  248. # If the command is being piped, print all found line by line
  249. if [[ ! -t 1 ]]; then
  250. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  251. return
  252. fi
  253. if (( ${#custom_themes} )); then
  254. print -P "%U%BCustom themes%b%u:"
  255. print -l ${(q-)custom_themes} | column
  256. fi
  257. if (( ${#builtin_themes} )); then
  258. (( ${#custom_themes} )) && echo # add a line of separation
  259. print -P "%U%BBuilt-in themes%b%u:"
  260. print -l ${(q-)builtin_themes} | column
  261. fi
  262. }
  263. function _omz::theme::use {
  264. if [[ -z "$1" ]]; then
  265. echo >&2 "Usage: omz theme use <theme>"
  266. return 1
  267. fi
  268. # Respect compatibility with old lookup order
  269. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  270. source "$ZSH_CUSTOM/$1.zsh-theme"
  271. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  272. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  273. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  274. source "$ZSH/themes/$1.zsh-theme"
  275. else
  276. _omz::log error "theme '$1' not found"
  277. return 1
  278. fi
  279. }
  280. function _omz::update {
  281. # Run update script
  282. env ZSH="$ZSH" sh "$ZSH/tools/upgrade.sh"
  283. # Update last updated file
  284. zmodload zsh/datetime
  285. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  286. # Remove update lock if it exists
  287. command rm -rf "$ZSH/log/update.lock"
  288. }