cli.zsh 9.6 KB

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