cli.zsh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before
  193. gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after
  194. gsub(/\((${(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. gsub(/\s+(${(j:|:)dis_plugins})/, \"\")
  202. gsub(/(${(j:|:)dis_plugins})\s+/, \"\")
  203. gsub(/\((${(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. gsub(/\s+(${(j:|:)dis_plugins})/, \"\")
  212. gsub(/(${(j:|:)dis_plugins})\s+/, \"\")
  213. gsub(/\((${(j:|:)dis_plugins})\)/, \"\")
  214. print \$0
  215. next
  216. }
  217. multi == 1 {
  218. gsub(/\s+(${(j:|:)dis_plugins})/, \"\")
  219. gsub(/(${(j:|:)dis_plugins})\s+/, \"\")
  220. gsub(/\((${(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. command mv -f ~/.zshrc ~/.zshrc.disabled
  239. command mv -f ~/.zshrc.swp ~/.zshrc
  240. return 1
  241. fi
  242. # Restart the zsh session if there were no errors
  243. _omz::log info "plugins disabled: ${(j:, :)dis_plugins}."
  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.enabled \
  290. && command mv -f ~/.zshrc ~/.zshrc.swp \
  291. && command mv -f ~/.zshrc.enabled ~/.zshrc
  292. # Exit if the new .zshrc file wasn't created correctly
  293. [[ $? -eq 0 ]] || {
  294. local ret=$?
  295. _omz::log error "error enabling 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. command mv -f ~/.zshrc ~/.zshrc.enabled
  302. command mv -f ~/.zshrc.swp ~/.zshrc
  303. return 1
  304. fi
  305. # Restart the zsh session if there were no errors
  306. _omz::log info "plugins enabled: ${(j:, :)add_plugins}."
  307. # Old zsh versions don't have ZSH_ARGZERO
  308. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  309. # Check whether to run a login shell
  310. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  311. }
  312. function _omz::plugin::info {
  313. if [[ -z "$1" ]]; then
  314. echo >&2 "Usage: omz plugin info <plugin>"
  315. return 1
  316. fi
  317. local readme
  318. for readme in "$ZSH_CUSTOM/plugins/$1/README.md" "$ZSH/plugins/$1/README.md"; do
  319. if [[ -f "$readme" ]]; then
  320. (( ${+commands[less]} )) && less "$readme" || cat "$readme"
  321. return 0
  322. fi
  323. done
  324. if [[ -d "$ZSH_CUSTOM/plugins/$1" || -d "$ZSH/plugins/$1" ]]; then
  325. _omz::log error "the '$1' plugin doesn't have a README file"
  326. else
  327. _omz::log error "'$1' plugin not found"
  328. fi
  329. return 1
  330. }
  331. function _omz::plugin::list {
  332. local -a custom_plugins builtin_plugins
  333. custom_plugins=("$ZSH_CUSTOM"/plugins/*(-/N:t))
  334. builtin_plugins=("$ZSH"/plugins/*(-/N:t))
  335. # If the command is being piped, print all found line by line
  336. if [[ ! -t 1 ]]; then
  337. print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
  338. return
  339. fi
  340. if (( ${#custom_plugins} )); then
  341. print -P "%U%BCustom plugins%b%u:"
  342. print -l ${(q-)custom_plugins} | column
  343. fi
  344. if (( ${#builtin_plugins} )); then
  345. (( ${#custom_plugins} )) && echo # add a line of separation
  346. print -P "%U%BBuilt-in plugins%b%u:"
  347. print -l ${(q-)builtin_plugins} | column
  348. fi
  349. }
  350. function _omz::plugin::load {
  351. if [[ -z "$1" ]]; then
  352. echo >&2 "Usage: omz plugin load <plugin> [...]"
  353. return 1
  354. fi
  355. local plugins=("$@")
  356. local plugin base has_completion=0
  357. for plugin in $plugins; do
  358. if [[ -d "$ZSH_CUSTOM/plugins/$plugin" ]]; then
  359. base="$ZSH_CUSTOM/plugins/$plugin"
  360. elif [[ -d "$ZSH/plugins/$plugin" ]]; then
  361. base="$ZSH/plugins/$plugin"
  362. else
  363. _omz::log warn "plugin '$plugin' not found"
  364. continue
  365. fi
  366. # Check if its a valid plugin
  367. if [[ ! -f "$base/_$plugin" && ! -f "$base/$plugin.plugin.zsh" ]]; then
  368. _omz::log warn "'$plugin' is not a valid plugin"
  369. continue
  370. # It it is a valid plugin, add its directory to $fpath unless it is already there
  371. elif (( ! ${fpath[(Ie)$base]} )); then
  372. fpath=("$base" $fpath)
  373. fi
  374. # Check if it has completion to reload compinit
  375. if [[ -f "$base/_$plugin" ]]; then
  376. has_completion=1
  377. fi
  378. # Load the plugin
  379. if [[ -f "$base/$plugin.plugin.zsh" ]]; then
  380. source "$base/$plugin.plugin.zsh"
  381. fi
  382. done
  383. # If we have completion, we need to reload the completion
  384. # We pass -D to avoid generating a new dump file, which would overwrite our
  385. # current one for the next session (and we don't want that because we're not
  386. # actually enabling the plugins for the next session).
  387. # Note that we still have to pass -d "$_comp_dumpfile", so that compinit
  388. # doesn't use the default zcompdump location (${ZDOTDIR:-$HOME}/.zcompdump).
  389. if (( has_completion )); then
  390. compinit -D -d "$_comp_dumpfile"
  391. fi
  392. }
  393. function _omz::pr {
  394. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  395. cat <<EOF
  396. Usage: omz pr <command> [options]
  397. Available commands:
  398. clean Delete all PR branches (ohmyzsh/pull-*)
  399. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  400. EOF
  401. return 1
  402. }
  403. local command="$1"
  404. shift
  405. _omz::pr::$command "$@"
  406. }
  407. function _omz::pr::clean {
  408. (
  409. set -e
  410. builtin cd -q "$ZSH"
  411. # Check if there are PR branches
  412. local fmt branches
  413. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  414. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  415. # Exit if there are no PR branches
  416. if [[ -z "$branches" ]]; then
  417. _omz::log info "there are no Pull Request branches to remove."
  418. return
  419. fi
  420. # Print found PR branches
  421. echo "$branches\n"
  422. # Confirm before removing the branches
  423. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  424. # Only proceed if the answer is a valid yes option
  425. [[ "$REPLY" != [yY$'\n'] ]] && return
  426. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  427. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  428. command git branch -D "$branch"
  429. done
  430. )
  431. }
  432. function _omz::pr::test {
  433. # Allow $1 to be a URL to the pull request
  434. if [[ "$1" = https://* ]]; then
  435. 1="${1:t}"
  436. fi
  437. # Check the input
  438. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  439. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  440. return 1
  441. fi
  442. # Save current git HEAD
  443. local branch
  444. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  445. _omz::log error "error when getting the current git branch. Aborting..."
  446. return 1
  447. }
  448. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  449. # If any of these operations fail, undo the changes made
  450. (
  451. set -e
  452. builtin cd -q "$ZSH"
  453. # Get the ohmyzsh git remote
  454. command git remote -v | while read remote url _; do
  455. case "$url" in
  456. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  457. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  458. esac
  459. done
  460. (( $found )) || {
  461. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  462. return 1
  463. }
  464. # Fetch pull request head
  465. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  466. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  467. _omz::log error "error when trying to fetch PR #$1."
  468. return 1
  469. }
  470. # Rebase pull request branch against the current master
  471. _omz::log info "rebasing PR #$1..."
  472. command git rebase master ohmyzsh/pull-$1 || {
  473. command git rebase --abort &>/dev/null
  474. _omz::log warn "could not rebase PR #$1 on top of master."
  475. _omz::log warn "you might not see the latest stable changes."
  476. _omz::log info "run \`zsh\` to test the changes."
  477. return 1
  478. }
  479. _omz::log info "fetch of PR #${1} successful."
  480. )
  481. # If there was an error, abort running zsh to test the PR
  482. [[ $? -eq 0 ]] || return 1
  483. # Run zsh to test the changes
  484. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  485. command zsh -l
  486. # After testing, go back to the previous HEAD if the user wants
  487. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  488. # Only proceed if the answer is a valid yes option
  489. [[ "$REPLY" != [yY$'\n'] ]] && return
  490. (
  491. set -e
  492. builtin cd -q "$ZSH"
  493. command git checkout "$branch" -- || {
  494. _omz::log error "could not go back to the previous branch ('$branch')."
  495. return 1
  496. }
  497. )
  498. }
  499. function _omz::theme {
  500. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  501. cat <<EOF
  502. Usage: omz theme <command> [options]
  503. Available commands:
  504. list List all available Oh My Zsh themes
  505. use <theme> Load an Oh My Zsh theme
  506. EOF
  507. return 1
  508. }
  509. local command="$1"
  510. shift
  511. _omz::theme::$command "$@"
  512. }
  513. function _omz::theme::list {
  514. local -a custom_themes builtin_themes
  515. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  516. builtin_themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r))
  517. # If the command is being piped, print all found line by line
  518. if [[ ! -t 1 ]]; then
  519. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  520. return
  521. fi
  522. if (( ${#custom_themes} )); then
  523. print -P "%U%BCustom themes%b%u:"
  524. print -l ${(q-)custom_themes} | column
  525. fi
  526. if (( ${#builtin_themes} )); then
  527. (( ${#custom_themes} )) && echo # add a line of separation
  528. print -P "%U%BBuilt-in themes%b%u:"
  529. print -l ${(q-)builtin_themes} | column
  530. fi
  531. }
  532. function _omz::theme::use {
  533. if [[ -z "$1" ]]; then
  534. echo >&2 "Usage: omz theme use <theme>"
  535. return 1
  536. fi
  537. # Respect compatibility with old lookup order
  538. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  539. source "$ZSH_CUSTOM/$1.zsh-theme"
  540. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  541. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  542. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  543. source "$ZSH/themes/$1.zsh-theme"
  544. else
  545. _omz::log error "theme '$1' not found"
  546. return 1
  547. fi
  548. }
  549. function _omz::update {
  550. local last_commit=$(cd "$ZSH"; git rev-parse HEAD)
  551. # Run update script
  552. if [[ "$1" != --unattended ]]; then
  553. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
  554. else
  555. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  556. fi
  557. # Update last updated file
  558. zmodload zsh/datetime
  559. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  560. # Remove update lock if it exists
  561. command rm -rf "$ZSH/log/update.lock"
  562. # Restart the zsh session if there were changes
  563. if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
  564. # Old zsh versions don't have ZSH_ARGZERO
  565. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  566. # Check whether to run a login shell
  567. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  568. fi
  569. }