cli.zsh 24 KB

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