cli.zsh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. 'reload:Reload the current zsh session'
  25. 'theme:Manage themes'
  26. 'update:Update Oh My Zsh'
  27. )
  28. if (( CURRENT == 2 )); then
  29. _describe 'command' cmds
  30. elif (( CURRENT == 3 )); then
  31. case "$words[2]" in
  32. changelog) local -a refs
  33. refs=("${(@f)$(command git -C "$ZSH" for-each-ref --format="%(refname:short):%(subject)" refs/heads refs/tags)}")
  34. _describe 'command' refs ;;
  35. plugin) subcmds=(
  36. 'disable:Disable plugin(s)'
  37. 'enable:Enable plugin(s)'
  38. 'info:Get plugin information'
  39. 'list:List plugins'
  40. 'load:Load plugin(s)'
  41. )
  42. _describe 'command' subcmds ;;
  43. pr) subcmds=('clean:Delete all Pull Request branches' 'test:Test a Pull Request')
  44. _describe 'command' subcmds ;;
  45. theme) subcmds=('list:List themes' 'set:Set a theme in your .zshrc file' 'use:Load a theme')
  46. _describe 'command' subcmds ;;
  47. esac
  48. elif (( CURRENT == 4 )); then
  49. case "${words[2]}::${words[3]}" in
  50. plugin::(disable|enable|load))
  51. local -aU valid_plugins
  52. if [[ "${words[3]}" = disable ]]; then
  53. # if command is "disable", only offer already enabled plugins
  54. valid_plugins=($plugins)
  55. else
  56. valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  57. # if command is "enable", remove already enabled plugins
  58. [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
  59. fi
  60. _describe 'plugin' valid_plugins ;;
  61. plugin::info)
  62. local -aU plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  63. _describe 'plugin' plugins ;;
  64. theme::(set|use))
  65. local -aU themes=("$ZSH"/themes/*.zsh-theme(.N:t:r) "$ZSH_CUSTOM"/**/*.zsh-theme(.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  66. _describe 'theme' themes ;;
  67. esac
  68. elif (( CURRENT > 4 )); then
  69. case "${words[2]}::${words[3]}" in
  70. plugin::(enable|disable|load))
  71. local -aU valid_plugins
  72. if [[ "${words[3]}" = disable ]]; then
  73. # if command is "disable", only offer already enabled plugins
  74. valid_plugins=($plugins)
  75. else
  76. valid_plugins=("$ZSH"/plugins/*/{_*,*.plugin.zsh}(.N:h:t) "$ZSH_CUSTOM"/plugins/*/{_*,*.plugin.zsh}(.N:h:t))
  77. # if command is "enable", remove already enabled plugins
  78. [[ "${words[3]}" = enable ]] && valid_plugins=(${valid_plugins:|plugins})
  79. fi
  80. # Remove plugins already passed as arguments
  81. # NOTE: $(( CURRENT - 1 )) is the last plugin argument completely passed, i.e. that which
  82. # has a space after them. This is to avoid removing plugins partially passed, which makes
  83. # the completion not add a space after the completed plugin.
  84. local -a args=(${words[4,$(( CURRENT - 1))]})
  85. valid_plugins=(${valid_plugins:|args})
  86. _describe 'plugin' valid_plugins ;;
  87. esac
  88. fi
  89. return 0
  90. }
  91. compdef _omz omz
  92. ## Utility functions
  93. function _omz::confirm {
  94. # If question supplied, ask it before reading the answer
  95. # NOTE: uses the logname of the caller function
  96. if [[ -n "$1" ]]; then
  97. _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  98. fi
  99. # Read one character
  100. read -r -k 1
  101. # If no newline entered, add a newline
  102. if [[ "$REPLY" != $'\n' ]]; then
  103. echo
  104. fi
  105. }
  106. function _omz::log {
  107. # if promptsubst is set, a message with `` or $()
  108. # will be run even if quoted due to `print -P`
  109. setopt localoptions nopromptsubst
  110. # $1 = info|warn|error|debug
  111. # $2 = text
  112. # $3 = (optional) name of the logger
  113. local logtype=$1
  114. local logname=${3:-${${functrace[1]#_}%:*}}
  115. # Don't print anything if debug is not active
  116. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  117. return
  118. fi
  119. # Choose coloring based on log type
  120. case "$logtype" in
  121. prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
  122. debug) print -P "%F{white}$logname%f: $2" ;;
  123. info) print -P "%F{green}$logname%f: $2" ;;
  124. warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
  125. error) print -P "%S%F{red}$logname%f%s: $2" ;;
  126. esac >&2
  127. }
  128. ## User-facing commands
  129. function _omz::help {
  130. cat >&2 <<EOF
  131. Usage: omz <command> [options]
  132. Available commands:
  133. help Print this help message
  134. changelog Print the changelog
  135. plugin <command> Manage plugins
  136. pr <command> Manage Oh My Zsh Pull Requests
  137. reload Reload the current zsh session
  138. theme <command> Manage themes
  139. update Update Oh My Zsh
  140. EOF
  141. }
  142. function _omz::changelog {
  143. local version=${1:-HEAD} format=${3:-"--text"}
  144. if ! command git -C "$ZSH" show-ref --verify refs/heads/$version &>/dev/null && \
  145. ! command git -C "$ZSH" show-ref --verify refs/tags/$version &>/dev/null && \
  146. ! command git -C "$ZSH" rev-parse --verify "${version}^{commit}" &>/dev/null; then
  147. cat >&2 <<EOF
  148. Usage: omz changelog [version]
  149. NOTE: <version> must be a valid branch, tag or commit.
  150. EOF
  151. return 1
  152. fi
  153. "$ZSH/tools/changelog.sh" "$version" "${2:-}" "$format"
  154. }
  155. function _omz::plugin {
  156. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  157. cat >&2 <<EOF
  158. Usage: omz plugin <command> [options]
  159. Available commands:
  160. disable <plugin> Disable plugin(s)
  161. enable <plugin> Enable plugin(s)
  162. info <plugin> Get information of a plugin
  163. list List all available Oh My Zsh plugins
  164. load <plugin> Load plugin(s)
  165. EOF
  166. return 1
  167. }
  168. local command="$1"
  169. shift
  170. _omz::plugin::$command "$@"
  171. }
  172. function _omz::plugin::disable {
  173. if [[ -z "$1" ]]; then
  174. echo >&2 "Usage: omz plugin disable <plugin> [...]"
  175. return 1
  176. fi
  177. # Check that plugin is in $plugins
  178. local -a dis_plugins=()
  179. for plugin in "$@"; do
  180. if [[ ${plugins[(Ie)$plugin]} -eq 0 ]]; then
  181. _omz::log warn "plugin '$plugin' is not enabled."
  182. continue
  183. fi
  184. dis_plugins+=("$plugin")
  185. done
  186. # Exit if there are no enabled plugins to disable
  187. if [[ ${#dis_plugins} -eq 0 ]]; then
  188. return 1
  189. fi
  190. # Remove plugins substitution awk script
  191. local awk_subst_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. "
  196. # Disable plugins awk script
  197. local awk_script="
  198. # if plugins=() is in oneline form, substitute disabled plugins and go to next line
  199. /^\s*plugins=\([^#]+\).*\$/ {
  200. $awk_subst_plugins
  201. print \$0
  202. next
  203. }
  204. # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
  205. /^\s*plugins=\(/ {
  206. multi=1
  207. $awk_subst_plugins
  208. print \$0
  209. next
  210. }
  211. # if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag
  212. multi == 1 && /^[^#]*\)/ {
  213. multi=0
  214. $awk_subst_plugins
  215. print \$0
  216. next
  217. }
  218. multi == 1 && length(\$0) > 0 {
  219. $awk_subst_plugins
  220. if (length(\$0) > 0) print \$0
  221. next
  222. }
  223. { print \$0 }
  224. "
  225. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  226. && command mv -f ~/.zshrc ~/.zshrc.bck \
  227. && command mv -f ~/.zshrc.new ~/.zshrc
  228. # Exit if the new .zshrc file wasn't created correctly
  229. [[ $? -eq 0 ]] || {
  230. local ret=$?
  231. _omz::log error "error disabling plugins."
  232. return $ret
  233. }
  234. # Exit if the new .zshrc file has syntax errors
  235. if ! zsh -n ~/.zshrc; then
  236. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  237. command mv -f ~/.zshrc ~/.zshrc.new
  238. command mv -f ~/.zshrc.bck ~/.zshrc
  239. return 1
  240. fi
  241. # Restart the zsh session if there were no errors
  242. _omz::log info "plugins disabled: ${(j:, :)dis_plugins}."
  243. # Old zsh versions don't have ZSH_ARGZERO
  244. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  245. # Check whether to run a login shell
  246. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  247. }
  248. function _omz::plugin::enable {
  249. if [[ -z "$1" ]]; then
  250. echo >&2 "Usage: omz plugin enable <plugin> [...]"
  251. return 1
  252. fi
  253. # Check that plugin is not in $plugins
  254. local -a add_plugins=()
  255. for plugin in "$@"; do
  256. if [[ ${plugins[(Ie)$plugin]} -ne 0 ]]; then
  257. _omz::log warn "plugin '$plugin' is already enabled."
  258. continue
  259. fi
  260. add_plugins+=("$plugin")
  261. done
  262. # Exit if there are no plugins to enable
  263. if [[ ${#add_plugins} -eq 0 ]]; then
  264. return 1
  265. fi
  266. # Enable plugins awk script
  267. local awk_script="
  268. # if plugins=() is in oneline form, substitute ) with new plugins and go to the next line
  269. /^\s*plugins=\([^#]+\).*\$/ {
  270. sub(/\)/, \" $add_plugins&\")
  271. print \$0
  272. next
  273. }
  274. # if plugins=() is in multiline form, enable multi flag
  275. /^\s*plugins=\(/ {
  276. multi=1
  277. }
  278. # if multi flag is enabled and we find a valid closing parenthesis,
  279. # add new plugins and disable multi flag
  280. multi == 1 && /^[^#]*\)/ {
  281. multi=0
  282. sub(/\)/, \" $add_plugins&\")
  283. print \$0
  284. next
  285. }
  286. { print \$0 }
  287. "
  288. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  289. && command mv -f ~/.zshrc ~/.zshrc.bck \
  290. && command mv -f ~/.zshrc.new ~/.zshrc
  291. # Exit if the new .zshrc file wasn't created correctly
  292. [[ $? -eq 0 ]] || {
  293. local ret=$?
  294. _omz::log error "error enabling plugins."
  295. return $ret
  296. }
  297. # Exit if the new .zshrc file has syntax errors
  298. if ! zsh -n ~/.zshrc; then
  299. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  300. command mv -f ~/.zshrc ~/.zshrc.new
  301. command mv -f ~/.zshrc.bck ~/.zshrc
  302. return 1
  303. fi
  304. # Restart the zsh session if there were no errors
  305. _omz::log info "plugins enabled: ${(j:, :)add_plugins}."
  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 -x
  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 -x
  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 >&2 <<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::reload {
  499. # Delete current completion cache
  500. command rm -f $_comp_dumpfile $ZSH_COMPDUMP
  501. # Old zsh versions don't have ZSH_ARGZERO
  502. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  503. # Check whether to run a login shell
  504. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  505. }
  506. function _omz::theme {
  507. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  508. cat >&2 <<EOF
  509. Usage: omz theme <command> [options]
  510. Available commands:
  511. list List all available Oh My Zsh themes
  512. set <theme> Set a theme in your .zshrc file
  513. use <theme> Load a theme
  514. EOF
  515. return 1
  516. }
  517. local command="$1"
  518. shift
  519. _omz::theme::$command "$@"
  520. }
  521. function _omz::theme::list {
  522. local -a custom_themes builtin_themes
  523. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  524. builtin_themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r))
  525. # If the command is being piped, print all found line by line
  526. if [[ ! -t 1 ]]; then
  527. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  528. return
  529. fi
  530. # Print theme in use
  531. if [[ -n "$ZSH_THEME" ]]; then
  532. print -Pn "%U%BCurrent theme%b%u: "
  533. [[ $ZSH_THEME = random ]] && echo "$RANDOM_THEME (via random)" || echo "$ZSH_THEME"
  534. echo
  535. fi
  536. # Print custom themes if there are any
  537. if (( ${#custom_themes} )); then
  538. print -P "%U%BCustom themes%b%u:"
  539. print -l ${(q-)custom_themes} | column -x
  540. echo
  541. fi
  542. # Print built-in themes
  543. print -P "%U%BBuilt-in themes%b%u:"
  544. print -l ${(q-)builtin_themes} | column -x
  545. }
  546. function _omz::theme::set {
  547. if [[ -z "$1" ]]; then
  548. echo >&2 "Usage: omz theme set <theme>"
  549. return 1
  550. fi
  551. # Check that theme exists
  552. if [[ ! -f "$ZSH_CUSTOM/$1.zsh-theme" ]] \
  553. && [[ ! -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]] \
  554. && [[ ! -f "$ZSH/themes/$1.zsh-theme" ]]; then
  555. _omz::log error "%B$1%b theme not found"
  556. return 1
  557. fi
  558. # Enable theme in .zshrc
  559. local awk_script='
  560. !set && /^\s*ZSH_THEME=[^#]+.*$/ {
  561. set=1
  562. sub(/^\s*ZSH_THEME=[^#]+.*$/, "ZSH_THEME=\"'$1'\" # set by `omz`")
  563. print $0
  564. next
  565. }
  566. { print $0 }
  567. END {
  568. # If no ZSH_THEME= line was found, return an error
  569. if (!set) exit 1
  570. }
  571. '
  572. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  573. || {
  574. # Prepend ZSH_THEME= line to .zshrc if it doesn't exist
  575. cat <<EOF
  576. ZSH_THEME="$1" # set by \`omz\`
  577. EOF
  578. cat ~/.zshrc
  579. } > ~/.zshrc.new \
  580. && command mv -f ~/.zshrc ~/.zshrc.bck \
  581. && command mv -f ~/.zshrc.new ~/.zshrc
  582. # Exit if the new .zshrc file wasn't created correctly
  583. [[ $? -eq 0 ]] || {
  584. local ret=$?
  585. _omz::log error "error setting theme."
  586. return $ret
  587. }
  588. # Exit if the new .zshrc file has syntax errors
  589. if ! zsh -n ~/.zshrc; then
  590. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  591. command mv -f ~/.zshrc ~/.zshrc.new
  592. command mv -f ~/.zshrc.bck ~/.zshrc
  593. return 1
  594. fi
  595. # Restart the zsh session if there were no errors
  596. _omz::log info "'$1' theme set correctly."
  597. # Old zsh versions don't have ZSH_ARGZERO
  598. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  599. # Check whether to run a login shell
  600. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  601. }
  602. function _omz::theme::use {
  603. if [[ -z "$1" ]]; then
  604. echo >&2 "Usage: omz theme use <theme>"
  605. return 1
  606. fi
  607. # Respect compatibility with old lookup order
  608. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  609. source "$ZSH_CUSTOM/$1.zsh-theme"
  610. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  611. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  612. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  613. source "$ZSH/themes/$1.zsh-theme"
  614. else
  615. _omz::log error "%B$1%b theme not found"
  616. return 1
  617. fi
  618. # Update theme settings
  619. ZSH_THEME="$1"
  620. [[ $1 = random ]] || unset RANDOM_THEME
  621. }
  622. function _omz::update {
  623. local last_commit=$(cd "$ZSH"; git rev-parse HEAD)
  624. # Run update script
  625. if [[ "$1" != --unattended ]]; then
  626. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
  627. else
  628. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh"
  629. fi
  630. # Update last updated file
  631. zmodload zsh/datetime
  632. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  633. # Remove update lock if it exists
  634. command rm -rf "$ZSH/log/update.lock"
  635. # Restart the zsh session if there were changes
  636. if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
  637. # Old zsh versions don't have ZSH_ARGZERO
  638. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  639. # Check whether to run a login shell
  640. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  641. fi
  642. }