gnu-utils.plugin.zsh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # ------------------------------------------------------------------------------
  2. # FILE: gnu-utils.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.0.0
  6. # ------------------------------------------------------------------------------
  7. # Detect if GNU coreutils are installed by looking for gwhoami
  8. if [[ ! -x "${commands[gwhoami]}" ]]; then
  9. return
  10. fi
  11. __gnu_utils() {
  12. local -a gcmds
  13. local gcmd
  14. # coreutils
  15. gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
  16. 'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
  17. 'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
  18. 'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
  19. 'gid' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
  20. 'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
  21. 'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
  22. 'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
  23. 'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
  24. 'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
  25. 'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
  26. 'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
  27. 'gwhoami' 'gyes')
  28. # findutils
  29. gcmds+=('gfind' 'gxargs' 'glocate')
  30. # Not part of either coreutils or findutils, installed separately.
  31. gcmds+=('gsed' 'gtar' 'gtime' 'gmake' 'ggrep')
  32. for gcmd in "${gcmds[@]}"; do
  33. # Do nothing if the command isn't found
  34. (( ${+commands[$gcmd]} )) || continue
  35. # This method allows for builtin commands to be primary but it's
  36. # lost if hash -r or rehash is executed, or if $PATH is updated.
  37. # Thus, a preexec hook is needed, which will only run if whoami
  38. # is not already rehashed.
  39. #
  40. hash ${gcmd[2,-1]}=${commands[$gcmd]}
  41. done
  42. return 0
  43. }
  44. __gnu_utils_preexec() {
  45. # Run __gnu_utils when the whoami command is not already rehashed.
  46. # This acts as a sign that we need to rehash all GNU utils.
  47. [[ "${commands[whoami]}" = "${commands[gwhoami]}" ]] || __gnu_utils
  48. }
  49. autoload -Uz add-zsh-hook
  50. add-zsh-hook preexec __gnu_utils_preexec