cli.zsh 22 KB

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