cli.zsh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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)$(cd "$ZSH"; command git 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 (
  146. cd "$ZSH"
  147. ! command git show-ref --verify refs/heads/$version && \
  148. ! command git show-ref --verify refs/tags/$version && \
  149. ! command git rev-parse --verify "${version}^{commit}"
  150. ) &>/dev/null; then
  151. cat >&2 <<EOF
  152. Usage: omz changelog [version]
  153. NOTE: <version> must be a valid branch, tag or commit.
  154. EOF
  155. return 1
  156. fi
  157. "$ZSH/tools/changelog.sh" "$version" "${2:-}" "$format"
  158. }
  159. function _omz::plugin {
  160. (( $# > 0 && $+functions[_omz::plugin::$1] )) || {
  161. cat >&2 <<EOF
  162. Usage: omz plugin <command> [options]
  163. Available commands:
  164. disable <plugin> Disable plugin(s)
  165. enable <plugin> Enable plugin(s)
  166. info <plugin> Get information of a plugin
  167. list List all available Oh My Zsh plugins
  168. load <plugin> Load plugin(s)
  169. EOF
  170. return 1
  171. }
  172. local command="$1"
  173. shift
  174. _omz::plugin::$command "$@"
  175. }
  176. function _omz::plugin::disable {
  177. if [[ -z "$1" ]]; then
  178. echo >&2 "Usage: omz plugin disable <plugin> [...]"
  179. return 1
  180. fi
  181. # Check that plugin is in $plugins
  182. local -a dis_plugins
  183. for plugin in "$@"; do
  184. if [[ ${plugins[(Ie)$plugin]} -eq 0 ]]; then
  185. _omz::log warn "plugin '$plugin' is not enabled."
  186. continue
  187. fi
  188. dis_plugins+=("$plugin")
  189. done
  190. # Exit if there are no enabled plugins to disable
  191. if [[ ${#dis_plugins} -eq 0 ]]; then
  192. return 1
  193. fi
  194. # Remove plugins substitution awk script
  195. local awk_subst_plugins="\
  196. gsub(/\s+(${(j:|:)dis_plugins})/, \"\") # with spaces before
  197. gsub(/(${(j:|:)dis_plugins})\s+/, \"\") # with spaces after
  198. gsub(/\((${(j:|:)dis_plugins})\)/, \"\") # without spaces (only plugin)
  199. "
  200. # Disable plugins awk script
  201. local awk_script="
  202. # if plugins=() is in oneline form, substitute disabled plugins and go to next line
  203. /^\s*plugins=\([^#]+\).*\$/ {
  204. $awk_subst_plugins
  205. print \$0
  206. next
  207. }
  208. # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
  209. /^\s*plugins=\(/ {
  210. multi=1
  211. $awk_subst_plugins
  212. print \$0
  213. next
  214. }
  215. # if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag
  216. multi == 1 && /^[^#]*\)/ {
  217. multi=0
  218. $awk_subst_plugins
  219. print \$0
  220. next
  221. }
  222. multi == 1 && length(\$0) > 0 {
  223. $awk_subst_plugins
  224. if (length(\$0) > 0) print \$0
  225. next
  226. }
  227. { print \$0 }
  228. "
  229. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  230. && command mv -f ~/.zshrc ~/.zshrc.bck \
  231. && command mv -f ~/.zshrc.new ~/.zshrc
  232. # Exit if the new .zshrc file wasn't created correctly
  233. [[ $? -eq 0 ]] || {
  234. local ret=$?
  235. _omz::log error "error disabling plugins."
  236. return $ret
  237. }
  238. # Exit if the new .zshrc file has syntax errors
  239. if ! zsh -n ~/.zshrc; then
  240. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  241. command mv -f ~/.zshrc ~/.zshrc.new
  242. command mv -f ~/.zshrc.bck ~/.zshrc
  243. return 1
  244. fi
  245. # Restart the zsh session if there were no errors
  246. _omz::log info "plugins disabled: ${(j:, :)dis_plugins}."
  247. # Old zsh versions don't have ZSH_ARGZERO
  248. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  249. # Check whether to run a login shell
  250. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  251. }
  252. function _omz::plugin::enable {
  253. if [[ -z "$1" ]]; then
  254. echo >&2 "Usage: omz plugin enable <plugin> [...]"
  255. return 1
  256. fi
  257. # Check that plugin is not in $plugins
  258. local -a add_plugins
  259. for plugin in "$@"; do
  260. if [[ ${plugins[(Ie)$plugin]} -ne 0 ]]; then
  261. _omz::log warn "plugin '$plugin' is already enabled."
  262. continue
  263. fi
  264. add_plugins+=("$plugin")
  265. done
  266. # Exit if there are no plugins to enable
  267. if [[ ${#add_plugins} -eq 0 ]]; then
  268. return 1
  269. fi
  270. # Enable plugins awk script
  271. local awk_script="
  272. # if plugins=() is in oneline form, substitute ) with new plugins and go to the next line
  273. /^\s*plugins=\([^#]+\).*\$/ {
  274. sub(/\)/, \" $add_plugins&\")
  275. print \$0
  276. next
  277. }
  278. # if plugins=() is in multiline form, enable multi flag
  279. /^\s*plugins=\(/ {
  280. multi=1
  281. }
  282. # if multi flag is enabled and we find a valid closing parenthesis,
  283. # add new plugins and disable multi flag
  284. multi == 1 && /^[^#]*\)/ {
  285. multi=0
  286. sub(/\)/, \" $add_plugins&\")
  287. print \$0
  288. next
  289. }
  290. { print \$0 }
  291. "
  292. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  293. && command mv -f ~/.zshrc ~/.zshrc.bck \
  294. && command mv -f ~/.zshrc.new ~/.zshrc
  295. # Exit if the new .zshrc file wasn't created correctly
  296. [[ $? -eq 0 ]] || {
  297. local ret=$?
  298. _omz::log error "error enabling plugins."
  299. return $ret
  300. }
  301. # Exit if the new .zshrc file has syntax errors
  302. if ! zsh -n ~/.zshrc; then
  303. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  304. command mv -f ~/.zshrc ~/.zshrc.new
  305. command mv -f ~/.zshrc.bck ~/.zshrc
  306. return 1
  307. fi
  308. # Restart the zsh session if there were no errors
  309. _omz::log info "plugins enabled: ${(j:, :)add_plugins}."
  310. # Old zsh versions don't have ZSH_ARGZERO
  311. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  312. # Check whether to run a login shell
  313. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  314. }
  315. function _omz::plugin::info {
  316. if [[ -z "$1" ]]; then
  317. echo >&2 "Usage: omz plugin info <plugin>"
  318. return 1
  319. fi
  320. local readme
  321. for readme in "$ZSH_CUSTOM/plugins/$1/README.md" "$ZSH/plugins/$1/README.md"; do
  322. if [[ -f "$readme" ]]; then
  323. (( ${+commands[less]} )) && less "$readme" || cat "$readme"
  324. return 0
  325. fi
  326. done
  327. if [[ -d "$ZSH_CUSTOM/plugins/$1" || -d "$ZSH/plugins/$1" ]]; then
  328. _omz::log error "the '$1' plugin doesn't have a README file"
  329. else
  330. _omz::log error "'$1' plugin not found"
  331. fi
  332. return 1
  333. }
  334. function _omz::plugin::list {
  335. local -a custom_plugins builtin_plugins
  336. custom_plugins=("$ZSH_CUSTOM"/plugins/*(-/N:t))
  337. builtin_plugins=("$ZSH"/plugins/*(-/N:t))
  338. # If the command is being piped, print all found line by line
  339. if [[ ! -t 1 ]]; then
  340. print -l ${(q-)custom_plugins} ${(q-)builtin_plugins}
  341. return
  342. fi
  343. if (( ${#custom_plugins} )); then
  344. print -P "%U%BCustom plugins%b%u:"
  345. print -l ${(q-)custom_plugins} | column -x
  346. fi
  347. if (( ${#builtin_plugins} )); then
  348. (( ${#custom_plugins} )) && echo # add a line of separation
  349. print -P "%U%BBuilt-in plugins%b%u:"
  350. print -l ${(q-)builtin_plugins} | column -x
  351. fi
  352. }
  353. function _omz::plugin::load {
  354. if [[ -z "$1" ]]; then
  355. echo >&2 "Usage: omz plugin load <plugin> [...]"
  356. return 1
  357. fi
  358. local plugin base has_completion=0
  359. for plugin in "$@"; do
  360. if [[ -d "$ZSH_CUSTOM/plugins/$plugin" ]]; then
  361. base="$ZSH_CUSTOM/plugins/$plugin"
  362. elif [[ -d "$ZSH/plugins/$plugin" ]]; then
  363. base="$ZSH/plugins/$plugin"
  364. else
  365. _omz::log warn "plugin '$plugin' not found"
  366. continue
  367. fi
  368. # Check if its a valid plugin
  369. if [[ ! -f "$base/_$plugin" && ! -f "$base/$plugin.plugin.zsh" ]]; then
  370. _omz::log warn "'$plugin' is not a valid plugin"
  371. continue
  372. # It it is a valid plugin, add its directory to $fpath unless it is already there
  373. elif (( ! ${fpath[(Ie)$base]} )); then
  374. fpath=("$base" $fpath)
  375. fi
  376. # Check if it has completion to reload compinit
  377. local -a comp_files
  378. comp_files=($base/_*(N))
  379. has_completion=$(( $#comp_files > 0 ))
  380. # Load the plugin
  381. if [[ -f "$base/$plugin.plugin.zsh" ]]; then
  382. source "$base/$plugin.plugin.zsh"
  383. fi
  384. done
  385. # If we have completion, we need to reload the completion
  386. # We pass -D to avoid generating a new dump file, which would overwrite our
  387. # current one for the next session (and we don't want that because we're not
  388. # actually enabling the plugins for the next session).
  389. # Note that we still have to pass -d "$_comp_dumpfile", so that compinit
  390. # doesn't use the default zcompdump location (${ZDOTDIR:-$HOME}/.zcompdump).
  391. if (( has_completion )); then
  392. compinit -D -d "$_comp_dumpfile"
  393. fi
  394. }
  395. function _omz::pr {
  396. (( $# > 0 && $+functions[_omz::pr::$1] )) || {
  397. cat >&2 <<EOF
  398. Usage: omz pr <command> [options]
  399. Available commands:
  400. clean Delete all PR branches (ohmyzsh/pull-*)
  401. test <PR_number_or_URL> Fetch PR #NUMBER and rebase against master
  402. EOF
  403. return 1
  404. }
  405. local command="$1"
  406. shift
  407. _omz::pr::$command "$@"
  408. }
  409. function _omz::pr::clean {
  410. (
  411. set -e
  412. builtin cd -q "$ZSH"
  413. # Check if there are PR branches
  414. local fmt branches
  415. fmt="%(color:bold blue)%(align:18,right)%(refname:short)%(end)%(color:reset) %(color:dim bold red)%(objectname:short)%(color:reset) %(color:yellow)%(contents:subject)"
  416. branches="$(command git for-each-ref --sort=-committerdate --color --format="$fmt" "refs/heads/ohmyzsh/pull-*")"
  417. # Exit if there are no PR branches
  418. if [[ -z "$branches" ]]; then
  419. _omz::log info "there are no Pull Request branches to remove."
  420. return
  421. fi
  422. # Print found PR branches
  423. echo "$branches\n"
  424. # Confirm before removing the branches
  425. _omz::confirm "do you want remove these Pull Request branches? [Y/n] "
  426. # Only proceed if the answer is a valid yes option
  427. [[ "$REPLY" != [yY$'\n'] ]] && return
  428. _omz::log info "removing all Oh My Zsh Pull Request branches..."
  429. command git branch --list 'ohmyzsh/pull-*' | while read branch; do
  430. command git branch -D "$branch"
  431. done
  432. )
  433. }
  434. function _omz::pr::test {
  435. # Allow $1 to be a URL to the pull request
  436. if [[ "$1" = https://* ]]; then
  437. 1="${1:t}"
  438. fi
  439. # Check the input
  440. if ! [[ -n "$1" && "$1" =~ ^[[:digit:]]+$ ]]; then
  441. echo >&2 "Usage: omz pr test <PR_NUMBER_or_URL>"
  442. return 1
  443. fi
  444. # Save current git HEAD
  445. local branch
  446. branch=$(builtin cd -q "$ZSH"; git symbolic-ref --short HEAD) || {
  447. _omz::log error "error when getting the current git branch. Aborting..."
  448. return 1
  449. }
  450. # Fetch PR onto ohmyzsh/pull-<PR_NUMBER> branch and rebase against master
  451. # If any of these operations fail, undo the changes made
  452. (
  453. set -e
  454. builtin cd -q "$ZSH"
  455. # Get the ohmyzsh git remote
  456. command git remote -v | while read remote url _; do
  457. case "$url" in
  458. https://github.com/ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  459. git@github.com:ohmyzsh/ohmyzsh(|.git)) found=1; break ;;
  460. esac
  461. done
  462. (( $found )) || {
  463. _omz::log error "could not found the ohmyzsh git remote. Aborting..."
  464. return 1
  465. }
  466. # Fetch pull request head
  467. _omz::log info "fetching PR #$1 to ohmyzsh/pull-$1..."
  468. command git fetch -f "$remote" refs/pull/$1/head:ohmyzsh/pull-$1 || {
  469. _omz::log error "error when trying to fetch PR #$1."
  470. return 1
  471. }
  472. # Rebase pull request branch against the current master
  473. _omz::log info "rebasing PR #$1..."
  474. command git rebase master ohmyzsh/pull-$1 || {
  475. command git rebase --abort &>/dev/null
  476. _omz::log warn "could not rebase PR #$1 on top of master."
  477. _omz::log warn "you might not see the latest stable changes."
  478. _omz::log info "run \`zsh\` to test the changes."
  479. return 1
  480. }
  481. _omz::log info "fetch of PR #${1} successful."
  482. )
  483. # If there was an error, abort running zsh to test the PR
  484. [[ $? -eq 0 ]] || return 1
  485. # Run zsh to test the changes
  486. _omz::log info "running \`zsh\` to test the changes. Run \`exit\` to go back."
  487. command zsh -l
  488. # After testing, go back to the previous HEAD if the user wants
  489. _omz::confirm "do you want to go back to the previous branch? [Y/n] "
  490. # Only proceed if the answer is a valid yes option
  491. [[ "$REPLY" != [yY$'\n'] ]] && return
  492. (
  493. set -e
  494. builtin cd -q "$ZSH"
  495. command git checkout "$branch" -- || {
  496. _omz::log error "could not go back to the previous branch ('$branch')."
  497. return 1
  498. }
  499. )
  500. }
  501. function _omz::reload {
  502. # Delete current completion cache
  503. command rm -f $_comp_dumpfile $ZSH_COMPDUMP
  504. # Old zsh versions don't have ZSH_ARGZERO
  505. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  506. # Check whether to run a login shell
  507. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  508. }
  509. function _omz::theme {
  510. (( $# > 0 && $+functions[_omz::theme::$1] )) || {
  511. cat >&2 <<EOF
  512. Usage: omz theme <command> [options]
  513. Available commands:
  514. list List all available Oh My Zsh themes
  515. set <theme> Set a theme in your .zshrc file
  516. use <theme> Load a theme
  517. EOF
  518. return 1
  519. }
  520. local command="$1"
  521. shift
  522. _omz::theme::$command "$@"
  523. }
  524. function _omz::theme::list {
  525. local -a custom_themes builtin_themes
  526. custom_themes=("$ZSH_CUSTOM"/**/*.zsh-theme(-.N:r:gs:"$ZSH_CUSTOM"/themes/:::gs:"$ZSH_CUSTOM"/:::))
  527. builtin_themes=("$ZSH"/themes/*.zsh-theme(-.N:t:r))
  528. # If the command is being piped, print all found line by line
  529. if [[ ! -t 1 ]]; then
  530. print -l ${(q-)custom_themes} ${(q-)builtin_themes}
  531. return
  532. fi
  533. # Print theme in use
  534. if [[ -n "$ZSH_THEME" ]]; then
  535. print -Pn "%U%BCurrent theme%b%u: "
  536. [[ $ZSH_THEME = random ]] && echo "$RANDOM_THEME (via random)" || echo "$ZSH_THEME"
  537. echo
  538. fi
  539. # Print custom themes if there are any
  540. if (( ${#custom_themes} )); then
  541. print -P "%U%BCustom themes%b%u:"
  542. print -l ${(q-)custom_themes} | column -x
  543. echo
  544. fi
  545. # Print built-in themes
  546. print -P "%U%BBuilt-in themes%b%u:"
  547. print -l ${(q-)builtin_themes} | column -x
  548. }
  549. function _omz::theme::set {
  550. if [[ -z "$1" ]]; then
  551. echo >&2 "Usage: omz theme set <theme>"
  552. return 1
  553. fi
  554. # Check that theme exists
  555. if [[ ! -f "$ZSH_CUSTOM/$1.zsh-theme" ]] \
  556. && [[ ! -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]] \
  557. && [[ ! -f "$ZSH/themes/$1.zsh-theme" ]]; then
  558. _omz::log error "%B$1%b theme not found"
  559. return 1
  560. fi
  561. # Enable theme in .zshrc
  562. local awk_script='
  563. !set && /^\s*ZSH_THEME=[^#]+.*$/ {
  564. set=1
  565. sub(/^\s*ZSH_THEME=[^#]+.*$/, "ZSH_THEME=\"'$1'\" # set by `omz`")
  566. print $0
  567. next
  568. }
  569. { print $0 }
  570. END {
  571. # If no ZSH_THEME= line was found, return an error
  572. if (!set) exit 1
  573. }
  574. '
  575. awk "$awk_script" ~/.zshrc > ~/.zshrc.new \
  576. || {
  577. # Prepend ZSH_THEME= line to .zshrc if it doesn't exist
  578. cat <<EOF
  579. ZSH_THEME="$1" # set by \`omz\`
  580. EOF
  581. cat ~/.zshrc
  582. } > ~/.zshrc.new \
  583. && command mv -f ~/.zshrc ~/.zshrc.bck \
  584. && command mv -f ~/.zshrc.new ~/.zshrc
  585. # Exit if the new .zshrc file wasn't created correctly
  586. [[ $? -eq 0 ]] || {
  587. local ret=$?
  588. _omz::log error "error setting theme."
  589. return $ret
  590. }
  591. # Exit if the new .zshrc file has syntax errors
  592. if ! zsh -n ~/.zshrc; then
  593. _omz::log error "broken syntax in ~/.zshrc. Rolling back changes..."
  594. command mv -f ~/.zshrc ~/.zshrc.new
  595. command mv -f ~/.zshrc.bck ~/.zshrc
  596. return 1
  597. fi
  598. # Restart the zsh session if there were no errors
  599. _omz::log info "'$1' theme set correctly."
  600. # Old zsh versions don't have ZSH_ARGZERO
  601. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  602. # Check whether to run a login shell
  603. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  604. }
  605. function _omz::theme::use {
  606. if [[ -z "$1" ]]; then
  607. echo >&2 "Usage: omz theme use <theme>"
  608. return 1
  609. fi
  610. # Respect compatibility with old lookup order
  611. if [[ -f "$ZSH_CUSTOM/$1.zsh-theme" ]]; then
  612. source "$ZSH_CUSTOM/$1.zsh-theme"
  613. elif [[ -f "$ZSH_CUSTOM/themes/$1.zsh-theme" ]]; then
  614. source "$ZSH_CUSTOM/themes/$1.zsh-theme"
  615. elif [[ -f "$ZSH/themes/$1.zsh-theme" ]]; then
  616. source "$ZSH/themes/$1.zsh-theme"
  617. else
  618. _omz::log error "%B$1%b theme not found"
  619. return 1
  620. fi
  621. # Update theme settings
  622. ZSH_THEME="$1"
  623. [[ $1 = random ]] || unset RANDOM_THEME
  624. }
  625. function _omz::update {
  626. local last_commit=$(cd "$ZSH"; git rev-parse HEAD)
  627. # Run update script
  628. if [[ "$1" != --unattended ]]; then
  629. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive || return $?
  630. else
  631. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" || return $?
  632. fi
  633. # Update last updated file
  634. zmodload zsh/datetime
  635. echo "LAST_EPOCH=$(( EPOCHSECONDS / 60 / 60 / 24 ))" >! "${ZSH_CACHE_DIR}/.zsh-update"
  636. # Remove update lock if it exists
  637. command rm -rf "$ZSH/log/update.lock"
  638. # Restart the zsh session if there were changes
  639. if [[ "$1" != --unattended && "$(cd "$ZSH"; git rev-parse HEAD)" != "$last_commit" ]]; then
  640. # Old zsh versions don't have ZSH_ARGZERO
  641. local zsh="${ZSH_ARGZERO:-${functrace[-1]%:*}}"
  642. # Check whether to run a login shell
  643. [[ "$zsh" = -* || -o login ]] && exec -l "${zsh#-}" || exec "$zsh"
  644. fi
  645. }