cli.zsh 24 KB

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