cli.zsh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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
  85. args=(${words[4,$(( CURRENT - 1))]})
  86. valid_plugins=(${valid_plugins:|args})
  87. _describe 'plugin' valid_plugins ;;
  88. esac
  89. fi
  90. return 0
  91. }
  92. compdef _omz omz
  93. ## Utility functions
  94. function _omz::confirm {
  95. # If question supplied, ask it before reading the answer
  96. # NOTE: uses the logname of the caller function
  97. if [[ -n "$1" ]]; then
  98. _omz::log prompt "$1" "${${functrace[1]#_}%:*}"
  99. fi
  100. # Read one character
  101. read -r -k 1
  102. # If no newline entered, add a newline
  103. if [[ "$REPLY" != $'\n' ]]; then
  104. echo
  105. fi
  106. }
  107. function _omz::log {
  108. # if promptsubst is set, a message with `` or $()
  109. # will be run even if quoted due to `print -P`
  110. setopt localoptions nopromptsubst
  111. # $1 = info|warn|error|debug
  112. # $2 = text
  113. # $3 = (optional) name of the logger
  114. local logtype=$1
  115. local logname=${3:-${${functrace[1]#_}%:*}}
  116. # Don't print anything if debug is not active
  117. if [[ $logtype = debug && -z $_OMZ_DEBUG ]]; then
  118. return
  119. fi
  120. # Choose coloring based on log type
  121. case "$logtype" in
  122. prompt) print -Pn "%S%F{blue}$logname%f%s: $2" ;;
  123. debug) print -P "%F{white}$logname%f: $2" ;;
  124. info) print -P "%F{green}$logname%f: $2" ;;
  125. warn) print -P "%S%F{yellow}$logname%f%s: $2" ;;
  126. error) print -P "%S%F{red}$logname%f%s: $2" ;;
  127. esac >&2
  128. }
  129. ## User-facing commands
  130. function _omz::help {
  131. cat >&2 <<EOF
  132. Usage: omz <command> [options]
  133. Available commands:
  134. help Print this help message
  135. changelog Print the changelog
  136. plugin <command> Manage plugins
  137. pr <command> Manage Oh My Zsh Pull Requests
  138. reload Reload the current zsh session
  139. theme <command> Manage themes
  140. update Update Oh My Zsh
  141. EOF
  142. }
  143. function _omz::changelog {
  144. local version=${1:-HEAD} format=${3:-"--text"}
  145. if ! command git -C "$ZSH" show-ref --verify refs/heads/$version &>/dev/null && \
  146. ! command git -C "$ZSH" show-ref --verify refs/tags/$version &>/dev/null && \
  147. ! command git -C "$ZSH" rev-parse --verify "${version}^{commit}" &>/dev/null; then
  148. cat >&2 <<EOF
  149. Usage: omz changelog [version]
  150. NOTE: <version> must be a valid branch, tag or commit.
  151. EOF
  152. return 1
  153. fi
  154. "$ZSH/tools/changelog.sh" "$version" "${2:-}" "$format"
  155. }
  156. function _omz::plugin {
  157. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  158. cat >&2 <<EOF
  159. Usage: omz plugin <command> [options]
  160. Available commands:
  161. disable <plugin> Disable plugin(s)
  162. enable <plugin> Enable plugin(s)
  163. info <plugin> Get information of a plugin
  164. list List all available Oh My Zsh plugins
  165. load <plugin> Load plugin(s)
  166. EOF
  167. return 1
  168. }
  169. local command="$1"
  170. shift
  171. _omz::plugin::$command "$@"
  172. }
  173. function _omz::plugin::disable {
  174. if [[ -z "$1" ]]; then
  175. echo >&2 "Usage: omz plugin disable <plugin> [...]"
  176. return 1
  177. fi
  178. # Check that plugin is in $plugins
  179. local -a dis_plugins
  180. for plugin in "$@"; do
  181. if [[ ${plugins[(Ie)$plugin]} -eq 0 ]]; then
  182. _omz::log warn "plugin '$plugin' is not enabled."
  183. continue
  184. fi
  185. dis_plugins+=("$plugin")
  186. done
  187. # Exit if there are no enabled plugins to disable
  188. if [[ ${#dis_plugins} -eq 0 ]]; then
  189. return 1
  190. fi
  191. # Remove plugins substitution awk script
  192. local awk_subst_plugins="\
  193. gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before
  194. gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after
  195. gsub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin)
  196. "
  197. # Disable plugins awk script
  198. local awk_script="
  199. # if plugins=() is in oneline form, substitute disabled plugins and go to next line
  200. /^\s*plugins=\([^#]+\).*\$/ {
  201. $awk_subst_plugins
  202. print \$0
  203. next
  204. }
  205. # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
  206. /^\s*plugins=\(/ {
  207. multi=1
  208. $awk_subst_plugins
  209. print \$0
  210. next
  211. }
  212. # if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag
  213. multi == 1 && /^[^#]*\)/ {
  214. multi=0
  215. $awk_subst_plugins
  216. print \$0
  217. next
  218. }
  219. multi == 1 && length(\$0) > 0 {
  220. $awk_subst_plugins
  221. if (length(\$0) > 0) print \$0
  222. next
  223. }
  224. { print \$0 }
  225. "
  226. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  227. && command mv -f ~/.zshrc ~/.zshrc.bck \
  228. && command mv -f ~/.zshrc.new ~/.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.new
  239. command mv -f ~/.zshrc.bck ~/.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.new \
  290. && command mv -f ~/.zshrc ~/.zshrc.bck \
  291. && command mv -f ~/.zshrc.new ~/.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.new
  302. command mv -f ~/.zshrc.bck ~/.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 -x
  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 -x
  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 plugin base has_completion=0
  356. for plugin in "$@"; 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. }