cli.zsh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. 'changelog:Print the changelog'
  21. 'help:Usage information'
  22. 'plugin:Manage plugins'
  23. 'pr:Manage Oh My Zsh Pull Requests'
  24. 'theme:Manage themes'
  25. 'update:Update Oh My Zsh'
  26. )
  27. if (( CURRENT == 2 )); then
  28. _describe 'command' cmds
  29. elif (( CURRENT == 3 )); then
  30. case "$words[2]" in
  31. changelog) local -a refs
  32. refs=("${(@f)$(command git for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
  33. _describe 'command' refs ;;
  34. plugin) subcmds=(
  35. 'disable:Disable plugin(s)'
  36. 'enable:Enable plugin(s)'
  37. 'info:Get plugin information'
  38. 'list:List plugins'
  39. 'load:Load plugin(s)'
  40. )
  41. _describe 'command' subcmds ;;
  42. pr) subcmds=('test:Test a Pull Request' 'clean:Delete all Pull Request branches')
  43. _describe 'command' subcmds ;;
  44. theme) subcmds=('use:Load a theme' 'list:List themes')
  45. _describe 'command' subcmds ;;
  46. esac
  47. elif (( CURRENT == 4 )); then
  48. case "${words[2]}::${words[3]}" in
  49. plugin::(disable|enable|load))
  50. local -aU valid_plugins
  51. if [[ "${words[3]}" = disable ]]; then
  52. # if command is "disable", only offer already enabled plugins
  53. valid_plugins=($plugins)
  54. else
  55. valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  56. # if command is "enable", remove already enabled plugins
  57. [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
  58. fi
  59. _describe 'plugin' valid_plugins ;;
  60. plugin::info)
  61. local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  62. _describe 'plugin' plugins ;;
  63. theme::use)
  64. local -aU themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  65. _describe 'theme' themes ;;
  66. esac
  67. elif (( CURRENT > 4 )); then
  68. case "${words[2]}::${words[3]}" in
  69. plugin::(enable|disable|load))
  70. local -aU valid_plugins
  71. if [[ "${words[3]}" = disable ]]; then
  72. # if command is "disable", only offer already enabled plugins
  73. valid_plugins=($plugins)
  74. else
  75. valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  76. # if command is "enable", remove already enabled plugins
  77. [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
  78. fi
  79. # Remove plugins already passed as arguments
  80. # NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which
  81. # has a space after them. This is to avoid removing plugins partially passed, which makes
  82. # the completion not add a space after the completed plugin.
  83. local -a args=(${words[4,$(( CURRENT - 1))]})
  84. valid_plugins=(${valid_plugins:|args})
  85. _describe 'plugin' valid_plugins ;;
  86. esac
  87. fi
  88. return 0
  89. }
  90. compdef _omz omz
  91. ## Utility functions
  92. function _omz::confirm {
  93. # If question supplied, ask it before reading the answer
  94. # NOTE: uses the logname of the caller function
  95. if [[ -n "$1" ]]; then
  96. _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  97. fi
  98. # Read one character
  99. read -r -k 1
  100. # If no newline entered, add a newline
  101. if [[ "$REPLY" != $'\n' ]]; then
  102. echo
  103. fi
  104. }
  105. function _omz::log {
  106. # if promptsubst is set, a message with `` or $()
  107. # will be run even if quoted due to `print -P`
  108. setopt localoptions nopromptsubst
  109. # $1 = info|warn|error|debug
  110. # $2 = text
  111. # $3 = (optional) name of the logger
  112. local logtype=$1
  113. local logname=${3:-${${functrace[1]#_}%:*}}
  114. # Don't print anything if debug is not active
  115. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  116. return
  117. fi
  118. # Choose coloring based on log type
  119. case "$logtype" in
  120. prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
  121. debug) print -P "%F{white}$logname%f: $2" ;;
  122. info) print -P "%F{green}$logname%f: $2" ;;
  123. warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
  124. error) print -P "%S%F{red}$logname%f%s: $2" ;;
  125. esac >&2
  126. }
  127. ## User-facing commands
  128. function _omz::help {
  129. cat <<EOF
  130. Usage: omz <command> [options]
  131. Available commands:
  132. help Print this help message
  133. changelog Print the changelog
  134. plugin <command> Manage plugins
  135. pr <command> Manage Oh My Zsh Pull Requests
  136. theme <command> Manage themes
  137. update Update Oh My Zsh
  138. EOF
  139. }
  140. function _omz::changelog {
  141. local version=${1:-HEAD} format=${3:-"--text"}
  142. if ! command git -C "$ZSH" show-ref --verify refs/heads/$version &>/dev/null && \
  143. ! command git -C "$ZSH" show-ref --verify refs/tags/$version &>/dev/null && \
  144. ! command git -C "$ZSH" rev-parse --verify "${version}^{commit}" &>/dev/null; then
  145. cat <<EOF
  146. Usage: omz changelog [version]
  147. NOTE: <version> must be a valid branch, tag or commit.
  148. EOF
  149. return 1
  150. fi
  151. "$ZSH/tools/changelog.sh" "$version" "${2:-}" "$format"
  152. }
  153. function _omz::plugin {
  154. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  155. cat <<EOF
  156. Usage: omz plugin <command> [options]
  157. Available commands:
  158. disable <plugin> Disable plugin(s)
  159. enable <plugin> Enable plugin(s)
  160. info <plugin> Get information of a plugin
  161. list List all available Oh My Zsh plugins
  162. load <plugin> Load plugin(s)
  163. EOF
  164. return 1
  165. }
  166. local command="$1"
  167. shift
  168. _omz::plugin::$command "$@"
  169. }
  170. function _omz::plugin::disable {
  171. if [[ -z "$1" ]]; then
  172. echo >&2 "Usage: omz plugin disable <plugin> [...]"
  173. return 1
  174. fi
  175. # Check that plugin is in $plugins
  176. local -a dis_plugins=()
  177. for plugin in "$@"; do
  178. if [[ ${plugins[(Ie)$plugin]} -eq 0 ]]; then
  179. _omz::log warn "plugin '$plugin' is not enabled."
  180. continue
  181. fi
  182. dis_plugins+=("$plugin")
  183. done
  184. # Exit if there are no enabled plugins to disable
  185. if [[ ${#dis_plugins} -eq 0 ]]; then
  186. return 1
  187. fi
  188. # Disable plugins awk script
  189. local awk_script="
  190. # if plugins=() is in oneline form, substitute disabled plugins and go to next line
  191. /^\s*plugins=\([^#]+\).*\$/ {
  192. sub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before
  193. sub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after
  194. sub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin)
  195. print \$0
  196. next
  197. }
  198. # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
  199. /^\s*plugins=\(/ {
  200. multi=1
  201. sub(/\s+(${(j:|:)dis_plugins})/, \"\")
  202. sub(/(${(j:|:)dis_plugins})\s+/, \"\")
  203. sub(/\((${(j:|:)dis_plugins})\)/, \"\")
  204. print \$0
  205. next
  206. }
  207. # if multi flag is enabled and we find a valid closing parenthesis,
  208. # add new plugins and disable multi flag
  209. multi == 1 && /^[^#]*\)/ {
  210. multi=0
  211. sub(/\s+(${(j:|:)dis_plugins})/, \"\")
  212. sub(/(${(j:|:)dis_plugins})\s+/, \"\")
  213. sub(/\((${(j:|:)dis_plugins})\)/, \"\")
  214. print \$0
  215. next
  216. }
  217. multi == 1 {
  218. sub(/\s+(${(j:|:)dis_plugins})/, \"\")
  219. sub(/(${(j:|:)dis_plugins})\s+/, \"\")
  220. sub(/\((${(j:|:)dis_plugins})\)/, \"\")
  221. print \$0
  222. next
  223. }
  224. { print \$0 }
  225. "
  226. awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \
  227. && mv ~/.zshrc ~/.zshrc.swp \
  228. && mv ~/.zshrc.disabled ~/.zshrc
  229. # Exit if the new .zshrc file wasn't created correctly
  230. [[ $? -eq 0 ]] || {
  231. local ret=$?
  232. _omz::log error "error disabling plugins."
  233. return $ret
  234. }
  235. # Exit if the new .zshrc file has syntax errors
  236. if ! zsh -n ~/.zshrc; then
  237. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  238. mv ~/.zshrc ~/.zshrc.disabled
  239. mv ~/.zshrc.swp ~/.zshrc
  240. return 1
  241. fi
  242. # Restart the zsh session if there were no errors
  243. _omz::log info ""
  244. # Old zsh versions don't have ZSH_ARGZERO
  245. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  246. # Check whether to run a login shell
  247. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  248. }
  249. function _omz::plugin::enable {
  250. if [[ -z "$1" ]]; then
  251. echo >&2 "Usage: omz plugin enable <plugin> [...]"
  252. return 1
  253. fi
  254. # Check that plugin is not in $plugins
  255. local -a add_plugins=()
  256. for plugin in "$@"; do
  257. if [[ ${plugins[(Ie)$plugin]} -ne 0 ]]; then
  258. _omz::log warn "plugin '$plugin' is already enabled."
  259. continue
  260. fi
  261. add_plugins+=("$plugin")
  262. done
  263. # Exit if there are no plugins to enable
  264. if [[ ${#add_plugins} -eq 0 ]]; then
  265. return 1
  266. fi
  267. # Enable plugins awk script
  268. local awk_script="
  269. # if plugins=() is in oneline form, substitute ) with new plugins and go to the next line
  270. /^\s*plugins=\([^#]+\).*\$/ {
  271. sub(/\)/, \" $add_plugins&\")
  272. print \$0
  273. next
  274. }
  275. # if plugins=() is in multiline form, enable multi flag
  276. /^\s*plugins=\(/ {
  277. multi=1
  278. }
  279. # if multi flag is enabled and we find a valid closing parenthesis,
  280. # add new plugins and disable multi flag
  281. multi == 1 && /^[^#]*\)/ {
  282. multi=0
  283. sub(/\)/, \" $add_plugins&\")
  284. print \$0
  285. next
  286. }
  287. { print \$0 }
  288. "
  289. awk "$awk_script" ~/.zshrc > ~/.zshrc.disabled \
  290. && mv ~/.zshrc ~/.zshrc.swp \
  291. && mv ~/.zshrc.disabled ~/.zshrc
  292. # Exit if the new .zshrc file wasn't created correctly
  293. [[ $? -eq 0 ]] || {
  294. local ret=$?
  295. _omz::log error "error disabling plugins."
  296. return $ret
  297. }
  298. # Exit if the new .zshrc file has syntax errors
  299. if ! zsh -n ~/.zshrc; then
  300. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  301. mv ~/.zshrc ~/.zshrc.disabled
  302. mv ~/.zshrc.swp ~/.zshrc
  303. return 1
  304. fi
  305. # Restart the zsh session if there were no errors
  306. # Old zsh versions don't have ZSH_ARGZERO
  307. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  308. # Check whether to run a login shell
  309. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  310. }
  311. function _omz::plugin::info {
  312. if [[ -z "$1" ]]; then
  313. echo >&2 "Usage: omz plugin info <plugin>"
  314. return 1
  315. fi
  316. local readme
  317. for readme in "$ZSH_CUSTOM/plugins/$1/README.md" "$ZSH/plugins/$1/README.md"; do
  318. if [[ -f "$readme" ]]; then
  319. (( ${+commands[less]} )) && less "$readme" || cat "$readme"
  320. return 0
  321. fi
  322. done
  323. if [[ -d "$ZSH_CUSTOM/plugins/$1" || -d "$ZSH/plugins/$1" ]]; then
  324. _omz::log error "the '$1' plugin doesn't have a README file"
  325. else
  326. _omz::log error "'$1' plugin not found"
  327. fi
  328. return 1
  329. }
  330. function _omz::plugin::list {
  331. local -a custom_plugins builtin_plugins
  332. custom_plugins=("$ZSH_CUSTOM"/plugins/*(-/N:t))
  333. builtin_plugins=("$ZSH"/plugins/*(-/N:t))
  334. # If the command is being piped, print all found line by line
  335. if [[ ! -t 1 ]]; then
  336. print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
  337. return
  338. fi
  339. if (( ${#custom_plugins} )); then
  340. print -P "%U%BCustom plugins%b%u:"
  341. print -l ${(q-)custom_plugins} | column
  342. fi
  343. if (( ${#builtin_plugins} )); then
  344. (( ${#custom_plugins} )) && echo # add a line of separation
  345. print -P "%U%BBuilt-in plugins%b%u:"
  346. print -l ${(q-)builtin_plugins} | column
  347. fi
  348. }
  349. function _omz::plugin::load {
  350. if [[ -z "$1" ]]; then
  351. echo >&2 "Usage: omz plugin load <plugin> [...]"
  352. return 1
  353. fi
  354. local plugins=("$@")
  355. local plugin base has_completion=0
  356. for plugin in $plugins; do
  357. if [[ -d "$ZSH_CUSTOM/plugins/$plugin" ]]; then
  358. base="$ZSH_CUSTOM/plugins/$plugin"
  359. elif [[ -d "$ZSH/plugins/$plugin" ]]; then
  360. base="$ZSH/plugins/$plugin"
  361. else
  362. _omz::log warn "plugin '$plugin' not found"
  363. continue
  364. fi
  365. # Check if its a valid plugin
  366. if [[ ! -f "$base/_$plugin" && ! -f "$base/$plugin.plugin.zsh" ]]; then
  367. _omz::log warn "'$plugin' is not a valid plugin"
  368. continue
  369. # It it is a valid plugin, add its directory to $fpath unless it is already there
  370. elif (( ! ${fpath[(Ie)$base]} )); then
  371. fpath=("$base" $fpath)
  372. fi
  373. # Check if it has completion to reload compinit
  374. if [[ -f "$base/_$plugin" ]]; then
  375. has_completion=1
  376. fi
  377. # Load the plugin
  378. if [[ -f "$base/$plugin.plugin.zsh" ]]; then
  379. source "$base/$plugin.plugin.zsh"
  380. fi
  381. done
  382. # If we have completion, we need to reload the completion
  383. # We pass -D to avoid generating a new dump file, which would overwrite our
  384. # current one for the next session (and we don't want that because we're not
  385. # actually enabling the plugins for the next session).
  386. # Note that we still have to pass -d "$_comp_dumpfile", so that compinit
  387. # doesn't use the default zcompdump location (${ZDOTDIR:-$HOME}/.zcompdump).
  388. if (( has_completion )); then
  389. compinit -D -d "$_comp_dumpfile"
  390. fi
  391. }
  392. function _omz::pr {
  393. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  394. cat <<EOF
  395. Usage: omz pr <command> [options]
  396. Available commands:
  397. clean Delete all PR branches (ohmyzsh/pull-*)
  398. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  399. EOF
  400. return 1
  401. }
  402. local command="$1"
  403. shift
  404. _omz::pr::$command "$@"
  405. }
  406. function _omz::pr::clean {
  407. (
  408. set -e
  409. builtin cd -q "$ZSH"
  410. # Check if there are PR branches
  411. local fmt branches
  412. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  413. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  414. # Exit if there are no PR branches
  415. if [[ -z "$branches" ]]; then
  416. _omz::log info "there are no Pull Request branches to remove."
  417. return
  418. fi
  419. # Print found PR branches
  420. echo "$branches\n"
  421. # Confirm before removing the branches
  422. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  423. # Only proceed if the answer is a valid yes option
  424. [[ "$REPLY" != [yY$'\n'] ]] && return
  425. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  426. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  427. command git branch -D "$branch"
  428. done
  429. )
  430. }
  431. function _omz::pr::test {
  432. # Allow $1 to be a URL to the pull request
  433. if [[ "$1" = https://* ]]; then
  434. 1="${1:t}"
  435. fi
  436. # Check the input
  437. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  438. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  439. return 1
  440. fi
  441. # Save current git HEAD
  442. local branch
  443. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  444. _omz::log error "error when getting the current git branch. Aborting..."
  445. return 1
  446. }
  447. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  448. # If any of these operations fail, undo the changes made
  449. (
  450. set -e
  451. builtin cd -q "$ZSH"
  452. # Get the ohmyzsh git remote
  453. command git remote -v | while read remote url _; do
  454. case "$url" in
  455. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  456. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  457. esac
  458. done
  459. (( $found )) || {
  460. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  461. return 1
  462. }
  463. # Fetch pull request head
  464. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  465. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  466. _omz::log error "error when trying to fetch PR #$1."
  467. return 1
  468. }
  469. # Rebase pull request branch against the current master
  470. _omz::log info "rebasing PR #$1..."
  471. command git rebase master ohmyzsh/pull-$1 || {
  472. command git rebase --abort &>/dev/null
  473. _omz::log warn "could not rebase PR #$1 on top of master."
  474. _omz::log warn "you might not see the latest stable changes."
  475. _omz::log info "run \`zsh\` to test the changes."
  476. return 1
  477. }
  478. _omz::log info "fetch of PR #${1} successful."
  479. )
  480. # If there was an error, abort running zsh to test the PR
  481. [[ $? -eq 0 ]] || return 1
  482. # Run zsh to test the changes
  483. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  484. command zsh -l
  485. # After testing, go back to the previous HEAD if the user wants
  486. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  487. # Only proceed if the answer is a valid yes option
  488. [[ "$REPLY" != [yY$'\n'] ]] && return
  489. (
  490. set -e
  491. builtin cd -q "$ZSH"
  492. command git checkout "$branch" -- || {
  493. _omz::log error "could not go back to the previous branch ('$branch')."
  494. return 1
  495. }
  496. )
  497. }
  498. function _omz::theme {
  499. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  500. cat <<EOF
  501. Usage: omz theme <command> [options]
  502. Available commands:
  503. list List all available Oh My Zsh themes
  504. use <theme> Load an Oh My Zsh theme
  505. EOF
  506. return 1
  507. }
  508. local command="$1"
  509. shift
  510. _omz::theme::$command "$@"
  511. }
  512. function _omz::theme::list {
  513. local -a custom_themes builtin_themes
  514. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  515. builtin_themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r))
  516. # If the command is being piped, print all found line by line
  517. if [[ ! -t 1 ]]; then
  518. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  519. return
  520. fi
  521. if (( ${#custom_themes} )); then
  522. print -P "%U%BCustom themes%b%u:"
  523. print -l ${(q-)custom_themes} | column
  524. fi
  525. if (( ${#builtin_themes} )); then
  526. (( ${#custom_themes} )) && echo # add a line of separation
  527. print -P "%U%BBuilt-in themes%b%u:"
  528. print -l ${(q-)builtin_themes} | column
  529. fi
  530. }
  531. function _omz::theme::use {
  532. if [[ -z "$1" ]]; then
  533. echo >&2 "Usage: omz theme use <theme>"
  534. return 1
  535. fi
  536. # Respect compatibility with old lookup order
  537. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  538. source "$ZSH_CUSTOM/$1.zsh-theme"
  539. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  540. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  541. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  542. source "$ZSH/themes/$1.zsh-theme"
  543. else
  544. _omz::log error "theme '$1' not found"
  545. return 1
  546. fi
  547. }
  548. function _omz::update {
  549. local last_commit=$(cd "$ZSH"; git rev-parse HEAD)
  550. # Run update script
  551. if [[ "$1" != --unattended ]]; then
  552. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
  553. else
  554. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  555. fi
  556. # Update last updated file
  557. zmodload zsh/datetime
  558. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  559. # Remove update lock if it exists
  560. command rm -rf "$ZSH/log/update.lock"
  561. # Restart the zsh session if there were changes
  562. if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
  563. # Old zsh versions don't have ZSH_ARGZERO
  564. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  565. # Check whether to run a login shell
  566. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  567. fi
  568. }