cli.zsh 22 KB

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